MCPcopy Index your code
hub / github.com/coder/coder / TestPostLogin

Function TestPostLogin

coderd/users_test.go:191–431  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

189}
190
191func TestPostLogin(t *testing.T) {
192 t.Parallel()
193 t.Run("InvalidUser", func(t *testing.T) {
194 t.Parallel()
195 client := coderdtest.New(t, nil)
196 ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
197 defer cancel()
198
199 _, err := client.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{
200 Email: "my@email.org",
201 Password: "password",
202 })
203 var apiErr *codersdk.Error
204 require.ErrorAs(t, err, &apiErr)
205 require.Equal(t, http.StatusUnauthorized, apiErr.StatusCode())
206 })
207
208 t.Run("BadPassword", func(t *testing.T) {
209 t.Parallel()
210 auditor := audit.NewMock()
211 client := coderdtest.New(t, &coderdtest.Options{Auditor: auditor})
212 numLogs := len(auditor.AuditLogs())
213
214 ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
215 defer cancel()
216
217 req := codersdk.CreateFirstUserRequest{
218 Email: "testuser@coder.com",
219 Username: "testuser",
220 Password: "SomeSecurePassword!",
221 }
222 _, err := client.CreateFirstUser(ctx, req)
223 require.NoError(t, err)
224 _, err = client.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{
225 Email: req.Email,
226 Password: "badpass",
227 })
228 numLogs++ // add an audit log for login
229 var apiErr *codersdk.Error
230 require.ErrorAs(t, err, &apiErr)
231 require.Equal(t, http.StatusUnauthorized, apiErr.StatusCode())
232
233 require.Len(t, auditor.AuditLogs(), numLogs)
234 require.Equal(t, database.AuditActionLogin, auditor.AuditLogs()[numLogs-1].Action)
235 })
236
237 // "hunter2" was the input of the previous hardcoded simulated hash, which
238 // an empty stored hash wrongly matched; this is a regression test.
239 t.Run("NonexistentUser401", func(t *testing.T) {
240 t.Parallel()
241 client := coderdtest.New(t, nil)
242 ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
243 defer cancel()
244
245 _, err := client.LoginWithPassword(ctx, codersdk.LoginWithPasswordRequest{
246 Email: "does-not-exist@coder.com",
247 Password: "hunter2",
248 })

Callers

nothing calls this directly

Calls 15

StatusCodeMethod · 0.95
NewFunction · 0.92
NewMockFunction · 0.92
NewWithDatabaseFunction · 0.92
UserFunction · 0.92
AsSystemRestrictedFunction · 0.92
NewFunction · 0.92
CreateFirstUserFunction · 0.92
CreateAnotherUserFunction · 0.92
DeploymentValuesFunction · 0.92
NowFunction · 0.92
LoginWithPasswordMethod · 0.80

Tested by

no test coverage detected