github.com/k0marov/go-socnet@v0.0.0-20220715154813-90d07867c782/features/profiles/delivery/http/handlers/displayers_test.go (about)

     1  package handlers_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/k0marov/go-socnet/core/general/client_errors"
     7  	"github.com/k0marov/go-socnet/core/general/core_entities"
     8  	"github.com/k0marov/go-socnet/core/general/core_values"
     9  	helpers "github.com/k0marov/go-socnet/core/helpers/http_test_helpers"
    10  	. "github.com/k0marov/go-socnet/core/helpers/test_helpers"
    11  	"net/http"
    12  	"net/http/httptest"
    13  	"testing"
    14  
    15  	"github.com/k0marov/go-socnet/features/profiles/delivery/http/responses"
    16  
    17  	"github.com/k0marov/go-socnet/features/profiles/delivery/http/handlers"
    18  	"github.com/k0marov/go-socnet/features/profiles/domain/entities"
    19  
    20  	"github.com/go-chi/chi/v5"
    21  )
    22  
    23  func TestGetMeHandler(t *testing.T) {
    24  	authUser := RandomAuthUser()
    25  	user := core_entities.UserFromAuth(authUser)
    26  	createRequestWithAuth := func() *http.Request {
    27  		return helpers.AddAuthDataToRequest(helpers.CreateRequest(nil), authUser)
    28  	}
    29  	helpers.BaseTest401(t, handlers.NewGetMeHandler(nil))
    30  	t.Run("should return 200 and a profile if authentication details are provided via context", func(t *testing.T) {
    31  		wantedProfile := RandomContextedProfile()
    32  		getter := func(gotUser, caller core_values.UserId) (entities.ContextedProfile, error) {
    33  			if gotUser == user.Id && caller == user.Id {
    34  				return wantedProfile, nil
    35  			}
    36  			panic(fmt.Sprintf("called with user=%v", gotUser))
    37  		}
    38  
    39  		response := httptest.NewRecorder()
    40  		handlers.NewGetMeHandler(getter).ServeHTTP(response, createRequestWithAuth())
    41  
    42  		AssertJSONData(t, response, responses.NewProfileResponse(wantedProfile))
    43  	})
    44  	helpers.BaseTestServiceErrorHandling(t, func(wantErr error, response *httptest.ResponseRecorder) {
    45  		getter := func(core_values.UserId, core_values.UserId) (entities.ContextedProfile, error) {
    46  			return entities.ContextedProfile{}, wantErr
    47  		}
    48  		handlers.NewGetMeHandler(getter).ServeHTTP(response, createRequestWithAuth())
    49  	})
    50  }
    51  func createRequestWithId(userId core_values.UserId) *http.Request {
    52  	request := helpers.CreateRequest(nil)
    53  	ctx := chi.NewRouteContext()
    54  	ctx.URLParams.Add("id", userId)
    55  	request = request.WithContext(context.WithValue(request.Context(), chi.RouteCtxKey, ctx))
    56  	return request
    57  }
    58  
    59  func TestGetByIdHandler(t *testing.T) {
    60  	helpers.BaseTest401(t, handlers.NewGetByIdHandler(nil))
    61  	t.Run("happy case", func(t *testing.T) {
    62  		targetId := RandomString()
    63  		caller := RandomAuthUser()
    64  		randomProfile := RandomContextedProfile()
    65  		profileGetter := func(target, callerId core_values.UserId) (entities.ContextedProfile, error) {
    66  			if target == targetId && callerId == caller.Id {
    67  				return randomProfile, nil
    68  			}
    69  			panic("called with unexpected arguments")
    70  		}
    71  
    72  		request := helpers.AddAuthDataToRequest(createRequestWithId(targetId), caller)
    73  		response := httptest.NewRecorder()
    74  
    75  		handlers.NewGetByIdHandler(profileGetter).ServeHTTP(response, request)
    76  		AssertJSONData(t, response, responses.NewProfileResponse(randomProfile))
    77  	})
    78  	t.Run("error case - id is not provided", func(t *testing.T) {
    79  		response := httptest.NewRecorder()
    80  		request := helpers.AddAuthDataToRequest(helpers.CreateRequest(nil), RandomAuthUser())
    81  		handlers.NewGetByIdHandler(nil).ServeHTTP(response, request)
    82  		AssertClientError(t, response, client_errors.IdNotProvided)
    83  	})
    84  	helpers.BaseTestServiceErrorHandling(t, func(err error, rr *httptest.ResponseRecorder) {
    85  		getter := func(target, caller core_values.UserId) (entities.ContextedProfile, error) {
    86  			return entities.ContextedProfile{}, err
    87  		}
    88  		request := helpers.AddAuthDataToRequest(createRequestWithId(RandomString()), RandomAuthUser())
    89  		handlers.NewGetByIdHandler(getter).ServeHTTP(rr, request)
    90  	})
    91  }
    92  
    93  func TestFollowsHandler(t *testing.T) {
    94  	caller := RandomAuthUser()
    95  	helpers.BaseTest401(t, handlers.NewGetFollowsHandler(nil))
    96  	t.Run("should return 200 and a list of profiles if profile with given id exists", func(t *testing.T) {
    97  		randomId := RandomString()
    98  		randomProfiles := []entities.ContextedProfile{RandomContextedProfile(), RandomContextedProfile()}
    99  		followsGetter := func(userId, callerId core_values.UserId) ([]entities.ContextedProfile, error) {
   100  			if userId == randomId && callerId == caller.Id {
   101  				return randomProfiles, nil
   102  			}
   103  			panic("called with unexpected arguments")
   104  		}
   105  
   106  		request := helpers.AddAuthDataToRequest(createRequestWithId(randomId), caller)
   107  		response := httptest.NewRecorder()
   108  
   109  		handlers.NewGetFollowsHandler(followsGetter).ServeHTTP(response, request)
   110  
   111  		AssertJSONData(t, response, responses.NewProfilesResponse(randomProfiles))
   112  	})
   113  	t.Run("error case - id is not provided", func(t *testing.T) {
   114  		response := httptest.NewRecorder()
   115  		request := helpers.AddAuthDataToRequest(helpers.CreateRequest(nil), RandomAuthUser())
   116  		handlers.NewGetFollowsHandler(nil).ServeHTTP(response, request)
   117  		AssertClientError(t, response, client_errors.IdNotProvided)
   118  	})
   119  	helpers.BaseTestServiceErrorHandling(t, func(err error, rr *httptest.ResponseRecorder) {
   120  		getter := func(userId, callerId core_values.UserId) ([]entities.ContextedProfile, error) {
   121  			return nil, err
   122  		}
   123  		request := helpers.AddAuthDataToRequest(createRequestWithId("42"), RandomAuthUser())
   124  		handlers.NewGetFollowsHandler(getter).ServeHTTP(rr, request)
   125  	})
   126  }