Skip to content

Commit

Permalink
🐛 Fix appending Tickers for new Users
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b committed Oct 24, 2023
1 parent fb6946f commit 3ad1a40
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 34 deletions.
17 changes: 5 additions & 12 deletions internal/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func (h *handler) GetUser(c *gin.Context) {

func (h *handler) PostUser(c *gin.Context) {
var body struct {
Email string `json:"email,omitempty" binding:"required" validate:"email"`
Password string `json:"password,omitempty" binding:"required" validate:"min=10"`
IsSuperAdmin bool `json:"isSuperAdmin,omitempty"`
Tickers []int `json:"tickers,omitempty"`
Email string `json:"email,omitempty" binding:"required" validate:"email"`
Password string `json:"password,omitempty" binding:"required" validate:"min=10"`
IsSuperAdmin bool `json:"isSuperAdmin,omitempty"`
Tickers []storage.Ticker `json:"tickers,omitempty"`
}

err := c.Bind(&body)
Expand All @@ -57,15 +57,8 @@ func (h *handler) PostUser(c *gin.Context) {
c.JSON(http.StatusBadRequest, response.ErrorResponse(response.CodeDefault, response.StorageError))
return
}

tickers, err := h.storage.FindTickersByIDs(body.Tickers)
if err != nil {
c.JSON(http.StatusBadRequest, response.ErrorResponse(response.CodeDefault, response.StorageError))
return
}

user.IsSuperAdmin = body.IsSuperAdmin
user.Tickers = tickers
user.Tickers = body.Tickers

err = h.storage.SaveUser(&user)
if err != nil {
Expand Down
22 changes: 0 additions & 22 deletions internal/api/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,27 +176,6 @@ func TestPostUserStorageError(t *testing.T) {
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{}, nil)
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 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,
Expand All @@ -216,7 +195,6 @@ func TestPostUser(t *testing.T) {
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{}, nil)
s.On("SaveUser", mock.Anything).Return(nil)
h := handler{
storage: s,
Expand Down

0 comments on commit 3ad1a40

Please sign in to comment.