Properly reset global state in auth tests
This commit is contained in:
@ -14,6 +14,7 @@ func mockHTTPHandler(writer http.ResponseWriter, _ *http.Request) {
|
||||
}
|
||||
|
||||
func TestNewRouterV1WithAuthenticationDisabled(t *testing.T) {
|
||||
config.Config.Server.Token = ""
|
||||
router := mux.NewRouter()
|
||||
v1 := newRouterV1(router)
|
||||
|
||||
@ -64,4 +65,5 @@ func TestNewRouterV1WithAuthenticationEnabled(t *testing.T) {
|
||||
router.ServeHTTP(recorder, request)
|
||||
assert.Equal(t, http.StatusUnauthorized, recorder.Code)
|
||||
})
|
||||
config.Config.Server.Token = ""
|
||||
}
|
||||
|
@ -21,8 +21,7 @@ type AuthenticationMiddlewareTestSuite struct {
|
||||
}
|
||||
|
||||
func (suite *AuthenticationMiddlewareTestSuite) SetupTest() {
|
||||
config.Config.Server.Token = testToken
|
||||
InitializeAuthentication()
|
||||
correctAuthenticationToken = []byte(testToken)
|
||||
suite.recorder = httptest.NewRecorder()
|
||||
request, err := http.NewRequest(http.MethodGet, "/api/v1/test", nil)
|
||||
if err != nil {
|
||||
@ -35,6 +34,10 @@ func (suite *AuthenticationMiddlewareTestSuite) SetupTest() {
|
||||
}))
|
||||
}
|
||||
|
||||
func (suite *AuthenticationMiddlewareTestSuite) TearDownTest() {
|
||||
correctAuthenticationToken = []byte(nil)
|
||||
}
|
||||
|
||||
func (suite *AuthenticationMiddlewareTestSuite) TestReturns401WhenHeaderUnset() {
|
||||
suite.httpAuthenticationMiddleware.ServeHTTP(suite.recorder, suite.request)
|
||||
assert.Equal(suite.T(), http.StatusUnauthorized, suite.recorder.Code)
|
||||
@ -69,3 +72,20 @@ func (suite *AuthenticationMiddlewareTestSuite) TestPassesWhenTokenCorrect() {
|
||||
func TestHTTPAuthenticationMiddleware(t *testing.T) {
|
||||
suite.Run(t, new(AuthenticationMiddlewareTestSuite))
|
||||
}
|
||||
|
||||
func TestInitializeAuthentication(t *testing.T) {
|
||||
t.Run("if token unset", func(t *testing.T) {
|
||||
config.Config.Server.Token = ""
|
||||
initialized := InitializeAuthentication()
|
||||
assert.Equal(t, false, initialized)
|
||||
assert.Equal(t, []byte(nil), correctAuthenticationToken, "it should not set correctAuthenticationToken")
|
||||
})
|
||||
t.Run("if token set", func(t *testing.T) {
|
||||
config.Config.Server.Token = testToken
|
||||
initialized := InitializeAuthentication()
|
||||
assert.Equal(t, true, initialized)
|
||||
assert.Equal(t, []byte(testToken), correctAuthenticationToken, "it should set correctAuthenticationToken")
|
||||
config.Config.Server.Token = ""
|
||||
correctAuthenticationToken = []byte(nil)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user