Skip to content

Commit

Permalink
Updated GA
Browse files Browse the repository at this point in the history
  • Loading branch information
mukulmantosh committed Oct 22, 2023
1 parent a013b19 commit b605d66
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/server/user_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func (s *EchoServer) GetUserAddressById(ctx echo.Context) error {

func (s *EchoServer) UpdateUserAddress(ctx echo.Context) error {
ID := ctx.Param("id")
if len(ID) == 0 {
return ctx.JSON(http.StatusBadRequest, map[string]any{"error": "ID Missing"})
}
userAddress := new(models.UserAddress)
if err := ctx.Bind(userAddress); err != nil {
return ctx.JSON(http.StatusUnsupportedMediaType, map[string]any{"error": err.Error()})
Expand Down Expand Up @@ -85,6 +88,9 @@ func (s *EchoServer) UpdateUserAddress(ctx echo.Context) error {

func (s *EchoServer) DeleteUserAddress(ctx echo.Context) error {
ID := ctx.Param("id")
if len(ID) == 0 {
return ctx.JSON(http.StatusBadRequest, map[string]any{"error": "ID Missing"})
}
err := s.DB.DeleteUserAddress(ctx.Request().Context(), ID)
if err != nil {
return ctx.JSON(http.StatusInternalServerError, map[string]any{"error": err.Error()})
Expand Down
137 changes: 137 additions & 0 deletions internal/tests/users/update_user_address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package users

import (
"encoding/json"
"github.com/go-faker/faker/v4"
"github.com/labstack/echo/v4"
"github.com/mukulmantosh/go-ecommerce-app/internal/server"
"github.com/mukulmantosh/go-ecommerce-app/internal/tests"
"github.com/mukulmantosh/go-ecommerce-app/internal/utils"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func TestUpdateUserAddress(t *testing.T) {
testDB, _ := tests.Setup()
type FakeUser struct {
Username string `json:"username" faker:"username"`
Password string `json:"password" faker:"word,unique"`
FirstName string `json:"first_name" faker:"first_name"`
LastName string `json:"last_name" faker:"last_name"`
}

type FakeUserAddress struct {
Address string `json:"address"`
City string `json:"city"`
PostalCode string `json:"postal_code"`
Country string `json:"country"`
Mobile string `json:"mobile"`
UserId string `json:"user_id"`
}

type DataObj struct {
UserID string `json:"user_id"`
Message string `json:"message"`
}
type UserResponse struct {
Data DataObj `json:"data"`
}

type UserAddressDataObj struct {
UserAddressID string `json:"userAddressId"`
Address string `json:"address"`
PostalCode string `json:"postal_code"`
Country string `json:"country"`
Mobile string `json:"mobile"`
UserId string `json:"user_id"`
}

t.Run("update user address by id", func(t *testing.T) {
var service = server.NewServer(testDB)

var customUser FakeUser
_ = faker.FakeData(&customUser)
out, _ := json.Marshal(&customUser)
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/user", strings.NewReader(string(out)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

if assert.NoError(t, service.AddUser(c)) {
assert.Equal(t, http.StatusCreated, rec.Code)
}

var userResp UserResponse
data, _ := io.ReadAll(rec.Body)
err := json.Unmarshal(data, &userResp)
if err != nil {
t.Error(err)
}
userId := userResp.Data.UserID

userAddr := FakeUserAddress{
Address: "132, My Street, Kingston, New York",
City: "New York",
PostalCode: "12401",
Country: "USA",
Mobile: "9898989898",
UserId: userId,
}

// Serialize the struct to a JSON string
userAddressJSON, err := utils.StructToJSON(userAddr)
if err != nil {
t.Error(err.Error())
}

newReq := httptest.NewRequest(http.MethodPost, "/user/address", strings.NewReader(userAddressJSON))
newReq.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
newRec := httptest.NewRecorder()
getContext := e.NewContext(newReq, newRec)

if assert.NoError(t, service.AddUserAddress(getContext)) {
assert.Equal(t, http.StatusCreated, newRec.Code)
}

var userAddressResp UserAddressDataObj
dataAddr, _ := io.ReadAll(newRec.Body)
err = json.Unmarshal(dataAddr, &userAddressResp)
userAddressId := userAddressResp.UserAddressID

UpdateUserAddr := FakeUserAddress{
Address: "Libellengasse 15",
City: "Sankt Ulrich Am Waasen",
PostalCode: "8072",
Country: "Austria",
Mobile: "06888738564",
UserId: userId,
}

// Serialize the struct to a JSON string
UpdateUserAddressJSON, err := utils.StructToJSON(UpdateUserAddr)
if err != nil {
t.Error(err.Error())
}

newAddrReq := httptest.NewRequest(http.MethodPut, "/user/address",
strings.NewReader(UpdateUserAddressJSON))
newAddrReq.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
newAddrRec := httptest.NewRecorder()
UpdateAddrContext := e.NewContext(newAddrReq, newAddrRec)
UpdateAddrContext.SetParamNames("id")
UpdateAddrContext.SetParamValues(userAddressId)

if assert.NoError(t, service.UpdateUserAddress(UpdateAddrContext)) {
assert.Equal(t, http.StatusOK, newAddrRec.Code)
assert.Equal(t, "\"User Information Updated!\"\n", newAddrRec.Body.String())
}

tests.Teardown(testDB)
})

}

0 comments on commit b605d66

Please sign in to comment.