Skip to content

Commit

Permalink
Middleware tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
raul-brainattica committed Apr 16, 2015
1 parent b1c8068 commit 8a3eb05
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions tests/api_tests/auth_middleware_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package api_tests

import (
"api.jwt.auth/core/authentication"
"api.jwt.auth/routers"
"api.jwt.auth/services"
"api.jwt.auth/settings"
"fmt"
"github.com/codegangsta/negroni"
"github.com/stretchr/testify/assert"
. "gopkg.in/check.v1"
"net/http"
"net/http/httptest"
"os"
"testing"
)

func Test(t *testing.T) {
TestingT(t)
}

type MiddlewaresTestSuite struct{}

var _ = Suite(&MiddlewaresTestSuite{})
var t *testing.T
var token string
var server *negroni.Negroni

func (s *MiddlewaresTestSuite) SetUpSuite(c *C) {
os.Setenv("GO_ENV", "tests")
settings.Init()
}

func (s *MiddlewaresTestSuite) SetUpTest(c *C) {
authBackend := authentication.InitJWTAuthenticationBackend()
assert.NotNil(t, authBackend)
token, _ = authBackend.GenerateToken("1234")

router := routers.InitRoutes()
server = negroni.Classic()
server.UseHandler(router)
}

func (s *MiddlewaresTestSuite) TestRequireTokenAuthentication(c *C) {
resource := "/test/hello"

response := httptest.NewRecorder()
request, _ := http.NewRequest("GET", resource, nil)
request.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token))
server.ServeHTTP(response, request)

assert.Equal(t, response.Code, http.StatusOK)
}

func (s *MiddlewaresTestSuite) TestRequireTokenAuthenticationInvalidToken(c *C) {
resource := "/test/hello"

response := httptest.NewRecorder()
request, _ := http.NewRequest("GET", resource, nil)
request.Header.Set("Authorization", fmt.Sprintf("Bearer %v", "token"))
server.ServeHTTP(response, request)

assert.Equal(t, response.Code, http.StatusUnauthorized)
}

func (s *MiddlewaresTestSuite) TestRequireTokenAuthenticationEmptyToken(c *C) {
resource := "/test/hello"

response := httptest.NewRecorder()
request, _ := http.NewRequest("GET", resource, nil)
request.Header.Set("Authorization", fmt.Sprintf("Bearer %v", ""))
server.ServeHTTP(response, request)

assert.Equal(t, response.Code, http.StatusUnauthorized)
}

func (s *MiddlewaresTestSuite) TestRequireTokenAuthenticationWithoutToken(c *C) {
resource := "/test/hello"

response := httptest.NewRecorder()
request, _ := http.NewRequest("GET", resource, nil)
server.ServeHTTP(response, request)

assert.Equal(t, response.Code, http.StatusUnauthorized)
}

func (suite *MiddlewaresTestSuite) TestRequireTokenAuthenticationAfterLogout(c *C) {
resource := "/test/hello"

requestLogout, _ := http.NewRequest("GET", resource, nil)
requestLogout.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token))
services.Logout(requestLogout)

response := httptest.NewRecorder()
request, _ := http.NewRequest("GET", resource, nil)
request.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token))
server.ServeHTTP(response, request)

assert.Equal(t, response.Code, http.StatusUnauthorized)
}

0 comments on commit 8a3eb05

Please sign in to comment.