-
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.
- Loading branch information
Showing
5 changed files
with
149 additions
and
37 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -3,6 +3,65 @@ import bcrypt from "bcrypt"; | |
import supertest from "supertest"; | ||
import { basePath, web } from "../src/application/web"; | ||
import logger from "../src/application/logger"; | ||
import { Contact } from "@prisma/client"; | ||
|
||
describe("GET /contact:id", () => { | ||
let token: string = ""; | ||
let createdContact: Contact = {} as Contact; | ||
|
||
beforeAll(async () => { | ||
token = await ContactTestUtil.getToken(); | ||
createdContact = await ContactTestUtil.createContact(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await ContactTestUtil.deleteContact(); | ||
await ContactTestUtil.deleteUser(); | ||
}); | ||
|
||
it ("should return 200 - success getting a contact", async () => { | ||
const res = await supertest(web) | ||
.get(`${basePath}/contact/${createdContact.id}`) | ||
.set('Authorization', `Bearer ${token}`); | ||
|
||
logger.info(res.body); | ||
expect(res.status).toBe(200); | ||
expect(res.body.data.id).toBe(createdContact.id); | ||
expect(res.body.data.first_name).toBe(createdContact.first_name); | ||
expect(res.body.data.last_name).toBe(createdContact.last_name); | ||
expect(res.body.data.email).toBe(createdContact.email); | ||
expect(res.body.data.phone).toBe(createdContact.phone); | ||
}); | ||
|
||
it("should return 404 - invalid contact id", async () => { | ||
const res = await supertest(web) | ||
.get(`${basePath}/contact/test123`) | ||
.set('Authorization', `Bearer ${token}`); | ||
|
||
logger.info(res.body); | ||
expect(res.status).toBe(404); | ||
expect(res.body.errors).toBeDefined(); | ||
}); | ||
|
||
it("should return 404 - contact id doesn't exists", async () => { | ||
const res = await supertest(web) | ||
.get(`${basePath}/contact/10000`) | ||
.set('Authorization', `Bearer ${token}`); | ||
|
||
logger.info(res.body); | ||
expect(res.status).toBe(404); | ||
expect(res.body.errors).toBeDefined(); | ||
}); | ||
|
||
it("should return 401 - empty authorization", async () => { | ||
const res = await supertest(web) | ||
.get(`${basePath}/contact/${createdContact.id}`); | ||
|
||
logger.info(res.body); | ||
expect(res.status).toBe(401); | ||
expect(res.body.errors).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe("POST /contact", () => { | ||
let token: string = ""; | ||
|
@@ -16,40 +75,33 @@ describe("POST /contact", () => { | |
await ContactTestUtil.deleteUser(); | ||
}) | ||
|
||
const newContact = { | ||
first_name: "First", | ||
last_name: "Last", | ||
email: "[email protected]", | ||
phone: "08123456789" | ||
} | ||
|
||
it("should return 200 - success creating contact", async () => { | ||
const res = await supertest(web) | ||
.post(`${basePath}/contact`) | ||
.send(newContact) | ||
.send(ContactTestUtil.contact) | ||
.set('Authorization', `Bearer ${token}`); | ||
|
||
logger.info(res.body); | ||
expect(res.status).toBe(200); | ||
expect(res.body.data.id).toBeGreaterThanOrEqual(1); | ||
expect(res.body.data.first_name).toBe(newContact.first_name); | ||
expect(res.body.data.last_name).toBe(newContact.last_name); | ||
expect(res.body.data.email).toBe(newContact.email); | ||
expect(res.body.data.phone).toBe(newContact.phone); | ||
expect(res.body.data.first_name).toBe(ContactTestUtil.contact.first_name); | ||
expect(res.body.data.last_name).toBe(ContactTestUtil.contact.last_name); | ||
expect(res.body.data.email).toBe(ContactTestUtil.contact.email); | ||
expect(res.body.data.phone).toBe(ContactTestUtil.contact.phone); | ||
}); | ||
|
||
it("should return 200 - success creating contact with emty last_name, email, and phone", async () => { | ||
const res = await supertest(web) | ||
.post(`${basePath}/contact`) | ||
.send({ | ||
first_name: newContact.first_name | ||
first_name: ContactTestUtil.contact.first_name | ||
}) | ||
.set('Authorization', `Bearer ${token}`); | ||
|
||
logger.info(res.body); | ||
expect(res.status).toBe(200); | ||
expect(res.body.data.id).toBeGreaterThanOrEqual(1); | ||
expect(res.body.data.first_name).toBe(newContact.first_name); | ||
expect(res.body.data.first_name).toBe(ContactTestUtil.contact.first_name); | ||
expect(res.body.data.last_name).toBe(""); | ||
expect(res.body.data.email).toBe(""); | ||
expect(res.body.data.phone).toBe(""); | ||
|
@@ -59,8 +111,8 @@ describe("POST /contact", () => { | |
const res = await supertest(web) | ||
.post(`${basePath}/contact`) | ||
.send({ | ||
first_name: newContact.first_name, | ||
last_name: newContact.last_name, | ||
first_name: ContactTestUtil.contact.first_name, | ||
last_name: ContactTestUtil.contact.last_name, | ||
email: "test", | ||
}) | ||
.set('Authorization', `Bearer ${token}`); | ||
|
@@ -74,8 +126,8 @@ describe("POST /contact", () => { | |
const res = await supertest(web) | ||
.post(`${basePath}/contact`) | ||
.send({ | ||
first_name: newContact.first_name, | ||
last_name: newContact.last_name, | ||
first_name: ContactTestUtil.contact.first_name, | ||
last_name: ContactTestUtil.contact.last_name, | ||
phone: "0123456789012345678901", | ||
}) | ||
.set('Authorization', `Bearer ${token}`); | ||
|
@@ -84,6 +136,18 @@ describe("POST /contact", () => { | |
expect(res.status).toBe(400); | ||
expect(res.body.errors.phone).toBeDefined(); | ||
}); | ||
|
||
it("should return 401 - empty authorization", async () => { | ||
const res = await supertest(web) | ||
.post(`${basePath}/contact`) | ||
.send({ | ||
first_name: ContactTestUtil.contact.first_name | ||
}); | ||
|
||
logger.info(res.body); | ||
expect(res.status).toBe(401); | ||
expect(res.body.errors).toBeDefined(); | ||
}); | ||
}) | ||
|
||
class ContactTestUtil { | ||
|
@@ -93,6 +157,13 @@ class ContactTestUtil { | |
password: "password" | ||
}; | ||
|
||
static contact = { | ||
first_name: "First", | ||
last_name: "Last", | ||
email: "[email protected]", | ||
phone: "08123456789" | ||
} | ||
|
||
static async create() { | ||
await prisma.user.create({ | ||
data: { | ||
|
@@ -120,6 +191,17 @@ class ContactTestUtil { | |
await prisma.user.deleteMany({}); | ||
} | ||
|
||
static async createContact(): Promise<Contact> { | ||
const contact = await prisma.contact.create({ | ||
data: { | ||
...ContactTestUtil.contact, | ||
username: ContactTestUtil.user.username | ||
} | ||
}); | ||
|
||
return contact as Contact; | ||
} | ||
|
||
static async deleteContact() { | ||
await prisma.contact.deleteMany({}); | ||
} | ||
|
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