Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use global setup/teardown of test database, add student endpoint tests #143

Merged
merged 12 commits into from
Oct 1, 2024
Merged
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ jobs:

- name: Test
# This is just the default settings for GH Actions MySQL
# Nothing secret here aside from the hashed API key
# Nothing secret here aside from the API key
env:
DB_TEST_HOSTNAME: 127.0.0.1
DB_TEST_USERNAME: root
DB_TEST_PASSWORD: root
HASHED_API_KEY: ${{ secrets.HASHED_API_KEY }}
CDS_API_KEY: TEST_API_KEY
run: npm run test
12 changes: 9 additions & 3 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ const config: Config = {
preset: "ts-jest",
testEnvironment: "node",
coveragePathIgnorePatterns: [
"/node_modules/",
"/dist",
]
"./node_modules/",
"./dist/",
],
testPathIgnorePatterns: [
"./node_modules/",
"./dist/",
],
globalSetup: "./tests/setup.ts",
globalTeardown: "./tests/teardown.ts"
};

export default config;
11 changes: 9 additions & 2 deletions src/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ const HASHER = new SHA3(256);

const validKeys = new Map<string, APIKey>();

export function hashAPIKey(key: string): string {
HASHER.reset();
HASHER.update(key);
const hashed = HASHER.digest("hex");
HASHER.reset();
return hashed;
}

export async function getAPIKey(key: string): Promise<APIKey | null> {
const cachedKey = validKeys.get(key);
if (cachedKey !== undefined) {
return cachedKey;
}
HASHER.update(key);
const hashedKey = HASHER.digest("hex");
const hashedKey = hashAPIKey(key);
const apiKey = await APIKey.findOne({ where: { hashed_key: hashedKey } });
HASHER.reset();
if (apiKey !== null) {
Expand Down
2 changes: 1 addition & 1 deletion src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export const SignUpStudentSchema = S.struct({
export type SignUpStudentOptions = S.Schema.To<typeof SignUpStudentSchema>;

export async function signUpStudent(options: SignUpStudentOptions): Promise<SignUpResult> {

const encryptedPassword = encryptPassword(options.password);

let validCode;
Expand Down
2 changes: 1 addition & 1 deletion src/models/student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export function initializeStudentModel(sequelize: Sequelize) {
},
{
unique: true,
fields: ["verificationCode"]
fields: ["verification_code"]
}
]
});
Expand Down
2 changes: 1 addition & 1 deletion src/request_results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export namespace SignUpResult {
export function statusCode(result: SignUpResult): number {
switch (result) {
case SignUpResult.Ok:
return 200;
return 201;
case SignUpResult.EmailExists:
return 409;
case SignUpResult.BadRequest:
Expand Down
Loading
Loading