123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- package user
- import (
- "context"
- "database/sql"
- "net/http/httptest"
- "testing"
- "github.com/brianvoe/gofakeit/v6"
- "github.com/gofiber/fiber/v2"
- "github.com/gojuno/minimock/v3"
- "github.com/golang-jwt/jwt/v4"
- "github.com/stretchr/testify/assert"
- "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1/user/mocks"
- "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
- "git.dmitriygnatenko.ru/dima/homethings/internal/helpers/test"
- "git.dmitriygnatenko.ru/dima/homethings/internal/models"
- "git.dmitriygnatenko.ru/dima/homethings/internal/services/auth"
- )
- func TestUpdateUserHandler(t *testing.T) {
- t.Parallel()
- type req struct {
- method string
- route string
- contentType string
- body *dto.UpdateUserRequest
- }
- var (
- username = gofakeit.Username()
- password = gofakeit.Word()
- hash = gofakeit.Word()
- testError = gofakeit.Error()
- claims = jwt.MapClaims{
- auth.ClaimsKeyName: username,
- }
- user = models.User{
- ID: gofakeit.Uint64(),
- Username: username,
- Password: password,
- CreatedAt: gofakeit.Date(),
- UpdatedAt: gofakeit.Date(),
- }
- correctReq = req{
- method: fiber.MethodPut,
- route: "/v1/users/",
- body: &dto.UpdateUserRequest{
- Username: &username,
- Password: &password,
- },
- contentType: fiber.MIMEApplicationJSON,
- }
- )
- tests := []struct {
- name string
- req req
- resCode int
- resBody interface{}
- authService func(mc *minimock.Controller) AuthService
- userRepoMock func(mc *minimock.Controller) UserRepository
- }{
- {
- name: "positive case",
- req: correctReq,
- resCode: fiber.StatusOK,
- resBody: dto.EmptyResponse{},
- authService: func(mc *minimock.Controller) AuthService {
- mock := mocks.NewAuthServiceMock(mc)
- mock.GeneratePasswordHashMock.Inspect(func(p string) {
- assert.Equal(mc, password, p)
- }).Return(hash, nil)
- mock.GetClaimsMock.Return(claims)
- return mock
- },
- userRepoMock: func(mc *minimock.Controller) UserRepository {
- mock := mocks.NewUserRepositoryMock(mc)
- mock.GetMock.Inspect(func(ctx context.Context, u string) {
- assert.Equal(mc, username, u)
- }).Return(&user, nil)
- mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateUserRequest) {
- assert.Equal(mc, hash, req.Password.String)
- assert.Equal(mc, username, req.Username.String)
- }).Return(nil)
- return mock
- },
- },
- {
- name: "negative case - bad request",
- req: req{
- method: fiber.MethodPut,
- route: "/v1/users/",
- body: &dto.UpdateUserRequest{},
- contentType: fiber.MIMEApplicationJSON,
- },
- resCode: fiber.StatusBadRequest,
- authService: func(mc *minimock.Controller) AuthService {
- return mocks.NewAuthServiceMock(mc)
- },
- userRepoMock: func(mc *minimock.Controller) UserRepository {
- return mocks.NewUserRepositoryMock(mc)
- },
- },
- {
- name: "negative case - body parse error",
- req: req{
- method: fiber.MethodPut,
- route: "/v1/users/",
- body: &dto.UpdateUserRequest{},
- },
- resCode: fiber.StatusBadRequest,
- authService: func(mc *minimock.Controller) AuthService {
- return mocks.NewAuthServiceMock(mc)
- },
- userRepoMock: func(mc *minimock.Controller) UserRepository {
- return mocks.NewUserRepositoryMock(mc)
- },
- },
- {
- name: "negative case - auth service error",
- req: correctReq,
- resCode: fiber.StatusInternalServerError,
- authService: func(mc *minimock.Controller) AuthService {
- mock := mocks.NewAuthServiceMock(mc)
- mock.GeneratePasswordHashMock.Inspect(func(p string) {
- assert.Equal(mc, password, p)
- }).Return("", testError)
- return mock
- },
- userRepoMock: func(mc *minimock.Controller) UserRepository {
- return mocks.NewUserRepositoryMock(mc)
- },
- },
- {
- name: "negative case - repository error - get user",
- req: correctReq,
- resCode: fiber.StatusInternalServerError,
- authService: func(mc *minimock.Controller) AuthService {
- mock := mocks.NewAuthServiceMock(mc)
- mock.GeneratePasswordHashMock.Inspect(func(p string) {
- assert.Equal(mc, password, p)
- }).Return(hash, nil)
- mock.GetClaimsMock.Return(claims)
- return mock
- },
- userRepoMock: func(mc *minimock.Controller) UserRepository {
- mock := mocks.NewUserRepositoryMock(mc)
- mock.GetMock.Inspect(func(ctx context.Context, u string) {
- assert.Equal(mc, username, u)
- }).Return(nil, testError)
- return mock
- },
- },
- {
- name: "negative case - repository error - update user",
- req: correctReq,
- resCode: fiber.StatusInternalServerError,
- authService: func(mc *minimock.Controller) AuthService {
- mock := mocks.NewAuthServiceMock(mc)
- mock.GeneratePasswordHashMock.Inspect(func(p string) {
- assert.Equal(mc, password, p)
- }).Return(hash, nil)
- mock.GetClaimsMock.Return(claims)
- return mock
- },
- userRepoMock: func(mc *minimock.Controller) UserRepository {
- mock := mocks.NewUserRepositoryMock(mc)
- mock.GetMock.Inspect(func(ctx context.Context, u string) {
- assert.Equal(mc, username, u)
- }).Return(&user, nil)
- mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateUserRequest) {
- assert.Equal(mc, hash, req.Password.String)
- assert.Equal(mc, username, req.Username.String)
- }).Return(testError)
- return mock
- },
- },
- {
- name: "negative case - user not found",
- req: correctReq,
- resCode: fiber.StatusBadRequest,
- authService: func(mc *minimock.Controller) AuthService {
- mock := mocks.NewAuthServiceMock(mc)
- mock.GeneratePasswordHashMock.Inspect(func(p string) {
- assert.Equal(mc, password, p)
- }).Return(hash, nil)
- mock.GetClaimsMock.Return(claims)
- return mock
- },
- userRepoMock: func(mc *minimock.Controller) UserRepository {
- mock := mocks.NewUserRepositoryMock(mc)
- mock.GetMock.Inspect(func(ctx context.Context, u string) {
- assert.Equal(mc, username, u)
- }).Return(nil, sql.ErrNoRows)
- return mock
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- t.Parallel()
- mc := minimock.NewController(t)
- fiberApp := fiber.New()
- fiberApp.Put("/v1/users", UpdateUserHandler(tt.authService(mc), tt.userRepoMock(mc)))
- fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, test.ConvertDataToIOReader(tt.req.body))
- fiberReq.Header.Add(fiber.HeaderContentType, tt.req.contentType)
- fiberRes, _ := fiberApp.Test(fiberReq, test.TestTimeout)
- assert.Equal(t, tt.resCode, fiberRes.StatusCode)
- if tt.resBody != nil {
- assert.Equal(t, test.MarshalResponse(tt.resBody), test.ConvertBodyToString(fiberRes.Body))
- }
- })
- }
- }
|