-
Notifications
You must be signed in to change notification settings - Fork 2
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
4 changed files
with
167 additions
and
0 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
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 app from "@src/app"; | ||
import { tryber } from "@src/features/database"; | ||
import request from "supertest"; | ||
|
||
describe("POST /customers", () => { | ||
afterEach(async () => { | ||
await tryber.tables.WpAppqCustomer.do().delete(); | ||
}); | ||
|
||
it("Should answer 403 if not logged in", () => { | ||
return request(app) | ||
.post("/customers") | ||
.send({ name: "New project" }) | ||
.expect(403); | ||
}); | ||
it("Should answer 403 if logged in without permissions", async () => { | ||
const response = await request(app) | ||
.post("/customers") | ||
.send({ name: "New project" }) | ||
.set("Authorization", "Bearer tester"); | ||
expect(response.status).toBe(403); | ||
}); | ||
it("Should answer 201 if logged as user with full access on campaigns", async () => { | ||
const response = await request(app) | ||
.post("/customers") | ||
.send({ name: "New project" }) | ||
.set("Authorization", 'Bearer tester olp {"appq_campaign":true}'); | ||
expect(response.status).toBe(201); | ||
}); | ||
it("Should answer 403 if logged as user with access to some campaigns", async () => { | ||
const response = await request(app) | ||
.post("/customers") | ||
.send({ name: "New project" }) | ||
.set("Authorization", 'Bearer tester olp {"appq_campaign":[1,2]}'); | ||
expect(response.status).toBe(403); | ||
}); | ||
|
||
it("Should add customer", async () => { | ||
const postResponse = await request(app) | ||
.post("/customers") | ||
.send({ name: "New project" }) | ||
.set("Authorization", "Bearer admin"); | ||
|
||
expect(postResponse.status).toBe(201); | ||
expect(postResponse.body).toHaveProperty("id"); | ||
expect(postResponse.body).toHaveProperty("name"); | ||
const { id, name } = postResponse.body; | ||
|
||
const getResponse = await request(app) | ||
.get("/customers") | ||
.set("Authorization", "Bearer admin"); | ||
|
||
expect(getResponse.status).toBe(200); | ||
|
||
const customers = getResponse.body; | ||
expect(customers).toHaveLength(1); | ||
expect(customers[0].id).toBe(id); | ||
expect(customers[0].name).toBe(name); | ||
}); | ||
}); |
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,49 @@ | ||
/** OPENAPI-CLASS : post-customers */ | ||
|
||
import OpenapiError from "@src/features/OpenapiError"; | ||
import { tryber } from "@src/features/database"; | ||
import UserRoute from "@src/features/routes/UserRoute"; | ||
|
||
class RouteItem extends UserRoute<{ | ||
response: StoplightOperations["post-customers"]["responses"]["200"]["content"]["application/json"]; | ||
body: StoplightOperations["post-customers"]["requestBody"]["content"]["application/json"]; | ||
}> { | ||
private accessibleCampaigns: true | number[] = this.campaignOlps | ||
? this.campaignOlps | ||
: []; | ||
|
||
protected async filter() { | ||
if ((await super.filter()) === false) return false; | ||
if (this.doesNotHaveAccessToCampaigns()) { | ||
this.setError(403, new OpenapiError("You are not authorized to do this")); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private doesNotHaveAccessToCampaigns() { | ||
return this.accessibleCampaigns !== true; | ||
} | ||
|
||
protected async prepare(): Promise<void> { | ||
const customer = await this.createCustomer(); | ||
return this.setSuccess(201, customer); | ||
} | ||
|
||
private async createCustomer() { | ||
const customer = await tryber.tables.WpAppqCustomer.do() | ||
.insert({ | ||
company: this.getBody().name, | ||
pm_id: 0, | ||
}) | ||
.returning("id"); | ||
const id = customer[0].id ?? customer[0]; | ||
|
||
return { | ||
id: id, | ||
name: this.getBody().name, | ||
}; | ||
} | ||
} | ||
|
||
export default RouteItem; |
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