func TestAuthCreateSession(t *testing.T) { auth, _ := buildAuth() token := auth.GenerateToken() user := model.NewUser(token) err := auth.CreateSession(user) test.AssertNotErr("Auth.CreateSession", err, t) }
func TestAuthEncodeDecodeUser(t *testing.T) { auth, _ := buildAuth() token := auth.GenerateToken() user := model.NewUser(token) encoded, err := auth.encodeUser(user) test.AssertNotErr("Auth.encodeUser", err, t) _, err2 := auth.decodeUser(encoded) test.AssertNotErr("Auth.edecodeUser", err2, t) }
func TestAuthGetSessionExpireSession(t *testing.T) { auth, _ := buildAuth() token := auth.GenerateToken() user := model.NewUser(token) err := auth.CreateSession(user) test.AssertNotErr("Auth.CreateSession", err, t) time.Sleep(time.Second) user, errGet := auth.GetSession(token) test.AssertNotErr("Auth.GetSession", errGet, t) test.AssertNil("Auth.GetSession", user, t) }
func TestAuthUpdateSession(t *testing.T) { auth, _ := buildAuth() token := auth.GenerateToken() user := model.NewUser(token) err := auth.CreateSession(user) test.AssertNotErr("Auth.CreateSession", err, t) user.ID = "updated" errUpd := auth.UpdateSession(user) usercheck, errGet := auth.GetSession(token) test.AssertNotErr("Auth.UpdateSession", errUpd, t) test.AssertNotErr("formatURL", errGet, t) test.Assert("Auth.UpdateSession", user.ID, usercheck.ID, t) }
func TestHandlerServeHTTP(t *testing.T) { ctx, auth, _ := buildContext() next := NewTestHandler() handler := RequireAuth(ctx, next) request, _ := http.NewRequest("GET", "/", nil) token := auth.GenerateToken() user := model.NewUser(token) auth.CreateSession(user) request.Header.Add("authToken", token) response := httptest.NewRecorder() handler.ServeHTTP(response, request) test.Assert("Handler.ServeHTTP", http.StatusOK, response.Code, t) if next.IsCalled != true { t.Error("Handler.ServeHTTP() didn't call the next handler on successful authentication.") } if ctx.User == nil { t.Error("Handler.ServeHTTP() didn't set the user to the context on successful authentication.") } }