示例#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)
}
示例#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)
}
示例#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)
}
示例#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)
}
示例#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.")
	}
}