(t *testing.T)
| 17 | ) |
| 18 | |
| 19 | func TestLoginLogout(t *testing.T) { |
| 20 | testDBConf := TestServiceMan.GetDBConfig() |
| 21 | conn := db.GetConnection(testDBConf) |
| 22 | identityRepo := identity.NewDBIdentityRepo(conn) |
| 23 | test_support.ResetDatabase(testDBConf) |
| 24 | contextUserManager := httpserver.NewContextUserManager("foo", identityRepo) |
| 25 | sessionPersister := persister.NewSessionPersister(TestServiceMan.GetRedisURI(), nil, nil, nil) |
| 26 | testIdentityManager := httpserver.IdentityManager{ |
| 27 | IdentityRepo: identityRepo, |
| 28 | ContextUserManager: contextUserManager, |
| 29 | Persister: sessionPersister, |
| 30 | } |
| 31 | orgUser1, err := testIdentityManager.IdentityRepo.CreateUserWithOrganization("test@home.arpa", "test", "test") |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | orgUser2, err := testIdentityManager.IdentityRepo.CreateUserWithOrganization( |
| 36 | "test2@home.arpa", "test2", "test2", |
| 37 | ) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | r := mux.NewRouter() |
| 42 | r.StrictSlash(true) |
| 43 | r.Use(sessionPersister.Middleware) |
| 44 | r.Use(httpserver.RWContextMiddleware) |
| 45 | r.Use(testIdentityManager.Middleware) |
| 46 | r.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) { |
| 47 | testIdentityManager.Authenticate(w, r, orgUser1.User.Id, orgUser1.Id) |
| 48 | w.Write([]byte("")) |
| 49 | }) |
| 50 | r.HandleFunc("/loginContext", func(w http.ResponseWriter, r *http.Request) { |
| 51 | testIdentityManager.AuthenticateContext(r.Context(), orgUser2.User.Id, orgUser2.Id) |
| 52 | w.Write([]byte("")) |
| 53 | }) |
| 54 | r.HandleFunc("/name", func(w http.ResponseWriter, r *http.Request) { |
| 55 | authenticationInfo := testIdentityManager.ContextUserManager.GetAuthenticationInfo(r.Context()) |
| 56 | user := authenticationInfo.AuthenticatedUser.User |
| 57 | if user == nil { |
| 58 | w.Write([]byte("NO USER")) |
| 59 | } else { |
| 60 | w.Write([]byte("test-" + user.Id)) |
| 61 | } |
| 62 | }) |
| 63 | |
| 64 | r.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) { |
| 65 | testIdentityManager.Logout(w, r) |
| 66 | w.Write([]byte("")) |
| 67 | }) |
| 68 | t.Run("can login and logout", func(t *testing.T) { |
| 69 | req := httptest.NewRequest(http.MethodGet, "/login", nil) |
| 70 | w := httptest.NewRecorder() |
| 71 | handler := r |
| 72 | handler.ServeHTTP(w, req) |
| 73 | res := w.Result() |
| 74 | req2 := httptest.NewRequest(http.MethodGet, "/name", nil) |
| 75 | cookie := res.Header.Get("Set-Cookie") |
| 76 | req2.Header.Set("Cookie", cookie) |
nothing calls this directly
no test coverage detected