-
Notifications
You must be signed in to change notification settings - Fork 7
/
user_test.go
126 lines (119 loc) · 3.69 KB
/
user_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package handler
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/camphor-/relaym-server/domain/entity"
"github.com/camphor-/relaym-server/domain/mock_repository"
"github.com/camphor-/relaym-server/domain/mock_spotify"
"github.com/camphor-/relaym-server/usecase"
"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
"github.com/labstack/echo/v4"
)
func TestUserHandler_GetMe(t *testing.T) {
t.Parallel()
tests := []struct {
name string
userID string
prepareMockUserRepo func(repo *mock_repository.MockUser)
prepareMockUserCli func(cli *mock_spotify.MockUser)
want *userRes
wantErr bool
wantCode int
}{
{
name: "正しくユーザが取得できる",
userID: "userID",
prepareMockUserRepo: func(repo *mock_repository.MockUser) {
repo.EXPECT().FindByID("userID").Return(&entity.User{
ID: "userID",
SpotifyUserID: "spotify_user_id",
DisplayName: "display_name",
}, nil)
},
prepareMockUserCli: func(cli *mock_spotify.MockUser) {
cli.EXPECT().GetMe(gomock.Any()).Return(&entity.SpotifyUser{
SpotifyUserID: "spotify_user_id",
DisplayName: "spotify_display_name",
Product: "premium",
}, nil)
},
want: &userRes{
ID: "userID",
URI: "spotify:user:spotify_user_id",
DisplayName: "display_name",
IsPremium: true,
},
wantErr: false,
wantCode: http.StatusOK,
},
{
name: "DBからユーザの取得に失敗したときはInternalServerError",
userID: "userID",
prepareMockUserRepo: func(repo *mock_repository.MockUser) {
repo.EXPECT().FindByID("userID").Return(nil, errors.New("unknown error"))
},
prepareMockUserCli: func(cli *mock_spotify.MockUser) {},
want: nil,
wantErr: true,
wantCode: http.StatusInternalServerError,
},
{
name: "SpotifyAPIからユーザの取得に失敗したときはInternalServerError",
userID: "userID",
prepareMockUserRepo: func(repo *mock_repository.MockUser) {
repo.EXPECT().FindByID("userID").Return(&entity.User{
ID: "userID",
SpotifyUserID: "spotify_user_id",
DisplayName: "display_name",
}, nil)
},
prepareMockUserCli: func(cli *mock_spotify.MockUser) {
cli.EXPECT().GetMe(gomock.Any()).Return(nil, errors.New("unknown error"))
},
want: nil,
wantErr: true,
wantCode: http.StatusInternalServerError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c = setToContext(c, tt.userID, nil)
// モックの準備
ctrl := gomock.NewController(t)
defer ctrl.Finish()
repo := mock_repository.NewMockUser(ctrl)
tt.prepareMockUserRepo(repo)
cli := mock_spotify.NewMockUser(ctrl)
tt.prepareMockUserCli(cli)
uc := usecase.NewUserUseCase(cli, repo)
h := &UserHandler{userUC: uc}
err := h.GetMe(c)
if (err != nil) != tt.wantErr {
t.Errorf("GetMe() error = %v, wantErr %v", err, tt.wantErr)
}
// ステータスコードのチェック
if er, ok := err.(*echo.HTTPError); (ok && er.Code != tt.wantCode) || (!ok && rec.Code != tt.wantCode) {
t.Errorf("GetMe() code = %d, want = %d", rec.Code, tt.wantCode)
}
if !tt.wantErr {
got := &userRes{}
err := json.Unmarshal(rec.Body.Bytes(), got)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(got, tt.want) {
t.Errorf("GetMe() diff = %v", cmp.Diff(got, tt.want))
}
}
})
}
}