Adding auth tests.

This commit is contained in:
mike12345567 2021-03-10 12:20:07 +00:00
parent 2b7f445787
commit 08d15f9d03
3 changed files with 127 additions and 1 deletions

View File

@ -46,6 +46,7 @@ exports.authenticate = async ctx => {
version: app.version, version: app.version,
} }
// if in cloud add the user api key, unless self hosted // if in cloud add the user api key, unless self hosted
/* istanbul ignore next */
if (env.CLOUD && !env.SELF_HOSTED) { if (env.CLOUD && !env.SELF_HOSTED) {
const { apiKey } = await getAPIKey(ctx.user.appId) const { apiKey } = await getAPIKey(ctx.user.appId)
payload.apiKey = apiKey payload.apiKey = apiKey
@ -70,6 +71,7 @@ exports.authenticate = async ctx => {
exports.fetchSelf = async ctx => { exports.fetchSelf = async ctx => {
const { userId, appId } = ctx.user const { userId, appId } = ctx.user
/* istanbul ignore next */
if (!userId || !appId) { if (!userId || !appId) {
ctx.body = {} ctx.body = {}
return return

View File

@ -0,0 +1,106 @@
const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
const setup = require("./utilities")
describe("/authenticate", () => {
let request = setup.getRequest()
let config = setup.getConfig()
afterAll(setup.afterAll)
beforeEach(async () => {
await config.init()
})
describe("authenticate", () => {
it("should be able to create a layout", async () => {
await config.createUser("test@test.com", "p4ssw0rd")
const res = await request
.post(`/api/authenticate`)
.send({
email: "test@test.com",
password: "p4ssw0rd",
})
.set(config.publicHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.token).toBeDefined()
expect(res.body.email).toEqual("test@test.com")
expect(res.body.password).toBeUndefined()
})
it("should error if no app specified", async () => {
await request
.post(`/api/authenticate`)
.expect(400)
})
it("should error if no email specified", async () => {
await request
.post(`/api/authenticate`)
.send({
password: "test",
})
.set(config.publicHeaders())
.expect(400)
})
it("should error if no password specified", async () => {
await request
.post(`/api/authenticate`)
.send({
email: "test",
})
.set(config.publicHeaders())
.expect(400)
})
it("should error if invalid user specified", async () => {
await request
.post(`/api/authenticate`)
.send({
email: "test",
password: "test",
})
.set(config.publicHeaders())
.expect(401)
})
it("should throw same error if wrong password specified", async () => {
await config.createUser("test@test.com", "password")
await request
.post(`/api/authenticate`)
.send({
email: "test@test.com",
password: "test",
})
.set(config.publicHeaders())
.expect(401)
})
it("should throw an error for inactive users", async () => {
await config.createUser("test@test.com", "password")
await config.makeUserInactive("test@test.com")
await request
.post(`/api/authenticate`)
.send({
email: "test@test.com",
password: "password",
})
.set(config.publicHeaders())
.expect(401)
})
})
describe("fetch self", () => {
it("should be able to delete the layout", async () => {
await config.createUser("test@test.com", "p4ssw0rd")
const headers = await config.login("test@test.com", "p4ssw0rd")
const res = await request
.get(`/api/self`)
.set(headers)
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.email).toEqual("test@test.com")
})
})
})

View File

@ -241,7 +241,7 @@ class TestConfiguration {
async createUser( async createUser(
email = EMAIL, email = EMAIL,
password = PASSWORD, password = PASSWORD,
roleId = BUILTIN_ROLE_IDS.POWER roleId = BUILTIN_ROLE_IDS.POWER,
) { ) {
return this._req( return this._req(
{ {
@ -254,6 +254,24 @@ class TestConfiguration {
) )
} }
async makeUserInactive(email) {
const user = await this._req(
null,
{
email,
},
controllers.user.find
)
return this._req(
{
...user,
status: "inactive",
},
null,
controllers.user.update
)
}
async login(email, password) { async login(email, password) {
if (!email || !password) { if (!email || !password) {
await this.createUser() await this.createUser()