-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
raul-brainattica
committed
Apr 16, 2015
1 parent
b1c8068
commit 8a3eb05
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |