Skip to content

Commit

Permalink
auth backend tested
Browse files Browse the repository at this point in the history
  • Loading branch information
raul-brainattica committed Apr 16, 2015
1 parent 4f0829a commit 610b409
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions tests/unit_tests/auth_backend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package unit_tests

import (
"api.jwt.auth/core/authentication"
"api.jwt.auth/core/redis"
"api.jwt.auth/services/models"
"api.jwt.auth/settings"
jwt "github.com/dgrijalva/jwt-go"
"github.com/stretchr/testify/assert"
. "gopkg.in/check.v1"
"os"
"testing"
)

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

type AuthenticationBackendTestSuite struct{}

var _ = Suite(&AuthenticationBackendTestSuite{})
var t *testing.T

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

func (suite *AuthenticationBackendTestSuite) TestInitJWTAuthenticationBackend(c *C) {
authBackend := authentication.InitJWTAuthenticationBackend()
c.Assert(authBackend, NotNil)
c.Assert(authBackend.PublicKey, NotNil)
}

func (suite *AuthenticationBackendTestSuite) TestGenerateToken(c *C) {
authBackend := authentication.InitJWTAuthenticationBackend()
tokenString, err := authBackend.GenerateToken("1234")

assert.Nil(t, err)
assert.NotEmpty(t, tokenString)

token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return authBackend.PublicKey, nil
})

assert.Nil(t, err)
assert.True(t, token.Valid)
}

func (suite *AuthenticationBackendTestSuite) TestAuthenticate(c *C) {
authBackend := authentication.InitJWTAuthenticationBackend()
user := &models.User{
Username: "haku",
Password: "testing",
}
c.Assert(authBackend.Authenticate(user), Equals, true)
}

func (suite *AuthenticationBackendTestSuite) TestAuthenticateIncorrectPass(c *C) {
authBackend := authentication.InitJWTAuthenticationBackend()
user := &models.User{
Username: "haku",
Password: "Password",
}
c.Assert(authBackend.Authenticate(user), Equals, false)
}

func (suite *AuthenticationBackendTestSuite) TestAuthenticateIncorrectUsername(c *C) {
authBackend := authentication.InitJWTAuthenticationBackend()
user := &models.User{
Username: "Haku",
Password: "testing",
}
c.Assert(authBackend.Authenticate(user), Equals, false)
}

func (suite *AuthenticationBackendTestSuite) TestLogout(c *C) {
authBackend := authentication.InitJWTAuthenticationBackend()
tokenString, err := authBackend.GenerateToken("1234")
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return authBackend.PublicKey, nil
})
err = authBackend.Logout(tokenString, token)
assert.Nil(t, err)

redisConn := redis.Connect()
redisValue, err := redisConn.GetValue(tokenString)
assert.Nil(t, err)
assert.NotEmpty(t, redisValue)
}

func (suite *AuthenticationBackendTestSuite) TestIsInBlacklist(c *C) {
authBackend := authentication.InitJWTAuthenticationBackend()
tokenString, err := authBackend.GenerateToken("1234")
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return authBackend.PublicKey, nil
})
err = authBackend.Logout(tokenString, token)
assert.Nil(t, err)

assert.True(t, authBackend.IsInBlacklist(tokenString))
}

func (suite *AuthenticationBackendTestSuite) TestIsNotInBlacklist(c *C) {
authBackend := authentication.InitJWTAuthenticationBackend()
assert.False(t, authBackend.IsInBlacklist("1234"))
}

0 comments on commit 610b409

Please sign in to comment.