1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161import type { Payload } from 'payload'
import path from 'path'
import { fileURLToPath } from 'url'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { devUser } from '../credentials.js'
import { initPayloadInt } from '../helpers/initPayloadInt.js'
let payload: Payload
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
describe('Login With Username Feature', () => {
beforeAll(async () => {
;({ payload } = await initPayloadInt(dirname))
})
afterAll(async () => {
await payload.destroy()
})
it('should not allow creation with neither email nor username', async () => {
let errors = []
try {
await payload.create({
collection: 'login-with-either',
data: {
email: null,
username: null,
},
})
} catch (error) {
errors = error.data.errors
}
expect(errors).toHaveLength(2)
})
it('should not allow removing both username and email fields', async () => {
const emailToUse = 'example@email.com'
const usernameToUse = 'exampleUser'
const exampleUser = await payload.create({
collection: 'login-with-either',
data: {
email: emailToUse,
username: usernameToUse,
password: 'test',
},
})
let errors = []
try {
await payload.update({
collection: 'login-with-either',
id: exampleUser.id,
data: {
email: null,
username: null,
},
})
} catch (error) {
errors = error.data.errors
}
expect(errors).toHaveLength(2)
errors = []
await payload.update({
collection: 'login-with-either',
id: exampleUser.id,
data: {
username: null,
},
})
expect(errors).toHaveLength(0)
try {
await payload.update({
collection: 'login-with-either',
id: exampleUser.id,
data: {
email: null,
},
})
} catch (error) {
errors = error.data.errors
}
expect(errors).toHaveLength(2)
})
it('should allow login with either username or email', async () => {
await payload.create({
collection: 'login-with-either',
data: {
email: devUser.email,
username: 'dev',
password: devUser.password,
},
})
const loginWithEmail = await payload.login({
collection: 'login-with-either',
data: {
email: devUser.email,
password: devUser.password,
},
})
expect(loginWithEmail).toHaveProperty('token')
const loginWithUsername = await payload.login({
collection: 'login-with-either',
data: {
username: 'dev',
password: devUser.password,
},
})
expect(loginWithUsername).toHaveProperty('token')
})
it('should allow mutliple creates with optional email and username', async () => {
// create a user with just email
await payload.create({
collection: 'login-with-either',
data: {
email: 'email1@mail.com',
password: 'test',
},
})
// create second user with just email
const emailUser2 = await payload.create({
collection: 'login-with-either',
data: {
email: 'email2@mail.com',
password: 'test',
},
})
expect(emailUser2).toHaveProperty('id')
// create user with just username
await payload.create({
collection: 'login-with-either',
data: {
username: 'username1',
password: 'test',
},
})
// create second user with just username
const usernameUser2 = await payload.create({
collection: 'login-with-either',
data: {
username: 'username2',
password: 'test',
},
})
expect(usernameUser2).toHaveProperty('id')
})
})