-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from alexandergoncharov/develop
Add tests
- Loading branch information
Showing
16 changed files
with
2,200 additions
and
103 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,12 +1,15 @@ | ||
export const validationErrorMessage: string = "Wrong validation token"; | ||
export const unknowError: string = "Unknow Error"; | ||
export const wrongEmailOrPassword: string = "Wrong Email or Password"; | ||
export const VALIDATION_ERROR: string = "Wrong validation token"; | ||
export const UNKNOW_ERROR: string = "Unknow Error"; | ||
export const WRONG_EMAIL_OR_PASSWORD: string = "Wrong Email or Password"; | ||
export const DUPLICATED_EMAIL_ERROR: string = "Email already exist"; | ||
export const INFO_MESSAGE = "Some information about the <b>company</b>."; | ||
|
||
export const delayMs: number = 5000; | ||
export const DELAY_IN_MS: number = 5000; | ||
|
||
export enum StatusCode { | ||
Successful = 200, | ||
BadRequest = 400, | ||
Unauthorized = 401, | ||
NotFound = 404, | ||
Duplicated = 409, | ||
} |
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,60 @@ | ||
import chai from "chai"; | ||
import supertest from "supertest"; | ||
import { server } from "../src/index"; | ||
import { User } from "../src/models"; | ||
import { deleteToken } from "../src/repositories/token"; | ||
import { createAuthor, deleteAuthor, getAllAuthors } from "./dbUtils/author"; | ||
import { createUser, createUserToken, deleteUser } from "./dbUtils/user"; | ||
|
||
const app = supertest(server); | ||
const { expect } = chai; | ||
|
||
describe("Author api", () => { | ||
describe("for non authorized users", async () => { | ||
it("/author should return 401 if token missed", async () => { | ||
await app.get("/author").expect(401); | ||
}); | ||
|
||
it("/author should return 401 if token is invalid", async () => { | ||
await app.get("/author?token=invalidToken").expect(401); | ||
}); | ||
}); | ||
|
||
describe("for authorized users", () => { | ||
let user: User; | ||
let token: string; | ||
|
||
before(async () => { | ||
user = await createUser(); | ||
token = await createUserToken(user); | ||
}); | ||
|
||
after(async () => { | ||
await deleteToken(token); | ||
await deleteUser(user); | ||
}); | ||
|
||
it("/author should return random author", async () => { | ||
const author1 = await createAuthor(); | ||
const author2 = await createAuthor(); | ||
|
||
try { | ||
const response = await app.get(`/author?token=${token}`).expect(200); | ||
const responseAuthor = response.body; | ||
|
||
const allAuthors = await getAllAuthors(); | ||
|
||
const isAuthorExists = allAuthors.some( | ||
(author) => | ||
author.id === responseAuthor.authorId && | ||
author.name === responseAuthor.name | ||
); | ||
|
||
expect(isAuthorExists).to.be.true; | ||
} finally { | ||
await deleteAuthor(author1); | ||
await deleteAuthor(author2); | ||
} | ||
}).timeout(10000); | ||
}); | ||
}); |
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,28 @@ | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { appDataSource } from "../../src"; | ||
import { Author } from "../../src/models"; | ||
|
||
export const getAllAuthors = (): Promise<Author[]> => { | ||
const authorRepository = appDataSource.getRepository(Author); | ||
return authorRepository.find(); | ||
}; | ||
|
||
export const createAuthor = async (): Promise<Author> => { | ||
const authorRepository = appDataSource.getRepository(Author); | ||
const authorParams = { | ||
name: "testName" + uuidv4(), // For uniq name | ||
}; | ||
|
||
await authorRepository.insert(authorParams); | ||
|
||
const author: Author | null = await authorRepository.findOne({ | ||
where: { name: authorParams.name }, | ||
}); | ||
|
||
return author!; | ||
}; | ||
|
||
export const deleteAuthor = async (author: Author): Promise<void> => { | ||
const authorRepository = appDataSource.getRepository(Author); | ||
await authorRepository.delete(author); | ||
}; |
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,23 @@ | ||
import { v4 as uuidv4 } from "uuid"; | ||
import { appDataSource } from "../../src"; | ||
import { Author, Quote } from "../../src/models"; | ||
|
||
export const createQuote = async (author: Author): Promise<Quote> => { | ||
const quoteRepository = appDataSource.getRepository(Quote); | ||
const quoteText = "testQuote" + uuidv4(); // For uniq quote | ||
|
||
await quoteRepository.insert({ quote: quoteText, author }); | ||
|
||
const quote = await quoteRepository.findOne({ | ||
relations: ["author"], | ||
where: { quote: quoteText }, | ||
}); | ||
|
||
return quote!; | ||
}; | ||
|
||
export const deleteQuote = async (quote: Quote) => { | ||
const quoteRepository = appDataSource.getRepository(Quote); | ||
|
||
await quoteRepository.delete(quote); | ||
}; |
Oops, something went wrong.