1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package handlers
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/valyala/fasthttp"
"github.com/authelia/authelia/v4/internal/authentication"
"github.com/authelia/authelia/v4/internal/mocks"
)
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, err := s.mock.Ctx.GetSession()
s.Assert().NoError(err)
userSession.Username = "username"
s.Assert().NoError(s.mock.Ctx.SaveSession(userSession))
StateGET(s.mock.Ctx)
type Response struct {
Status string
Data StateResponse
}
expectedBody := Response{
Status: "OK",
Data: StateResponse{
Username: "username",
DefaultRedirectionURL: "https://www.example.com",
AuthenticationLevel: authentication.NotAuthenticated,
},
}
actualBody := Response{}
err = json.Unmarshal(s.mock.Ctx.Response.Body(), &actualBody)
require.NoError(s.T(), err)
assert.Equal(s.T(), fasthttp.StatusOK, s.mock.Ctx.Response.StatusCode())
assert.Equal(s.T(), []byte("application/json; charset=utf-8"), s.mock.Ctx.Response.Header.ContentType())
assert.Equal(s.T(), expectedBody, actualBody)
}
func (s *StateGetSuite) TestShouldReturnAuthenticationLevelFromSession() {
userSession, err := s.mock.Ctx.GetSession()
s.Assert().NoError(err)
userSession.Username = "john"
userSession.AuthenticationMethodRefs.UsernameAndPassword = true
s.Assert().NoError(s.mock.Ctx.SaveSession(userSession))
require.NoError(s.T(), err)
StateGET(s.mock.Ctx)
type Response struct {
Status string
Data StateResponse
}
expectedBody := Response{
Status: "OK",
Data: StateResponse{
Username: "john",
DefaultRedirectionURL: "https://www.example.com",
AuthenticationLevel: authentication.OneFactor,
FactorKnowledge: true,
},
}
actualBody := Response{}
err = json.Unmarshal(s.mock.Ctx.Response.Body(), &actualBody)
require.NoError(s.T(), err)
assert.Equal(s.T(), fasthttp.StatusOK, s.mock.Ctx.Response.StatusCode())
assert.Equal(s.T(), []byte("application/json; charset=utf-8"), s.mock.Ctx.Response.Header.ContentType())
assert.Equal(s.T(), expectedBody, actualBody)
}
func TestRunStateGetSuite(t *testing.T) {
s := new(StateGetSuite)
suite.Run(t, s)
}
|