Skip to content

Commit

Permalink
Merge pull request #143 from Carifio24/global-setup-teardown-students
Browse files Browse the repository at this point in the history
Use global setup/teardown of test database, add student endpoint tests
  • Loading branch information
Carifio24 authored Oct 1, 2024
2 parents 5f081d3 + cdef6ad commit f72a7d1
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 115 deletions.
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

0 comments on commit f72a7d1

Please sign in to comment.