Skip to content

Commit

Permalink
Merge pull request #90 from olaven/feature/test_fix_for_projects_test…
Browse files Browse the repository at this point in the history
…_card

Fixing test card
  • Loading branch information
olaven committed Mar 25, 2021
2 parents 0bd8898 + 0f91c93 commit 4afecb7
Show file tree
Hide file tree
Showing 22 changed files with 504 additions and 489 deletions.
4 changes: 3 additions & 1 deletion packages/api/globalConfig.json
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{"mongoUri":"mongodb://127.0.0.1:62752/e64cf421-d3ec-458c-86fa-c802e9186b31?"}
{
"mongoUri": "mongodb://127.0.0.1:62752/e64cf421-d3ec-458c-86fa-c802e9186b31?"
}
4 changes: 3 additions & 1 deletion packages/authentication/globalConfig.json
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{"mongoUri":"mongodb://127.0.0.1:62885/d1125fff-a42a-4f60-a9ee-4433eb22868e?"}
{
"mongoUri": "mongodb://127.0.0.1:62885/d1125fff-a42a-4f60-a9ee-4433eb22868e?"
}
280 changes: 135 additions & 145 deletions packages/authentication/src/routes/user-routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { app } from "../app";
import { hash } from "../cryptography/cryptography";
import { credentialsAreValid } from "./user-routes";


const signUp = (
credentials = test.mocks.credentials(),
agent = supertest.agent(app)
Expand All @@ -28,18 +27,14 @@ const extractBearerToken = async (test: supertest.Test) => {
} = await test;

return token;
};

}

const login = (credentials = test.mocks.credentials(), agent = supertest.agent(app)) =>
agent
.post("/users/sessions")
.send(credentials as any);

const login = (
credentials = test.mocks.credentials(),
agent = supertest.agent(app)
) => agent.post("/users/sessions").send(credentials as any);

describe("The authentication endpoint for users", () => {


describe("Local test utils", () => {
describe("extractBearerToken", () => {
it("Does extract something defined", async () => {
Expand Down Expand Up @@ -226,187 +221,182 @@ describe("The authentication endpoint for users", () => {
});

it("returns BAD_REQUEST on users that don't have valid email addresses", async () => {
const { status } = await signUp({
...test.mocks.credentials(),
email: "notemail.com",
});

const { status } = await signUp({
...test.mocks.credentials(),
email: "notemail.com"
});

expect(status).toEqual(BAD_REQUEST);
expect(status).toEqual(BAD_REQUEST);
});
});

describe("POST endpoint for creating new sessions",() => {

it("Does respond with 201 on succesful request", async () => {

const credentials = test.mocks.credentials();
await signUp(credentials);

const { status } = await login(credentials);
expect(status).toBe(CREATED);
});

it("Does respond with 401 if credentials are invalid", async () => {

const credentials = test.mocks.credentials();
//NOTE: not signing up
describe("POST endpoint for creating new sessions", () => {
it("Does respond with 201 on succesful request", async () => {
const credentials = test.mocks.credentials();
await signUp(credentials);

const { status } = await login(credentials);
expect(status).toBe(UNAUTHORIZED);
})
const { status } = await login(credentials);
expect(status).toBe(CREATED);
});

it("Does create a user, but does not store the password", async () => {
it("Does respond with 401 if credentials are invalid", async () => {
const credentials = test.mocks.credentials();
const { status } = await signUp(credentials);
expect(status).toEqual(CREATED);
//NOTE: not signing up

const user = await database.users.getByEmail(credentials.email);
expect(user.password_hash).not.toEqual(credentials.password);
const { status } = await login(credentials);
expect(status).toBe(UNAUTHORIZED);
});
});

it("Does create a user and stores hash comparable with bcrypt", async () => {
const credentials = test.mocks.credentials();
const { status } = await signUp(credentials);
expect(status).toEqual(CREATED);
it("Does create a user, but does not store the password", async () => {
const credentials = test.mocks.credentials();
const { status } = await signUp(credentials);
expect(status).toEqual(CREATED);

const user = await database.users.getByEmail(credentials.email);
expect(await hash.compare(credentials.password, user.password_hash)).toBe(
true
);
});
const user = await database.users.getByEmail(credentials.email);
expect(user.password_hash).not.toEqual(credentials.password);
});

it("Responds with CONFLICT if attempting to create the same user twice", async () => {
const credentials = test.mocks.credentials();
const firstResponse = await signUp(credentials);
expect(firstResponse.status).toEqual(CREATED);
it("Does create a user and stores hash comparable with bcrypt", async () => {
const credentials = test.mocks.credentials();
const { status } = await signUp(credentials);
expect(status).toEqual(CREATED);

const secondResponse = await signUp(credentials);
expect(secondResponse.status).toEqual(CONFLICT);
});
const user = await database.users.getByEmail(credentials.email);
expect(await hash.compare(credentials.password, user.password_hash)).toBe(
true
);
});

it("Returns BAD_REQUEST on users that have passwords shorter than 8 characters", async () => {
const { status } = await signUp({
...test.mocks.credentials(),
password: faker.random.alphaNumeric(7),
});
it("Responds with CONFLICT if attempting to create the same user twice", async () => {
const credentials = test.mocks.credentials();
const firstResponse = await signUp(credentials);
expect(firstResponse.status).toEqual(CREATED);

expect(status).toEqual(BAD_REQUEST);
const secondResponse = await signUp(credentials);
expect(secondResponse.status).toEqual(CONFLICT);
});

it("Returns BAD_REQUEST on users that have passwords shorter than 8 characters", async () => {
const { status } = await signUp({
...test.mocks.credentials(),
password: faker.random.alphaNumeric(7),
});

it("returns BAD_REQUEST on users that have passwords without lowercase letters", async () => {
const { status } = await signUp({
...test.mocks.credentials(),
password: faker.random.alphaNumeric(80).toLowerCase(),
});
expect(status).toEqual(BAD_REQUEST);
});

expect(status).toEqual(BAD_REQUEST);
it("returns BAD_REQUEST on users that have passwords without lowercase letters", async () => {
const { status } = await signUp({
...test.mocks.credentials(),
password: faker.random.alphaNumeric(80).toLowerCase(),
});

it("returns BAD_REQUEST on users that have passwords without uppercase letters", async () => {
const { status } = await signUp({
...test.mocks.credentials(),
password: faker.random.alphaNumeric(80).toUpperCase(),
});
expect(status).toEqual(BAD_REQUEST);
});

expect(status).toEqual(BAD_REQUEST);
it("returns BAD_REQUEST on users that have passwords without uppercase letters", async () => {
const { status } = await signUp({
...test.mocks.credentials(),
password: faker.random.alphaNumeric(80).toUpperCase(),
});

it("returns BAD_REQUEST on users that don't have numbers", async () => {
const { status } = await signUp({
...test.mocks.credentials(),
password: faker.random.alpha({ count: 50 }),
});
expect(status).toEqual(BAD_REQUEST);
});

expect(status).toEqual(BAD_REQUEST);
it("returns BAD_REQUEST on users that don't have numbers", async () => {
const { status } = await signUp({
...test.mocks.credentials(),
password: faker.random.alpha({ count: 50 }),
});

expect(status).toEqual(BAD_REQUEST);
});
});

describe("GET endpoint for retrieving information about the logged in user", () => {
const getMe = (token: string, agent = supertest(app)) =>
agent.get("/users/me").set("Authorization", "Bearer " + token);
describe("GET endpoint for retrieving information about the logged in user", () => {
const getMe = (token: string, agent = supertest(app)) =>
agent.get("/users/me").set("Authorization", "Bearer " + token);

it("Responds with UNAUTHORIZED if the user is not logged in", async () => {
const { status } = await getMe(null);
expect(status).toEqual(UNAUTHORIZED);
});
it("Responds with UNAUTHORIZED if the user is not logged in", async () => {
const { status } = await getMe(null);
expect(status).toEqual(UNAUTHORIZED);
});

it("Resopnds with FORBIDDEN if the JWT token is present, but not valid", async () => {
//a token from jwt.io, not encrypted with the same secret
const token =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
const { status } = await getMe(token);
expect(status).toEqual(FORBIDDEN);
});
it("Resopnds with FORBIDDEN if the JWT token is present, but not valid", async () => {
//a token from jwt.io, not encrypted with the same secret
const token =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
const { status } = await getMe(token);
expect(status).toEqual(FORBIDDEN);
});

it("Responds with OK if the user is logged in", async () => {
const { status } = await getMe(await extractBearerToken(signUp()));
it("Responds with OK if the user is logged in", async () => {
const { status } = await getMe(await extractBearerToken(signUp()));

expect(status).toEqual(OK);
});
expect(status).toEqual(OK);
});

it("Returns the user object if the user is logged in", async () => {
const token = await extractBearerToken(signUp());
const { body } = await getMe(token).expect(OK);
it("Returns the user object if the user is logged in", async () => {
const token = await extractBearerToken(signUp());
const { body } = await getMe(token).expect(OK);

expect(body.email).toBeDefined();
expect(body._id).toBeDefined();
});
expect(body.email).toBeDefined();
expect(body._id).toBeDefined();
});

it("Does not return the password hash to client", async () => {
expect(
(await getMe(await extractBearerToken(signUp()))).body.password_hash
).toBeUndefined();
});
it("Does not return the password hash to client", async () => {
expect(
(await getMe(await extractBearerToken(signUp()))).body.password_hash
).toBeUndefined();
});

it("Does return user data for the correct user", async () => {
const credentials = test.mocks.credentials();
it("Does return user data for the correct user", async () => {
const credentials = test.mocks.credentials();

const { body, status } = await getMe(
await extractBearerToken(signUp(credentials))
);
const { body, status } = await getMe(
await extractBearerToken(signUp(credentials))
);

expect(status).toEqual(OK);
expect(body.email).toEqual(credentials.email);
});
expect(status).toEqual(OK);
expect(body.email).toEqual(credentials.email);
});

it("Responds with FORBIDDEN if there is a token, but it's not properly formatted", async () => {
const { status } = await getMe("badly formatted token");
expect(status).toEqual(FORBIDDEN);
});
it("Responds with FORBIDDEN if there is a token, but it's not properly formatted", async () => {
const { status } = await getMe("badly formatted token");
expect(status).toEqual(FORBIDDEN);
});

it("Responds with OK if user is logged in", async () => {
const token = await extractBearerToken(signUp());
it("Responds with OK if user is logged in", async () => {
const token = await extractBearerToken(signUp());

const { status } = await getMe(token);
expect(status).toEqual(OK);
});
const { status } = await getMe(token);
expect(status).toEqual(OK);
});
});

describe("PUT endpont for token refresh", () => {
const refreshToken = (oldToken: string) =>
supertest(app)
.put("/users/sessions")
.set("Authorization", "Bearer " + oldToken);
describe("PUT endpont for token refresh", () => {
const refreshToken = (oldToken: string) =>
supertest(app)
.put("/users/sessions")
.set("Authorization", "Bearer " + oldToken);

it("Responds with OK on valid request", async () => {
const token = await extractBearerToken(signUp());
const { status } = await refreshToken(token);
it("Responds with OK on valid request", async () => {
const token = await extractBearerToken(signUp());
const { status } = await refreshToken(token);

expect(status).toEqual(OK);
});
expect(status).toEqual(OK);
});

it("Responds with a different token on valid request", async () => {
const oldToken = await extractBearerToken(signUp());
it("Responds with a different token on valid request", async () => {
const oldToken = await extractBearerToken(signUp());

await test.sleep(1200); //to update JWT `.iat`
const response = await refreshToken(oldToken);
const newToken = response.body.token;
await test.sleep(1200); //to update JWT `.iat`
const response = await refreshToken(oldToken);
const newToken = response.body.token;

expect(oldToken).toBeDefined();
expect(newToken).toBeDefined();
expect(oldToken).not.toEqual(newToken);
});
expect(oldToken).toBeDefined();
expect(newToken).toBeDefined();
expect(oldToken).not.toEqual(newToken);
});

});
Loading

0 comments on commit 4afecb7

Please sign in to comment.