2019-04-25 04:52:08 +07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2019-12-24 09:14:52 +07:00
|
|
|
"github.com/authelia/authelia/internal/mocks"
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2020-04-05 19:37:21 +07:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/authentication"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
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"
|
2020-04-22 10:33:14 +07:00
|
|
|
s.mock.Ctx.SaveSession(userSession) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
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{}
|
|
|
|
|
2020-04-22 10:33:14 +07:00
|
|
|
json.Unmarshal(s.mock.Ctx.Response.Body(), &actualBody) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
2019-04-25 04:52:08 +07:00
|
|
|
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
|
2020-04-22 10:33:14 +07:00
|
|
|
s.mock.Ctx.SaveSession(userSession) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
StateGet(s.mock.Ctx)
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
Status string
|
|
|
|
Data StateResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedBody := Response{
|
|
|
|
Status: "OK",
|
|
|
|
Data: StateResponse{
|
|
|
|
Username: "",
|
|
|
|
DefaultRedirectionURL: "",
|
|
|
|
AuthenticationLevel: authentication.OneFactor,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actualBody := Response{}
|
|
|
|
|
2020-04-22 10:33:14 +07:00
|
|
|
json.Unmarshal(s.mock.Ctx.Response.Body(), &actualBody) //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
2019-04-25 04:52:08 +07:00
|
|
|
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)
|
|
|
|
}
|