Created
December 10, 2020 22:52
-
-
Save antydemant/45d5fd8c93f2e03fbf6ddfe7dc220a8a to your computer and use it in GitHub Desktop.
Deno Tests
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
import { assertEquals, superoak, bcrypt } from "../deps.js"; | |
import { getNumberOfWeek } from "../utils/dates.js"; | |
import { isHashSameWith, isUserExist } from "../utils/validation-rules.js"; | |
import { getUser } from "../services/userService.js"; | |
import { executeQuery } from "../database/database.js"; | |
import app from "../app.js"; | |
Deno.test({ | |
name: "Should GET request to / return status code 200", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.get("/").expect(200); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should GET request to /api/summary return status code 200", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.get("/api/summary").expect(200); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should GET request to /api/summary/:year/:month/:day return status code 200", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.get("/api/summary/2020/12/12").expect(200); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should GET request to /api/summary/:year/:month/:day return status code 200", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.get("/api/summary/1988/12/12").expect(200).expect({ | |
message: { | |
average_generic_mood: null, | |
average_sleep_duration: null, | |
average_sleep_quality: null, | |
average_sports_time: null, | |
average_studies_time: null, | |
average_eating_quality: null, | |
}, | |
}); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should GET request to /auth/login return status code 200", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.get("/auth/login").expect(200); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should GET request to /auth/registration return status code 200", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.get("/auth/registration").expect(200); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should GET request to /auth/summary return status code 200", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.get("/auth/logout").expect(200); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should GET request to /route/not/found return status code 404", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.get("/route/not/found").expect(404); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should POST request to /auth/login send invalid email and empty password and return validation messages", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.post("/auth/login") | |
.send("email=blabla@com") | |
.expect(/password is invalid/) | |
.expect(/Email is invalid or doesn't exist/); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should POST request to /auth/login send valid email and password and return message with non-existent user", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.post("/auth/login") | |
.send("[email protected]&password=blabla") | |
.expect(/Email is invalid or doesn't exist/); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should POST request to /auth/registration send invalid email and empty password and return validation messages", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.post("/auth/registration") | |
.send("email=blabla") | |
.expect(/password is invalid/) | |
.expect(/Email is invalid or user already exists!/); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should GET request to /behavior/reporting return status code 200", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.get("/behavior/reporting").expect(200); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should GET request to /behavior/reporting/morning return status code 200", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.get("/behavior/reporting/morning").expect(200); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should GET request to /behavior/reporting/evening return status code 200", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.get("/behavior/reporting/evening").expect(200); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should GET request to /behavior/summary return status code 200", | |
fn: async () => { | |
const testClient = await superoak(app); | |
await testClient.get("/behavior/summary").expect(200); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should return number of week from full date", | |
fn: async () => { | |
const date = new Date("2020/12/12"); | |
const numberOfWeek = getNumberOfWeek(date); | |
assertEquals(numberOfWeek, 50); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should return valid user by email", | |
fn: async () => { | |
const email = "[email protected]"; | |
const user = await getUser(email) | |
assertEquals(user.email, email); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should not return user by email if not exist", | |
fn: async () => { | |
const email = `alohomora${Math.random()}@alohomora.com`; | |
const user = await getUser(email) | |
assertEquals(user, null); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should return object if isUserExist function match invalid case", | |
fn: async () => { | |
const email = `alohomora${Math.random()}@alohomora.com`; | |
const returnedValue = await isUserExist(email) | |
assertEquals(returnedValue, { | |
implicit: false, | |
params: { | |
value: email, | |
}, | |
rule: "isUserExist", | |
}); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); | |
Deno.test({ | |
name: "Should return metadata object on executeQuery function call with valid query param", | |
fn: async () => { | |
const returnedValue = await executeQuery("SELECT 'Hello World';") | |
assertEquals(returnedValue.rowCount, 1); | |
assertEquals(returnedValue.command, "SELECT"); | |
assertEquals(returnedValue.rows,[["Hello World"]]); | |
}, | |
sanitizeResources: false, | |
sanitizeOps: false | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment