|
| 1 | +package unit_tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "api.jwt.auth/services" |
| 5 | + "api.jwt.auth/services/models" |
| 6 | + "api.jwt.auth/settings" |
| 7 | + jwt "github.com/dgrijalva/jwt-go" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + . "gopkg.in/check.v1" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +func Test(t *testing.T) { |
| 16 | + TestingT(t) |
| 17 | +} |
| 18 | + |
| 19 | +type AuthenticationServicesTestSuite struct{} |
| 20 | + |
| 21 | +var _ = Suite(&AuthenticationServicesTestSuite{}) |
| 22 | +var t *testing.T |
| 23 | + |
| 24 | +func (s *AuthenticationServicesTestSuite) SetUpSuite(c *C) { |
| 25 | + os.Setenv("GO_ENV", "tests") |
| 26 | + settings.Init() |
| 27 | +} |
| 28 | + |
| 29 | +func (suite *AuthenticationServicesTestSuite) TestLogin(c *C) { |
| 30 | + user := models.User{ |
| 31 | + Username: "haku", |
| 32 | + Password: "testing", |
| 33 | + } |
| 34 | + response, token := services.Login(&user) |
| 35 | + assert.Equal(t, http.StatusOK, response) |
| 36 | + assert.NotEmpty(t, token) |
| 37 | +} |
| 38 | + |
| 39 | +func (suite *AuthenticationServicesTestSuite) TestLoginIncorrectPassword(c *C) { |
| 40 | + user := models.User{ |
| 41 | + Username: "haku", |
| 42 | + Password: "Password", |
| 43 | + } |
| 44 | + response, token := services.Login(&user) |
| 45 | + assert.Equal(t, http.StatusUnauthorized, response) |
| 46 | + assert.Empty(t, token) |
| 47 | +} |
| 48 | + |
| 49 | +func (suite *AuthenticationServicesTestSuite) TestLoginIncorrectUsername(c *C) { |
| 50 | + user := models.User{ |
| 51 | + Username: "Username", |
| 52 | + Password: "testing", |
| 53 | + } |
| 54 | + response, token := services.Login(&user) |
| 55 | + assert.Equal(t, http.StatusUnauthorized, response) |
| 56 | + assert.Empty(t, token) |
| 57 | +} |
| 58 | + |
| 59 | +func (suite *AuthenticationServicesTestSuite) TestLoginEmptyCredentials(c *C) { |
| 60 | + user := models.User{ |
| 61 | + Username: "", |
| 62 | + Password: "", |
| 63 | + } |
| 64 | + response, token := services.Login(&user) |
| 65 | + assert.Equal(t, http.StatusUnauthorized, response) |
| 66 | + assert.Empty(t, token) |
| 67 | +} |
| 68 | + |
| 69 | +func (suite *AuthenticationServicesTestSuite) TestRefreshToken(c *C) { |
| 70 | + user := models.User{ |
| 71 | + Username: "haku", |
| 72 | + Password: "testing", |
| 73 | + } |
| 74 | + authBackend := authentication.InitJWTAuthenticationBackend() |
| 75 | + tokenString, err := authBackend.GenerateToken(user.UUID) |
| 76 | + token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { |
| 77 | + return authBackend.PublicKey, nil |
| 78 | + }) |
| 79 | + assert.Nil(t, err) |
| 80 | + |
| 81 | + newToken := services.RefreshToken(token) |
| 82 | + assert.NotEmpty(t, newToken) |
| 83 | +} |
| 84 | + |
| 85 | +func (suite *AuthenticationServicesTestSuite) TestRefreshTokenInvalidToken(c *C) { |
| 86 | + token := jwt.New(jwt.GetSigningMethod("RS256")) |
| 87 | + newToken := services.RefreshToken(token) |
| 88 | + assert.Empty(t, newToken) |
| 89 | +} |
| 90 | + |
| 91 | +func (suite *AuthenticationServicesTestSuite) TestLogout(c *C) { |
| 92 | + user := models.User{ |
| 93 | + Username: "haku", |
| 94 | + Password: "testing", |
| 95 | + } |
| 96 | + authBackend := auth.InitJWTAuthenticationBackend() |
| 97 | + tokenString, err := authentication.GenerateToken(user.UUID) |
| 98 | + token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { |
| 99 | + return authBackend.PublicKey, nil |
| 100 | + }) |
| 101 | + |
| 102 | + err = services.Logout(tokenString, token) |
| 103 | + assert.Nil(t, err) |
| 104 | +} |
0 commit comments