github.com/oinume/lekcije@v0.0.0-20231017100347-5b4c5eb6ab24/backend/interface/http/me_test.go (about)

     1  package http
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/oinume/lekcije/backend/context_data"
    14  )
    15  
    16  func TestGetMe(t *testing.T) {
    17  	//t.Parallel()
    18  	a := assert.New(t)
    19  	r := require.New(t)
    20  	var accessLog, appLog bytes.Buffer
    21  	s := newTestServer(t, &accessLog, &appLog)
    22  
    23  	req, err := http.NewRequest("GET", "/me", nil)
    24  	r.NoError(err)
    25  	user := helper.CreateRandomUser(t)
    26  	ctx := context_data.SetLoggedInUser(req.Context(), user)
    27  	ctx = context_data.SetTrackingID(ctx, fmt.Sprint(user.ID))
    28  	req = req.WithContext(ctx)
    29  
    30  	w := httptest.NewRecorder()
    31  	http.HandlerFunc(s.getMeHandler()).ServeHTTP(w, req)
    32  
    33  	a.Equal(http.StatusOK, w.Code)
    34  	a.Contains(w.Body.String(), "フォローしている講師")
    35  }
    36  
    37  func TestGetMeSetting(t *testing.T) {
    38  	//t.Parallel()
    39  	a := assert.New(t)
    40  	r := require.New(t)
    41  	var accessLog, appLog bytes.Buffer
    42  	s := newTestServer(t, &accessLog, &appLog)
    43  
    44  	req, err := http.NewRequest("GET", "/me/setting", nil)
    45  	r.NoError(err)
    46  	user := helper.CreateRandomUser(t)
    47  	ctx := context_data.SetLoggedInUser(req.Context(), user)
    48  	ctx = context_data.SetTrackingID(ctx, fmt.Sprint(user.ID))
    49  	req = req.WithContext(ctx)
    50  
    51  	w := httptest.NewRecorder()
    52  	http.HandlerFunc(s.getMeHandler()).ServeHTTP(w, req)
    53  
    54  	a.Equal(http.StatusOK, w.Code)
    55  	a.Contains(w.Body.String(), "フォローしている講師")
    56  }