2021-07-13 23:08:58 +12:00
|
|
|
// Mock data
|
|
|
|
|
2021-10-27 03:47:36 +13:00
|
|
|
require("../../../tests/utilities/dbConfig")
|
2021-07-15 23:55:35 +12:00
|
|
|
|
2022-04-20 06:42:52 +12:00
|
|
|
const { dangerousGetDB } = require("../../../db")
|
2021-07-13 23:08:58 +12:00
|
|
|
const { authenticateThirdParty } = require("../third-party-common")
|
2021-07-15 23:55:35 +12:00
|
|
|
const { data } = require("./utilities/mock-data")
|
|
|
|
|
|
|
|
const {
|
|
|
|
StaticDatabases,
|
|
|
|
generateGlobalUserID
|
|
|
|
} = require("../../../db/utils")
|
|
|
|
const { newid } = require("../../../hashing")
|
|
|
|
|
|
|
|
let db
|
|
|
|
|
|
|
|
const done = jest.fn()
|
|
|
|
|
|
|
|
const getErrorMessage = () => {
|
|
|
|
return done.mock.calls[0][2].message
|
|
|
|
}
|
2021-07-13 23:08:58 +12:00
|
|
|
|
2021-10-07 03:57:13 +13:00
|
|
|
const saveUser = async (user) => {
|
|
|
|
return await db.put(user)
|
|
|
|
}
|
|
|
|
|
2021-07-13 23:08:58 +12:00
|
|
|
describe("third party common", () => {
|
2021-07-15 23:55:35 +12:00
|
|
|
describe("authenticateThirdParty", () => {
|
|
|
|
let thirdPartyUser
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-04-20 06:42:52 +12:00
|
|
|
db = dangerousGetDB(StaticDatabases.GLOBAL.name)
|
2021-07-15 23:55:35 +12:00
|
|
|
thirdPartyUser = data.buildThirdPartyUser()
|
|
|
|
})
|
2021-07-13 23:08:58 +12:00
|
|
|
|
2021-07-15 23:55:35 +12:00
|
|
|
afterEach(async () => {
|
|
|
|
jest.clearAllMocks()
|
|
|
|
await db.destroy()
|
2021-07-13 23:08:58 +12:00
|
|
|
})
|
|
|
|
|
2021-07-15 23:55:35 +12:00
|
|
|
describe("validation", () => {
|
|
|
|
const testValidation = async (message) => {
|
2021-10-07 03:57:13 +13:00
|
|
|
await authenticateThirdParty(thirdPartyUser, false, done, saveUser)
|
2021-07-15 23:55:35 +12:00
|
|
|
expect(done.mock.calls.length).toBe(1)
|
|
|
|
expect(getErrorMessage()).toContain(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
it("provider fails", async () => {
|
|
|
|
delete thirdPartyUser.provider
|
|
|
|
testValidation("third party user provider required")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("user id fails", async () => {
|
|
|
|
delete thirdPartyUser.userId
|
|
|
|
testValidation("third party user id required")
|
|
|
|
})
|
|
|
|
|
|
|
|
it("email fails", async () => {
|
|
|
|
delete thirdPartyUser.email
|
|
|
|
testValidation("third party user email required")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
const expectUserIsAuthenticated = () => {
|
|
|
|
const user = done.mock.calls[0][1]
|
|
|
|
expect(user).toBeDefined()
|
|
|
|
expect(user._id).toBeDefined()
|
|
|
|
expect(user._rev).toBeDefined()
|
|
|
|
expect(user.token).toBeDefined()
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
|
|
|
const expectUserIsSynced = (user, thirdPartyUser) => {
|
|
|
|
expect(user.provider).toBe(thirdPartyUser.provider)
|
|
|
|
expect(user.firstName).toBe(thirdPartyUser.profile.name.givenName)
|
|
|
|
expect(user.lastName).toBe(thirdPartyUser.profile.name.familyName)
|
|
|
|
expect(user.thirdPartyProfile).toStrictEqual(thirdPartyUser.profile._json)
|
|
|
|
expect(user.oauth2).toStrictEqual(thirdPartyUser.oauth2)
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("when the user doesn't exist", () => {
|
|
|
|
describe("when a local account is required", () => {
|
|
|
|
it("returns an error message", async () => {
|
2021-10-07 03:57:13 +13:00
|
|
|
await authenticateThirdParty(thirdPartyUser, true, done, saveUser)
|
2021-07-15 23:55:35 +12:00
|
|
|
expect(done.mock.calls.length).toBe(1)
|
|
|
|
expect(getErrorMessage()).toContain("Email does not yet exist. You must set up your local budibase account first.")
|
|
|
|
})
|
|
|
|
})
|
2021-07-13 23:08:58 +12:00
|
|
|
|
2021-07-15 23:55:35 +12:00
|
|
|
describe("when a local account isn't required", () => {
|
|
|
|
it("creates and authenticates the user", async () => {
|
2021-10-07 03:57:13 +13:00
|
|
|
await authenticateThirdParty(thirdPartyUser, false, done, saveUser)
|
2021-07-15 23:55:35 +12:00
|
|
|
const user = expectUserIsAuthenticated()
|
|
|
|
expectUserIsSynced(user, thirdPartyUser)
|
|
|
|
expect(user.roles).toStrictEqual({})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("when the user exists", () => {
|
|
|
|
let dbUser
|
|
|
|
let id
|
|
|
|
let email
|
|
|
|
|
|
|
|
const createUser = async () => {
|
|
|
|
dbUser = {
|
|
|
|
_id: id,
|
|
|
|
email: email,
|
|
|
|
}
|
2021-09-22 03:07:04 +12:00
|
|
|
const response = await db.put(dbUser)
|
2021-07-15 23:55:35 +12:00
|
|
|
dbUser._rev = response.rev
|
|
|
|
}
|
|
|
|
|
|
|
|
const expectUserIsUpdated = (user) => {
|
|
|
|
// id is unchanged
|
|
|
|
expect(user._id).toBe(id)
|
|
|
|
// user is updated
|
|
|
|
expect(user._rev).not.toBe(dbUser._rev)
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("exists by email", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
id = generateGlobalUserID(newid()) // random id
|
|
|
|
email = thirdPartyUser.email // matching email
|
|
|
|
await createUser()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("syncs and authenticates the user", async () => {
|
2021-10-07 03:57:13 +13:00
|
|
|
await authenticateThirdParty(thirdPartyUser, true, done, saveUser)
|
2021-07-15 23:55:35 +12:00
|
|
|
|
|
|
|
const user = expectUserIsAuthenticated()
|
|
|
|
expectUserIsSynced(user, thirdPartyUser)
|
|
|
|
expectUserIsUpdated(user)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-10-27 00:40:30 +13:00
|
|
|
describe("exists by email with different casing", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
id = generateGlobalUserID(newid()) // random id
|
|
|
|
email = thirdPartyUser.email.toUpperCase() // matching email except for casing
|
|
|
|
await createUser()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("syncs and authenticates the user", async () => {
|
|
|
|
await authenticateThirdParty(thirdPartyUser, true, done, saveUser)
|
|
|
|
|
|
|
|
const user = expectUserIsAuthenticated()
|
|
|
|
expectUserIsSynced(user, thirdPartyUser)
|
|
|
|
expectUserIsUpdated(user)
|
|
|
|
expect(user.email).toBe(thirdPartyUser.email.toUpperCase())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2021-07-15 23:55:35 +12:00
|
|
|
describe("exists by id", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
id = generateGlobalUserID(thirdPartyUser.userId) // matching id
|
|
|
|
email = "test@test.com" // random email
|
|
|
|
await createUser()
|
|
|
|
})
|
|
|
|
|
|
|
|
it("syncs and authenticates the user", async () => {
|
2021-10-07 03:57:13 +13:00
|
|
|
await authenticateThirdParty(thirdPartyUser, true, done, saveUser)
|
2021-07-15 23:55:35 +12:00
|
|
|
|
|
|
|
const user = expectUserIsAuthenticated()
|
|
|
|
expectUserIsSynced(user, thirdPartyUser)
|
|
|
|
expectUserIsUpdated(user)
|
|
|
|
})
|
|
|
|
})
|
2021-07-13 23:08:58 +12:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|