2019-04-25 04:52:08 +07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-16 08:47:31 +07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-04-25 04:52:08 +07:00
|
|
|
"github.com/stretchr/testify/suite"
|
2020-04-05 19:37:21 +07:00
|
|
|
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/authentication"
|
|
|
|
"github.com/authelia/authelia/v4/internal/mocks"
|
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-12-16 08:47:31 +07:00
|
|
|
err := s.mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(s.T(), err)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
StateGET(s.mock.Ctx)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
Status string
|
|
|
|
Data StateResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedBody := Response{
|
|
|
|
Status: "OK",
|
|
|
|
Data: StateResponse{
|
|
|
|
Username: "username",
|
|
|
|
DefaultRedirectionURL: "",
|
|
|
|
AuthenticationLevel: authentication.NotAuthenticated,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actualBody := Response{}
|
|
|
|
|
2020-12-16 08:47:31 +07:00
|
|
|
err = json.Unmarshal(s.mock.Ctx.Response.Body(), &actualBody)
|
|
|
|
require.NoError(s.T(), err)
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.Equal(s.T(), 200, s.mock.Ctx.Response.StatusCode())
|
2022-07-08 19:18:52 +07:00
|
|
|
assert.Equal(s.T(), []byte("application/json; charset=utf-8"), s.mock.Ctx.Response.Header.ContentType())
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.Equal(s.T(), expectedBody, actualBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StateGetSuite) TestShouldReturnAuthenticationLevelFromSession() {
|
|
|
|
userSession := s.mock.Ctx.GetSession()
|
|
|
|
userSession.AuthenticationLevel = authentication.OneFactor
|
2020-12-16 08:47:31 +07:00
|
|
|
err := s.mock.Ctx.SaveSession(userSession)
|
|
|
|
require.NoError(s.T(), err)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2022-04-08 11:13:47 +07:00
|
|
|
StateGET(s.mock.Ctx)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
Status string
|
|
|
|
Data StateResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedBody := Response{
|
|
|
|
Status: "OK",
|
|
|
|
Data: StateResponse{
|
|
|
|
Username: "",
|
|
|
|
DefaultRedirectionURL: "",
|
|
|
|
AuthenticationLevel: authentication.OneFactor,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actualBody := Response{}
|
|
|
|
|
2020-12-16 08:47:31 +07:00
|
|
|
err = json.Unmarshal(s.mock.Ctx.Response.Body(), &actualBody)
|
|
|
|
require.NoError(s.T(), err)
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.Equal(s.T(), 200, s.mock.Ctx.Response.StatusCode())
|
2022-07-08 19:18:52 +07:00
|
|
|
assert.Equal(s.T(), []byte("application/json; charset=utf-8"), s.mock.Ctx.Response.Header.ContentType())
|
2019-04-25 04:52:08 +07:00
|
|
|
assert.Equal(s.T(), expectedBody, actualBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunStateGetSuite(t *testing.T) {
|
|
|
|
s := new(StateGetSuite)
|
|
|
|
suite.Run(t, s)
|
|
|
|
}
|