Skip to content

Commit

Permalink
test auth done
Browse files Browse the repository at this point in the history
  • Loading branch information
JolyBI3ll committed Dec 18, 2024
1 parent 142115a commit 3edd823
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 16 deletions.
4 changes: 2 additions & 2 deletions internal/cities/controller/cities_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ func (h *CityHandler) GetOneCity(w http.ResponseWriter, r *http.Request) {
}
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
payload, err := h.utils.ConvertOneCityProtoToGo(city.City)
if err != nil {
logger.AccessLogger.Error("Failed to convert city data",
Expand All @@ -156,6 +154,8 @@ func (h *CityHandler) GetOneCity(w http.ResponseWriter, r *http.Request) {
body := domain.OneCityResponse{
City: payload,
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if _, err = easyjson.MarshalToWriter(body, w); err != nil {
logger.AccessLogger.Error("Failed to encode response",
zap.String("request_id", requestID),
Expand Down
363 changes: 363 additions & 0 deletions internal/cities/controller/cities_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,363 @@
package controller

import (
"2024_2_FIGHT-CLUB/domain"
"2024_2_FIGHT-CLUB/internal/service/logger"
"2024_2_FIGHT-CLUB/internal/service/utils"
"2024_2_FIGHT-CLUB/microservices/city_service/controller/gen"
"2024_2_FIGHT-CLUB/microservices/city_service/mocks"
"errors"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"io"
"net/http"
"net/http/httptest"
"testing"
)

func TestCityHandler_GetCities_Success(t *testing.T) {
require.NoError(t, logger.InitLoggers())
defer func() {
err := logger.SyncLoggers()
if err != nil {
return
}
}()

// Моки
mockClient := new(mocks.MockGrpcClient)
mockUtils := &utils.MockUtils{}

mockResponse := &gen.GetCitiesResponse{
Cities: []*gen.City{
{
Id: 1,
Title: "Москва",
Entitle: "Moscow",
Description: "Some Desc",
Image: "test_image",
},
{
Id: 2,
Title: "Волгоград",
Entitle: "Volgograd",
Description: "Some Desc",
Image: "test_image",
},
},
}

mockPayload := domain.AllCitiesResponse{
Cities: []*domain.City{
{
ID: 1,
Title: "Москва",
EnTitle: "Moscow",
Description: "Some Desc",
Image: "test_image",
},
{
ID: 2,
Title: "Волгоград",
EnTitle: "Volgograd",
Description: "Some Desc",
Image: "test_image",
},
},
}

// Настройка моков
mockClient.On("GetCities", mock.Anything, mock.Anything, mock.Anything).Return(mockResponse, nil)
mockUtils.On("ConvertAllCitiesProtoToGo", mockResponse).Return(mockPayload, nil)

cityHandler := CityHandler{
client: mockClient,
utils: mockUtils,
}

req := httptest.NewRequest(http.MethodGet, "/cities", nil)
req.Header.Set("X-Real-IP", "127.0.0.1")
w := httptest.NewRecorder()

// Вызов метода
cityHandler.GetCities(w, req)

// Проверка
result := w.Result()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(result.Body)

body, _ := io.ReadAll(result.Body)
expectedResponse := `{"cities":[{"description":"Some Desc", "enTitle":"Moscow", "id":1, "image":"test_image", "title":"Москва"},{"description":"Some Desc", "enTitle":"Volgograd", "id":2, "image":"test_image", "title":"Волгоград"}]}`
assert.Equal(t, http.StatusOK, result.StatusCode)
assert.JSONEq(t, expectedResponse, string(body))

mockClient.AssertExpectations(t)
mockUtils.AssertExpectations(t)
}

func TestCityHandler_GetCities_GrpcError(t *testing.T) {
require.NoError(t, logger.InitLoggers())
defer func() {
err := logger.SyncLoggers()
if err != nil {
return
}
}()

mockClient := new(mocks.MockGrpcClient)
mockUtils := &utils.MockUtils{}

grpcErr := status.Error(codes.Internal, "gRPC error")

// Настройка мока
mockClient.On("GetCities", mock.Anything, &gen.GetCitiesRequest{}, mock.Anything).Return(&gen.GetCitiesResponse{}, grpcErr)

cityHandler := CityHandler{
client: mockClient,
utils: mockUtils,
}

req := httptest.NewRequest(http.MethodGet, "/cities", nil)
w := httptest.NewRecorder()

// Вызов метода
cityHandler.GetCities(w, req)

// Проверка
result := w.Result()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(result.Body)

assert.Equal(t, http.StatusInternalServerError, result.StatusCode)

mockClient.AssertExpectations(t)
}

func TestCityHandler_GetCities_ConversionError(t *testing.T) {
require.NoError(t, logger.InitLoggers())
defer func() {
err := logger.SyncLoggers()
if err != nil {
return
}
}()

mockClient := new(mocks.MockGrpcClient)
mockUtils := &utils.MockUtils{}

mockResponse := &gen.GetCitiesResponse{
Cities: []*gen.City{
{
Id: 1,
Title: "Москва",
Entitle: "Moscow",
Description: "Some Desc",
Image: "test_image",
},
{
Id: 2,
Title: "Волгоград",
Entitle: "Volgograd",
Description: "Some Desc",
Image: "test_image",
},
},
}

conversionErr := errors.New("conversion error")

// Настройка моков
mockClient.On("GetCities", mock.Anything, &gen.GetCitiesRequest{}, mock.Anything).Return(mockResponse, nil)
mockUtils.On("ConvertAllCitiesProtoToGo", mockResponse).Return(nil, conversionErr)

cityHandler := CityHandler{
client: mockClient,
utils: mockUtils,
}

req := httptest.NewRequest(http.MethodGet, "/cities", nil)
w := httptest.NewRecorder()

// Вызов метода
cityHandler.GetCities(w, req)

// Проверка
result := w.Result()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(result.Body)

assert.Equal(t, http.StatusInternalServerError, result.StatusCode)

mockClient.AssertExpectations(t)
mockUtils.AssertExpectations(t)
}

func TestCityHandler_GetOneCity_Success(t *testing.T) {
require.NoError(t, logger.InitLoggers())
defer func() {
err := logger.SyncLoggers()
if err != nil {
return
}
}()

// Моки
mockClient := new(mocks.MockGrpcClient)
mockUtils := new(utils.MockUtils)

mockCityResponse := &gen.GetOneCityResponse{
City: &gen.City{
Id: 1,
Title: "Москва",
Entitle: "Moscow",
Description: "Some Desc",
Image: "test_image",
},
}

mockClient.On("GetOneCity", mock.Anything, mock.Anything, mock.Anything).Return(mockCityResponse, nil)
mockUtils.On("ConvertOneCityProtoToGo", mockCityResponse.City).Return(domain.City{
ID: 1,
Title: "Москва",
EnTitle: "Moscow",
Description: "Some Desc",
Image: "test_image",
}, nil)

// Инициализация обработчика
cityHandler := CityHandler{
client: mockClient,
utils: mockUtils,
}

// HTTP-запрос
req := httptest.NewRequest(http.MethodGet, "/city/test_city", nil)
req = mux.SetURLVars(req, map[string]string{"city": "test_city"})
w := httptest.NewRecorder()

// Вызов метода
cityHandler.GetOneCity(w, req)

// Проверка
result := w.Result()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(result.Body)

assert.Equal(t, http.StatusOK, result.StatusCode)
mockClient.AssertExpectations(t)
mockUtils.AssertExpectations(t)
}

func TestCityHandler_GetOneCity_GrpcError(t *testing.T) {
require.NoError(t, logger.InitLoggers())
defer func() {
err := logger.SyncLoggers()
if err != nil {
return
}
}()

// Моки
mockClient := new(mocks.MockGrpcClient)
grpcErr := status.Error(codes.Internal, "gRPC error")

mockClient.On("GetOneCity", mock.Anything, mock.Anything, mock.Anything).Return(&gen.GetOneCityResponse{}, grpcErr)

// Инициализация обработчика
cityHandler := CityHandler{
client: mockClient,
}

req := httptest.NewRequest(http.MethodGet, "/city/test_city", nil)
req = mux.SetURLVars(req, map[string]string{"city": "test_city"})
w := httptest.NewRecorder()

// Вызов метода
cityHandler.GetOneCity(w, req)

// Проверка
result := w.Result()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(result.Body)

assert.Equal(t, http.StatusInternalServerError, result.StatusCode)
mockClient.AssertExpectations(t)
}

func TestCityHandler_GetOneCity_ConversionError(t *testing.T) {
require.NoError(t, logger.InitLoggers())
defer func() {
err := logger.SyncLoggers()
if err != nil {
return
}
}()

// Моки
mockClient := new(mocks.MockGrpcClient)
mockUtils := new(utils.MockUtils)

mockCityResponse := &gen.GetOneCityResponse{
City: &gen.City{
Id: 1,
Title: "Москва",
Entitle: "Moscow",
Description: "Some Desc",
Image: "test_image",
},
}

mockClient.On("GetOneCity", mock.Anything, mock.Anything, mock.Anything).Return(mockCityResponse, nil)
mockUtils.On("ConvertOneCityProtoToGo", mockCityResponse.City).Return(nil, errors.New("conversion error"))

// Инициализация обработчика
cityHandler := CityHandler{
client: mockClient,
utils: mockUtils,
}

req := httptest.NewRequest(http.MethodGet, "/city/test_city", nil)
req = mux.SetURLVars(req, map[string]string{"city": "test_city"})
w := httptest.NewRecorder()

// Вызов метода
cityHandler.GetOneCity(w, req)

// Проверка
result := w.Result()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
return
}
}(result.Body)

assert.Equal(t, http.StatusInternalServerError, result.StatusCode)
mockClient.AssertExpectations(t)
mockUtils.AssertExpectations(t)
}
8 changes: 6 additions & 2 deletions internal/service/utils/mocks_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ func (m *MockUtils) ConvertSessionDataProtoToGo(sessionData *authGen.SessionData

func (m *MockUtils) ConvertAllCitiesProtoToGo(cities *cityGen.GetCitiesResponse) ([]*domain.City, error) {
args := m.Called(cities)
if res, ok := args.Get(0).([]*domain.City); ok {
return res, args.Error(1)
var trueRes []*domain.City
if res, ok := args.Get(0).(domain.AllCitiesResponse); ok {
for _, city := range res.Cities {

Check failure on line 67 in internal/service/utils/mocks_convert.go

View workflow job for this annotation

GitHub Actions / Lint

S1011: should replace loop with `trueRes = append(trueRes, res.Cities...)` (gosimple)
trueRes = append(trueRes, city)
}
return trueRes, args.Error(1)
}
return []*domain.City{}, args.Error(1)
}
Expand Down
Loading

0 comments on commit 3edd823

Please sign in to comment.