-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement tests and start of user notes
- Loading branch information
1 parent
8e6cc90
commit 1e87cd6
Showing
33 changed files
with
653 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
HTTP_PORT=3010 | ||
|
||
LOG_PATH=./logs | ||
SESSION_SECRET=changeme | ||
|
||
POSTGRES_HOST=localhost | ||
POSTGRES_PORT=5432 | ||
POSTGRES_USER=redose_api | ||
POSTGRES_PASSWORD=P@ssw0rd | ||
POSTGRES_DATABASE=redose_test | ||
|
||
SMTP_HOST=localhost | ||
SMTP_PORT=465 | ||
SMTP_USER=redose_api | ||
SMTP_PASSWORD=P@ssw0rd | ||
|
||
DISCORD_CLIENT_ID=mockDiscordClientId | ||
DISCORD_CLIENT_SECRET=mockDiscordClientSecret | ||
DISCORD_HOME_GUILD_ID=mockDiscordGuildId |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
TEST_USER="${POSTGRES_USER}_test" | ||
TEST_DATABASE="${POSTGRES_DATABASE}_test" | ||
|
||
psql -v ON_ERROR_STOP=1 -U "${POSTGRES_USER}" <<-EOSQL | ||
CREATE USER ${TEST_USER} WITH PASSWORD '${POSTGRES_PASSWORD}'; | ||
CREATE DATABASE ${POSTGRES_DATABASE}; | ||
CREATE DATABASE ${TEST_DATABASE}; | ||
GRANT ALL PRIVILEGES ON DATABASE ${POSTGERS_DATABASE} TO ${POSTGRES_USER}; | ||
GRANT ALL PRIVILEGES ON DATABASE ${TEST_DATABASE} TO ${TEST_USER}; | ||
GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_DATABASE} TO ${POSTGRES_USER}; | ||
GRANT ALL PRIVILEGES ON DATABASE ${TEST_DATABASE} TO ${POSTGRES_USER}; | ||
EOSQL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export default function createMockLogger() { | ||
return { | ||
log: jest.fn(), | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn(), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { | ||
Client, | ||
ClientOptions, | ||
CommandInteraction, | ||
User, | ||
} from 'discord.js'; | ||
|
||
export default async function createMockDiscordClient() { | ||
return class MockDiscordClient { | ||
client!: Client; | ||
user!: User; | ||
interaction!: CommandInteraction; | ||
#options?: ClientOptions; | ||
|
||
constructor(options?: ClientOptions) { | ||
this.#options = options; | ||
this.#mockClient(); | ||
this.#mockUser(); | ||
this.#mockInteractions(); | ||
} | ||
|
||
getInteractions(): CommandInteraction { | ||
return this.interaction; | ||
} | ||
|
||
#mockClient() { | ||
this.client = new Client({ intents: [] }); | ||
this.client.login = jest.fn(() => Promise.resolve('MOCK_LOGIN_TOKEN')); | ||
} | ||
|
||
#mockUser() { | ||
this.user = Reflect.construct(User, [ | ||
this.client, | ||
{ | ||
id: 'mockUserId', | ||
username: 'mockUsername', | ||
discriminator: 'mockDiscriminator#0001', | ||
avatar: 'mockUserAvatar.png', | ||
bot: false, | ||
}, | ||
]); | ||
} | ||
|
||
#mockInteractions() { | ||
this.interaction = Reflect.construct(CommandInteraction, [ | ||
this.client, | ||
{ | ||
data: this.#options, | ||
id: BigInt(1), | ||
user: this.user, | ||
}, | ||
]); | ||
this.interaction.reply = jest.fn(); | ||
this.interaction.isCommand = jest.fn(() => true); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { UserNote } from '@redose/types'; | ||
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; | ||
import type { Command } from '.'; | ||
|
||
const noteCommand: Command = { | ||
meta: new SlashCommandBuilder() | ||
.setName('note') | ||
.setDescription('Manage and create notes associated with a user') | ||
.addSubcommand((subcommand) => subcommand | ||
.setName('add') | ||
.setDescription('Create a new note for a user') | ||
.addUserOption((option) => option | ||
.setName('user') | ||
.setDescription('User to associate note to') | ||
.setRequired(true)) | ||
.addStringOption((option) => option | ||
.setName('contents') | ||
.setDescription('Contents of the note') | ||
.setRequired(true))), | ||
|
||
async execute(interaction, { knex }) { | ||
const userId = interaction.options.getUser('user')!.id; | ||
if (userId === interaction.user.id) { | ||
return interaction.reply({ | ||
ephemeral: true, | ||
embeds: [ | ||
new EmbedBuilder() | ||
.setTitle('note add - Error') | ||
.setDescription('You cannot repeat yourself.') | ||
.setColor('#fe3333'), | ||
], | ||
}); | ||
} | ||
|
||
await knex<UserNote>('userNotes').insert({ | ||
userId, | ||
content: interaction.options.getString('content')!, | ||
createdBy: interaction.user.id, | ||
}); | ||
|
||
return interaction.reply({ | ||
ephemeral: true, | ||
embeds: [ | ||
new EmbedBuilder() | ||
.setTitle('note add - Success') | ||
.setDescription('Note successfully created.') | ||
.setColor('#33fe33'), | ||
], | ||
}); | ||
}, | ||
}; | ||
|
||
export default noteCommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { RequestHandler } from 'express'; | ||
import type { Deps } from '..'; | ||
|
||
type PermissionRoles = 'Admin' | 'Moderator' | 'Responder'; | ||
|
||
export default function authorize({ discordClient }: Deps, roleIds: string[]): RequestHandler { | ||
return async (req, res, next) => { | ||
next(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export { default as authorize } from './authorize'; | ||
export { default as isAuthenticated } from './is-authenticated'; | ||
export { default as meUrlParam } from './me-url-param'; | ||
export { default as defaultErrorHandler } from './default-error-handler'; | ||
export { default as joiErrorHandler } from './joi-error-handler'; | ||
export { default as withCurrentUser } from './with-current-user'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { RequestHandler } from 'express'; | ||
import type { Deps } from '..'; | ||
|
||
export default function authorize(deps: Deps): RequestHandler { | ||
const { discordClient } = deps; | ||
|
||
return async (req, res, next) => { | ||
if (!req.session.userId) { | ||
throw new Error('authorize middleware should be used after isAuthenticated'); | ||
} | ||
Object.assign(res.locals, { | ||
user: await discordClient.users.fetch(req.session.userId), | ||
}); | ||
next(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.