Basic update test
This commit is contained in:
parent
4ddd450a89
commit
88e054c366
|
@ -32,55 +32,97 @@ describe("UserDB", () => {
|
||||||
db.init(quotas, groups, features)
|
db.init(quotas, groups, features)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("creating a new user will persist it", async () => {
|
describe("save", () => {
|
||||||
const email = generator.email({})
|
describe("create", () => {
|
||||||
const user: User = structures.users.user({
|
it("creating a new user will persist it", async () => {
|
||||||
email,
|
const email = generator.email({})
|
||||||
tenantId: config.getTenantId(),
|
const user: User = structures.users.user({
|
||||||
|
email,
|
||||||
|
tenantId: config.getTenantId(),
|
||||||
|
})
|
||||||
|
|
||||||
|
await config.doInTenant(async () => {
|
||||||
|
const saveUserResponse = await db.save(user)
|
||||||
|
|
||||||
|
const persistedUser = await db.getUserByEmail(email)
|
||||||
|
expect(persistedUser).toEqual({
|
||||||
|
...user,
|
||||||
|
_id: saveUserResponse._id,
|
||||||
|
_rev: expect.stringMatching(/^1-\w+/),
|
||||||
|
password: expect.not.stringMatching(user.password!),
|
||||||
|
status: UserStatus.ACTIVE,
|
||||||
|
createdAt: Date.now(),
|
||||||
|
updatedAt: new Date().toISOString(),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it("the same email cannot be used twice in the same tenant", async () => {
|
||||||
|
const email = generator.email({})
|
||||||
|
const user: User = structures.users.user({
|
||||||
|
email,
|
||||||
|
tenantId: config.getTenantId(),
|
||||||
|
})
|
||||||
|
|
||||||
|
await config.doInTenant(() => db.save(user))
|
||||||
|
|
||||||
|
await config.doInTenant(() =>
|
||||||
|
expect(db.save(user)).rejects.toThrow(
|
||||||
|
`Email already in use: '${email}'`
|
||||||
|
)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("the same email cannot be used twice in different tenants", async () => {
|
||||||
|
const email = generator.email({})
|
||||||
|
const user: User = structures.users.user({
|
||||||
|
email,
|
||||||
|
tenantId: config.getTenantId(),
|
||||||
|
})
|
||||||
|
|
||||||
|
await config.doInTenant(() => db.save(user))
|
||||||
|
|
||||||
|
config.newTenant()
|
||||||
|
await config.doInTenant(() =>
|
||||||
|
expect(db.save(user)).rejects.toThrow(
|
||||||
|
`Email already in use: '${email}'`
|
||||||
|
)
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
await config.doInTenant(async () => {
|
describe("update", () => {
|
||||||
const saveUserResponse = await db.save(user)
|
let user: User
|
||||||
|
|
||||||
const persistedUser = await db.getUserByEmail(email)
|
beforeEach(async () => {
|
||||||
expect(persistedUser).toEqual({
|
user = await config.doInTenant(() =>
|
||||||
...user,
|
db.save(
|
||||||
_id: saveUserResponse._id,
|
structures.users.user({
|
||||||
_rev: expect.stringMatching(/^1-\w+/),
|
email: generator.email({}),
|
||||||
password: expect.not.stringMatching(user.password!),
|
tenantId: config.getTenantId(),
|
||||||
status: UserStatus.ACTIVE,
|
})
|
||||||
createdAt: Date.now(),
|
)
|
||||||
updatedAt: new Date().toISOString(),
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("can update user properties", async () => {
|
||||||
|
await config.doInTenant(async () => {
|
||||||
|
const updatedName = generator.first()
|
||||||
|
user.firstName = updatedName
|
||||||
|
|
||||||
|
await db.save(user)
|
||||||
|
|
||||||
|
const persistedUser = await db.getUserByEmail(user.email)
|
||||||
|
expect(persistedUser).toEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
_id: user._id,
|
||||||
|
email: user.email,
|
||||||
|
firstName: updatedName,
|
||||||
|
lastName: user.lastName,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it("the same email cannot be used twice in the same tenant", async () => {
|
|
||||||
const email = generator.email({})
|
|
||||||
const user: User = structures.users.user({
|
|
||||||
email,
|
|
||||||
tenantId: config.getTenantId(),
|
|
||||||
})
|
|
||||||
|
|
||||||
await config.doInTenant(() => db.save(user))
|
|
||||||
|
|
||||||
await config.doInTenant(() =>
|
|
||||||
expect(db.save(user)).rejects.toThrow(`Email already in use: '${email}'`)
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("the same email cannot be used twice in different tenants", async () => {
|
|
||||||
const email = generator.email({})
|
|
||||||
const user: User = structures.users.user({
|
|
||||||
email,
|
|
||||||
tenantId: config.getTenantId(),
|
|
||||||
})
|
|
||||||
|
|
||||||
await config.doInTenant(() => db.save(user))
|
|
||||||
|
|
||||||
config.newTenant()
|
|
||||||
await config.doInTenant(() =>
|
|
||||||
expect(db.save(user)).rejects.toThrow(`Email already in use: '${email}'`)
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue