authelia/internal/handlers/handler_state_test.go
Amir Zarrinkafsh 54694c4fca
[MISC] Ignore errcheck recommendations for legacy code (#893)
* [MISC] Ignore errcheck recommendations for legacy code
Some of this is likely intended to stay how it is, some could use refactoring, for now we will mark is and ignore it from the linter to be potentially addressed in the future.

* [MISC] Ensure files are gofmt-ed
2020-04-22 13:33:14 +10:00

89 lines
2.3 KiB
Go

package handlers
import (
"encoding/json"
"testing"
"github.com/authelia/authelia/internal/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/authelia/authelia/internal/authentication"
)
type StateGetSuite struct {
suite.Suite
mock *mocks.MockAutheliaCtx
}
func (s *StateGetSuite) SetupTest() {
s.mock = mocks.NewMockAutheliaCtx(s.T())
}
func (s *StateGetSuite) TearDownTest() {
s.mock.Close()
}
func (s *StateGetSuite) TestShouldReturnUsernameFromSession() {
userSession := s.mock.Ctx.GetSession()
userSession.Username = "username"
s.mock.Ctx.SaveSession(userSession) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
StateGet(s.mock.Ctx)
type Response struct {
Status string
Data StateResponse
}
expectedBody := Response{
Status: "OK",
Data: StateResponse{
Username: "username",
DefaultRedirectionURL: "",
AuthenticationLevel: authentication.NotAuthenticated,
},
}
actualBody := Response{}
json.Unmarshal(s.mock.Ctx.Response.Body(), &actualBody) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
assert.Equal(s.T(), 200, s.mock.Ctx.Response.StatusCode())
assert.Equal(s.T(), []byte("application/json"), s.mock.Ctx.Response.Header.ContentType())
assert.Equal(s.T(), expectedBody, actualBody)
}
func (s *StateGetSuite) TestShouldReturnAuthenticationLevelFromSession() {
userSession := s.mock.Ctx.GetSession()
userSession.AuthenticationLevel = authentication.OneFactor
s.mock.Ctx.SaveSession(userSession) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
StateGet(s.mock.Ctx)
type Response struct {
Status string
Data StateResponse
}
expectedBody := Response{
Status: "OK",
Data: StateResponse{
Username: "",
DefaultRedirectionURL: "",
AuthenticationLevel: authentication.OneFactor,
},
}
actualBody := Response{}
json.Unmarshal(s.mock.Ctx.Response.Body(), &actualBody) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
assert.Equal(s.T(), 200, s.mock.Ctx.Response.StatusCode())
assert.Equal(s.T(), []byte("application/json"), s.mock.Ctx.Response.Header.ContentType())
assert.Equal(s.T(), expectedBody, actualBody)
}
func TestRunStateGetSuite(t *testing.T) {
s := new(StateGetSuite)
suite.Run(t, s)
}