Skip to content

Commit 610b409

Browse files
author
raul-brainattica
committed
auth backend tested
1 parent 4f0829a commit 610b409

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

tests/unit_tests/auth_backend_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package unit_tests
2+
3+
import (
4+
"api.jwt.auth/core/authentication"
5+
"api.jwt.auth/core/redis"
6+
"api.jwt.auth/services/models"
7+
"api.jwt.auth/settings"
8+
jwt "github.com/dgrijalva/jwt-go"
9+
"github.com/stretchr/testify/assert"
10+
. "gopkg.in/check.v1"
11+
"os"
12+
"testing"
13+
)
14+
15+
func Test(t *testing.T) {
16+
TestingT(t)
17+
}
18+
19+
type AuthenticationBackendTestSuite struct{}
20+
21+
var _ = Suite(&AuthenticationBackendTestSuite{})
22+
var t *testing.T
23+
24+
func (s *AuthenticationBackendTestSuite) SetUpSuite(c *C) {
25+
os.Setenv("GO_ENV", "tests")
26+
settings.Init()
27+
}
28+
29+
func (suite *AuthenticationBackendTestSuite) TestInitJWTAuthenticationBackend(c *C) {
30+
authBackend := authentication.InitJWTAuthenticationBackend()
31+
c.Assert(authBackend, NotNil)
32+
c.Assert(authBackend.PublicKey, NotNil)
33+
}
34+
35+
func (suite *AuthenticationBackendTestSuite) TestGenerateToken(c *C) {
36+
authBackend := authentication.InitJWTAuthenticationBackend()
37+
tokenString, err := authBackend.GenerateToken("1234")
38+
39+
assert.Nil(t, err)
40+
assert.NotEmpty(t, tokenString)
41+
42+
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
43+
return authBackend.PublicKey, nil
44+
})
45+
46+
assert.Nil(t, err)
47+
assert.True(t, token.Valid)
48+
}
49+
50+
func (suite *AuthenticationBackendTestSuite) TestAuthenticate(c *C) {
51+
authBackend := authentication.InitJWTAuthenticationBackend()
52+
user := &models.User{
53+
Username: "haku",
54+
Password: "testing",
55+
}
56+
c.Assert(authBackend.Authenticate(user), Equals, true)
57+
}
58+
59+
func (suite *AuthenticationBackendTestSuite) TestAuthenticateIncorrectPass(c *C) {
60+
authBackend := authentication.InitJWTAuthenticationBackend()
61+
user := &models.User{
62+
Username: "haku",
63+
Password: "Password",
64+
}
65+
c.Assert(authBackend.Authenticate(user), Equals, false)
66+
}
67+
68+
func (suite *AuthenticationBackendTestSuite) TestAuthenticateIncorrectUsername(c *C) {
69+
authBackend := authentication.InitJWTAuthenticationBackend()
70+
user := &models.User{
71+
Username: "Haku",
72+
Password: "testing",
73+
}
74+
c.Assert(authBackend.Authenticate(user), Equals, false)
75+
}
76+
77+
func (suite *AuthenticationBackendTestSuite) TestLogout(c *C) {
78+
authBackend := authentication.InitJWTAuthenticationBackend()
79+
tokenString, err := authBackend.GenerateToken("1234")
80+
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
81+
return authBackend.PublicKey, nil
82+
})
83+
err = authBackend.Logout(tokenString, token)
84+
assert.Nil(t, err)
85+
86+
redisConn := redis.Connect()
87+
redisValue, err := redisConn.GetValue(tokenString)
88+
assert.Nil(t, err)
89+
assert.NotEmpty(t, redisValue)
90+
}
91+
92+
func (suite *AuthenticationBackendTestSuite) TestIsInBlacklist(c *C) {
93+
authBackend := authentication.InitJWTAuthenticationBackend()
94+
tokenString, err := authBackend.GenerateToken("1234")
95+
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
96+
return authBackend.PublicKey, nil
97+
})
98+
err = authBackend.Logout(tokenString, token)
99+
assert.Nil(t, err)
100+
101+
assert.True(t, authBackend.IsInBlacklist(tokenString))
102+
}
103+
104+
func (suite *AuthenticationBackendTestSuite) TestIsNotInBlacklist(c *C) {
105+
authBackend := authentication.InitJWTAuthenticationBackend()
106+
assert.False(t, authBackend.IsInBlacklist("1234"))
107+
}

0 commit comments

Comments
 (0)