Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix appending Tickers for new Users #264

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading