Beispiel #1
0
func TestAuthCreateSession(t *testing.T) {
	auth, _ := buildAuth()
	token := auth.GenerateToken()
	user := model.NewUser(token)
	err := auth.CreateSession(user)

	test.AssertNotErr("Auth.CreateSession", err, t)
}
Beispiel #2
0
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)
}
Beispiel #3
0
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)
}
Beispiel #4
0
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)
}
Beispiel #5
0
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.")
	}
}