budibase/packages/server/middleware/routes/neo/tests/user.spec.js

80 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-04-09 17:53:48 +02:00
const couchdb = require("../../../../db");
const supertest = require("supertest");
const app = require("../../../../app");
2020-04-10 17:37:59 +02:00
const { createInstanceDatabase, insertDocument, destroyDatabase } = require("./couchTestUtils");
2020-04-09 17:53:48 +02:00
const TEST_INSTANCE_ID = "testing-123";
2020-04-10 17:37:59 +02:00
const TEST_USER = {
name: "Dave"
}
2020-04-09 17:53:48 +02:00
describe("/users", () => {
let request;
2020-04-10 17:37:59 +02:00
let server;
2020-04-09 17:53:48 +02:00
beforeAll(async () => {
2020-04-10 17:37:59 +02:00
server = await app({
2020-04-09 17:53:48 +02:00
config: {
port: 3000
}
});
request = supertest(server);
});
afterAll(async () => {
2020-04-10 17:37:59 +02:00
server.close();
2020-04-09 17:53:48 +02:00
})
2020-04-10 17:37:59 +02:00
describe("fetch", () => {
beforeEach(async () => {
await createInstanceDatabase(TEST_INSTANCE_ID);
await insertDocument(TEST_INSTANCE_ID, {
id: "cool-user-id",
type: "user",
name: "Dave"
})
});
afterEach(async () => {
await destroyDatabase(TEST_INSTANCE_ID);
});
it("returns a list of users from an instance db", done => {
request
.get(`/api/${TEST_INSTANCE_ID}/users`)
.set("Accept", "application/json")
.expect('Content-Type', /json/)
.expect(200)
.end(async (err, res) => {
const createdUser = res.body[0].doc;
expect(createdUser.name).toEqual(TEST_USER.name);
done();
});
})
});
2020-04-09 17:53:48 +02:00
describe("create", () => {
2020-04-10 17:37:59 +02:00
beforeEach(async () => {
await createInstanceDatabase(TEST_INSTANCE_ID);
});
afterEach(async () => {
await destroyDatabase(TEST_INSTANCE_ID);
});
2020-04-10 12:18:15 +02:00
it("returns a success message when a user is successfully created", done => {
2020-04-09 17:53:48 +02:00
request
2020-04-10 12:18:15 +02:00
.post(`/api/${TEST_INSTANCE_ID}/users`)
.send({ name: "John" })
2020-04-09 17:53:48 +02:00
.set("Accept", "application/json")
.expect('Content-Type', /json/)
.expect(200)
.end(async (err, res) => {
2020-04-10 17:37:59 +02:00
expect(res.body.message).toEqual("User created successfully.");
2020-04-09 17:53:48 +02:00
done();
});
})
});
});