-
Notifications
You must be signed in to change notification settings - Fork 11
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
1 parent
d565126
commit 42422b1
Showing
35 changed files
with
3,481 additions
and
3,748 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
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
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 |
---|---|---|
|
@@ -2,7 +2,6 @@ package api | |
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
|
@@ -12,80 +11,93 @@ import ( | |
|
||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/suite" | ||
"github.com/systemli/ticker/internal/api/response" | ||
"github.com/systemli/ticker/internal/config" | ||
"github.com/systemli/ticker/internal/logger" | ||
"github.com/systemli/ticker/internal/storage" | ||
) | ||
|
||
var l = logger.NewLogrus("debug", "text") | ||
type APITestSuite struct { | ||
cfg config.Config | ||
store *storage.MockStorage | ||
logger *logrus.Logger | ||
suite.Suite | ||
} | ||
|
||
func init() { | ||
logrus.SetOutput(io.Discard) | ||
func (s *APITestSuite) SetupTest() { | ||
gin.SetMode(gin.TestMode) | ||
} | ||
logrus.SetOutput(io.Discard) | ||
|
||
func TestHealthz(t *testing.T) { | ||
c := config.LoadConfig("") | ||
s := &storage.MockStorage{} | ||
r := API(c, s, l) | ||
s.cfg = config.LoadConfig("") | ||
s.store = &storage.MockStorage{} | ||
|
||
logger := logger.NewLogrus("debug", "text") | ||
logger.SetOutput(io.Discard) | ||
s.logger = logger | ||
} | ||
|
||
func (s *APITestSuite) TestHealthz() { | ||
r := API(s.cfg, s.store, s.logger) | ||
req := httptest.NewRequest(http.MethodGet, "/healthz", nil) | ||
w := httptest.NewRecorder() | ||
r.ServeHTTP(w, req) | ||
|
||
fmt.Println(w.Body.String()) | ||
assert.Equal(t, http.StatusOK, w.Code) | ||
s.Equal(http.StatusOK, w.Code) | ||
s.store.AssertExpectations(s.T()) | ||
} | ||
|
||
func TestLoginNotSuccessful(t *testing.T) { | ||
c := config.LoadConfig("") | ||
s := &storage.MockStorage{} | ||
user := storage.User{} | ||
user.UpdatePassword("password") | ||
s.On("FindUserByEmail", mock.Anything).Return(user, nil) | ||
r := API(c, s, l) | ||
func (s *APITestSuite) TestLogin() { | ||
s.Run("when password is wrong", func() { | ||
user, err := storage.NewUser("[email protected]", "password") | ||
s.NoError(err) | ||
s.store.On("FindUserByEmail", mock.Anything).Return(user, nil) | ||
r := API(s.cfg, s.store, s.logger) | ||
|
||
body := `{"username":"[email protected]","password":"WRONG"}` | ||
req := httptest.NewRequest(http.MethodPost, "/v1/admin/login", strings.NewReader(body)) | ||
req.Header.Add("Content-Type", "application/json") | ||
w := httptest.NewRecorder() | ||
r.ServeHTTP(w, req) | ||
body := `{"username":"[email protected]","password":"WRONG"}` | ||
req := httptest.NewRequest(http.MethodPost, "/v1/admin/login", strings.NewReader(body)) | ||
req.Header.Add("Content-Type", "application/json") | ||
w := httptest.NewRecorder() | ||
r.ServeHTTP(w, req) | ||
|
||
var res response.Response | ||
err := json.Unmarshal(w.Body.Bytes(), &res) | ||
assert.Nil(t, err) | ||
assert.Equal(t, http.StatusUnauthorized, w.Code) | ||
assert.Nil(t, res.Data) | ||
assert.Equal(t, res.Error.Code, response.CodeBadCredentials) | ||
assert.Equal(t, res.Error.Message, response.Unauthorized) | ||
} | ||
var res response.Response | ||
err = json.Unmarshal(w.Body.Bytes(), &res) | ||
s.NoError(err) | ||
s.Equal(http.StatusUnauthorized, w.Code) | ||
s.Nil(res.Data) | ||
s.Equal(res.Error.Code, response.CodeBadCredentials) | ||
s.Equal(res.Error.Message, response.Unauthorized) | ||
s.store.AssertExpectations(s.T()) | ||
}) | ||
|
||
func TestLoginSuccessful(t *testing.T) { | ||
c := config.LoadConfig("") | ||
s := &storage.MockStorage{} | ||
user := storage.User{} | ||
user.UpdatePassword("password") | ||
s.On("FindUserByEmail", mock.Anything).Return(user, nil) | ||
r := API(c, s, l) | ||
s.Run("when login is successful", func() { | ||
user, err := storage.NewUser("[email protected]", "password") | ||
s.NoError(err) | ||
s.store.On("FindUserByEmail", mock.Anything).Return(user, nil) | ||
r := API(s.cfg, s.store, s.logger) | ||
|
||
body := `{"username":"[email protected]","password":"password"}` | ||
req := httptest.NewRequest(http.MethodPost, "/v1/admin/login", strings.NewReader(body)) | ||
req.Header.Add("Content-Type", "application/json") | ||
w := httptest.NewRecorder() | ||
r.ServeHTTP(w, req) | ||
body := `{"username":"[email protected]","password":"password"}` | ||
req := httptest.NewRequest(http.MethodPost, "/v1/admin/login", strings.NewReader(body)) | ||
req.Header.Add("Content-Type", "application/json") | ||
w := httptest.NewRecorder() | ||
r.ServeHTTP(w, req) | ||
|
||
var res struct { | ||
Code int `json:"code"` | ||
Expire time.Time `json:"expire"` | ||
Token string `json:"token"` | ||
} | ||
err = json.Unmarshal(w.Body.Bytes(), &res) | ||
s.NoError(err) | ||
s.Equal(http.StatusOK, w.Code) | ||
s.Equal(http.StatusOK, res.Code) | ||
s.NotNil(res.Expire) | ||
s.NotNil(res.Token) | ||
s.store.AssertExpectations(s.T()) | ||
}) | ||
} | ||
|
||
var res struct { | ||
Code int `json:"code"` | ||
Expire time.Time `json:"expire"` | ||
Token string `json:"token"` | ||
} | ||
err := json.Unmarshal(w.Body.Bytes(), &res) | ||
assert.Nil(t, err) | ||
assert.Equal(t, http.StatusOK, w.Code) | ||
assert.Equal(t, http.StatusOK, res.Code) | ||
assert.NotNil(t, res.Expire) | ||
assert.NotNil(t, res.Token) | ||
func TestAPITestSuite(t *testing.T) { | ||
suite.Run(t, new(APITestSuite)) | ||
} |
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
Oops, something went wrong.