Skip to content

Commit

Permalink
Extend Test Cases
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b committed Oct 8, 2023
1 parent e46e13f commit 43e0738
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
2 changes: 0 additions & 2 deletions internal/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,12 @@ func (h *handler) PostUser(c *gin.Context) {
return
}

//TODO: Validation
user, err := storage.NewUser(body.Email, body.Password)
if err != nil {
c.JSON(http.StatusBadRequest, response.ErrorResponse(response.CodeDefault, response.StorageError))
return
}

// load tickers by body.Tickers
tickers, err := h.storage.FindTickersByIDs(body.Tickers)
if err != nil {
c.JSON(http.StatusBadRequest, response.ErrorResponse(response.CodeDefault, response.StorageError))
Expand Down
59 changes: 59 additions & 0 deletions internal/api/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ func TestPostUserMissingBody(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, w.Code)
}

func TestPostUserTooLongPassword(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
json := `{"email":"louis@systemli.org","password":"swusp-dud-gust-grong-yuz-swuft-plaft-glact-skast-swem-yen-kom-tut-prisp-gont"}`
c.Request = httptest.NewRequest(http.MethodPost, "/v1/admin/users", strings.NewReader(json))
c.Request.Header.Add("Content-Type", "application/json")
c.Set("me", storage.User{ID: 1, IsSuperAdmin: true})
s := &storage.MockStorage{}
h := handler{
storage: s,
config: config.NewConfig(),
}

h.PostUser(c)

assert.Equal(t, http.StatusBadRequest, w.Code)
}

func TestPostUserStorageError(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
Expand All @@ -170,6 +188,26 @@ func TestPostUserStorageError(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, w.Code)
}

func TestPostUserStorageError2(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
json := `{"email":"louis@systemli.org","password":"password1234"}`
c.Request = httptest.NewRequest(http.MethodPost, "/v1/admin/users", strings.NewReader(json))
c.Request.Header.Add("Content-Type", "application/json")
c.Set("me", storage.User{ID: 1, IsSuperAdmin: true})
s := &storage.MockStorage{}
s.On("FindTickersByIDs", mock.Anything).Return([]storage.Ticker{}, errors.New("storage error"))
s.On("SaveUser", mock.Anything).Return(errors.New("storage error"))
h := handler{
storage: s,
config: config.NewConfig(),
}

h.PostUser(c)

assert.Equal(t, http.StatusBadRequest, w.Code)
}

func TestPostUser(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
Expand Down Expand Up @@ -245,6 +283,27 @@ func TestPutUserStorageError(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, w.Code)
}

func TestPutUserStorageError2(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Set("me", storage.User{ID: 1, IsSuperAdmin: true})
c.Set("user", storage.User{})
json := `{"email":"louis@systemli.org","password":"password1234","is_super_admin":true,"tickers":[1]}`
c.Request = httptest.NewRequest(http.MethodPost, "/v1/admin/users", strings.NewReader(json))
c.Request.Header.Add("Content-Type", "application/json")
s := &storage.MockStorage{}
s.On("FindTickersByIDs", mock.Anything).Return([]storage.Ticker{}, errors.New("storage error"))
s.On("SaveUser", mock.Anything).Return(errors.New("storage error"))
h := handler{
storage: s,
config: config.NewConfig(),
}

h.PutUser(c)

assert.Equal(t, http.StatusBadRequest, w.Code)
}

func TestPutUser(t *testing.T) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
Expand Down
22 changes: 19 additions & 3 deletions internal/storage/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,35 @@ import (
"github.com/stretchr/testify/assert"
)

const (
Password = "password"
TooLongPassword = "swusp-dud-gust-grong-yuz-swuft-plaft-glact-skast-swem-yen-kom-tut-prisp-gont"
)

func TestUserAuthenticate(t *testing.T) {
user, err := NewUser("louis@systemli.org", "password")
user, err := NewUser("louis@systemli.org", Password)
assert.Nil(t, err)

assert.False(t, user.Authenticate("wrong"))
assert.True(t, user.Authenticate("password"))
assert.True(t, user.Authenticate(Password))
}

func TestUserUpdatePassword(t *testing.T) {
user, err := NewUser("louis@systemli.org", "password")
user, err := NewUser("louis@systemli.org", Password)
assert.Nil(t, err)

oldEncPassword := user.EncryptedPassword
user.UpdatePassword("newPassword")
assert.NotEqual(t, oldEncPassword, user.EncryptedPassword)

user.UpdatePassword(TooLongPassword)
assert.NotEqual(t, oldEncPassword, user.EncryptedPassword)
}

func TestNewUser(t *testing.T) {
_, err := NewUser("user@systemli.org", Password)
assert.Nil(t, err)

_, err = NewUser("user@systemli.org", TooLongPassword)
assert.NotNil(t, err)
}

0 comments on commit 43e0738

Please sign in to comment.