Welcome to mirror list, hosted at ThFree Co, Russian Federation.

handlers_test.go « handlers « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a1fa32462748d0c7b79343165e2bae71979a069e (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package handlers

import (
	"errors"
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"

	"github.com/golang/mock/gomock"
	"github.com/stretchr/testify/require"

	"gitlab.com/gitlab-org/gitlab-pages/internal/handlers/mock"
)

func TestNotHandleArtifactRequestReturnsFalse(t *testing.T) {
	mockCtrl := gomock.NewController(t)

	mockArtifact := mock.NewMockArtifact(mockCtrl)
	mockArtifact.EXPECT().
		TryMakeRequest(gomock.Any(), gomock.Any(), "", gomock.Any()).
		Return(false).
		Times(1)

	mockAuth := mock.NewMockAuth(mockCtrl)
	mockAuth.EXPECT().
		GetTokenIfExists(gomock.Any(), gomock.Any()).
		Return("", nil).
		Times(1)

	handlers := New(mockAuth, mockArtifact)

	result := httptest.NewRecorder()
	reqURL, err := url.Parse("/something")
	require.NoError(t, err)
	r := &http.Request{URL: reqURL}

	require.False(t, handlers.HandleArtifactRequest(result, r, mock.NewMockDomain(mockCtrl)))
}

func TestHandleArtifactRequestedReturnsTrue(t *testing.T) {
	mockCtrl := gomock.NewController(t)

	mockArtifact := mock.NewMockArtifact(mockCtrl)
	mockArtifact.EXPECT().
		TryMakeRequest(gomock.Any(), gomock.Any(), "", gomock.Any()).
		Return(true).
		Times(1)

	mockAuth := mock.NewMockAuth(mockCtrl)
	mockAuth.EXPECT().
		GetTokenIfExists(gomock.Any(), gomock.Any()).
		Return("", nil).
		Times(1)

	handlers := New(mockAuth, mockArtifact)

	result := httptest.NewRecorder()
	r := httptest.NewRequest(http.MethodGet, "/something", nil)

	require.True(t, handlers.HandleArtifactRequest(result, r, mock.NewMockDomain(mockCtrl)))
}

func TestNotFoundWithTokenIsNotHandled(t *testing.T) {
	mockCtrl := gomock.NewController(t)

	mockAuth := mock.NewMockAuth(mockCtrl)
	mockAuth.EXPECT().CheckResponseForInvalidToken(gomock.Any(), gomock.Any(), gomock.Any()).
		Return(false)

	handlers := New(mockAuth, nil)

	w := httptest.NewRecorder()
	reqURL, _ := url.Parse("/")
	r := &http.Request{URL: reqURL}
	response := &http.Response{StatusCode: http.StatusNotFound}
	handled := handlers.checkIfLoginRequiredOrInvalidToken(w, r, "token", mock.NewMockDomain(mockCtrl))(response)

	require.False(t, handled)
}

func TestForbiddenWithTokenIsNotHandled(t *testing.T) {
	cases := map[string]struct {
		StatusCode int
		Token      string
		Handled    bool
	}{
		"403 Forbidden with token": {
			http.StatusForbidden,
			"token",
			false,
		},
		"403 Forbidden with no token": {
			http.StatusForbidden,
			"",
			true,
		},
	}

	for tn, tc := range cases {
		t.Run(tn, func(t *testing.T) {
			mockCtrl := gomock.NewController(t)

			mockAuth := mock.NewMockAuth(mockCtrl)
			if tc.Token == "" {
				mockAuth.EXPECT().IsAuthSupported().Return(true)
				mockAuth.EXPECT().RequireAuth(gomock.Any(), gomock.Any(), gomock.Any()).Return(true)
			} else {
				mockAuth.EXPECT().CheckResponseForInvalidToken(gomock.Any(), gomock.Any(), gomock.Any()).
					Return(false)
			}

			handlers := New(mockAuth, nil)

			w := httptest.NewRecorder()
			r := httptest.NewRequest(http.MethodGet, "/", nil)
			response := &http.Response{StatusCode: tc.StatusCode}
			handled := handlers.checkIfLoginRequiredOrInvalidToken(w, r, tc.Token, mock.NewMockDomain(mockCtrl))(response)

			require.Equal(t, tc.Handled, handled)
		})
	}
}

func TestNotFoundWithoutTokenIsNotHandledWhenNotAuthSupport(t *testing.T) {
	mockCtrl := gomock.NewController(t)

	mockAuth := mock.NewMockAuth(mockCtrl)
	mockAuth.EXPECT().IsAuthSupported().Return(false)

	handlers := New(mockAuth, nil)

	w := httptest.NewRecorder()
	r := httptest.NewRequest(http.MethodGet, "/", nil)
	response := &http.Response{StatusCode: http.StatusNotFound}
	handled := handlers.checkIfLoginRequiredOrInvalidToken(w, r, "", mock.NewMockDomain(mockCtrl))(response)

	require.False(t, handled)
}

func TestNotFoundWithoutTokenIsHandled(t *testing.T) {
	mockCtrl := gomock.NewController(t)

	mockAuth := mock.NewMockAuth(mockCtrl)
	mockAuth.EXPECT().IsAuthSupported().Return(true)
	mockAuth.EXPECT().RequireAuth(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return(true)

	handlers := New(mockAuth, nil)

	w := httptest.NewRecorder()
	r := httptest.NewRequest(http.MethodGet, "/", nil)
	response := &http.Response{StatusCode: http.StatusNotFound}
	handled := handlers.checkIfLoginRequiredOrInvalidToken(w, r, "", mock.NewMockDomain(mockCtrl))(response)

	require.True(t, handled)
}

func TestInvalidTokenResponseIsHandled(t *testing.T) {
	mockCtrl := gomock.NewController(t)

	mockAuth := mock.NewMockAuth(mockCtrl)
	mockAuth.EXPECT().CheckResponseForInvalidToken(gomock.Any(), gomock.Any(), gomock.Any()).
		Return(true)

	handlers := New(mockAuth, nil)

	w := httptest.NewRecorder()
	r := httptest.NewRequest(http.MethodGet, "/", nil)
	response := &http.Response{StatusCode: http.StatusUnauthorized}
	handled := handlers.checkIfLoginRequiredOrInvalidToken(w, r, "token", mock.NewMockDomain(mockCtrl))(response)

	require.True(t, handled)
}

func TestHandleArtifactRequestButGetTokenFails(t *testing.T) {
	mockCtrl := gomock.NewController(t)

	mockArtifact := mock.NewMockArtifact(mockCtrl)
	mockArtifact.EXPECT().
		TryMakeRequest(gomock.Any(), gomock.Any(), "", gomock.Any()).
		Times(0)

	mockAuth := mock.NewMockAuth(mockCtrl)
	mockAuth.EXPECT().GetTokenIfExists(gomock.Any(), gomock.Any()).Return("", errors.New("error when retrieving token"))

	handlers := New(mockAuth, mockArtifact)

	result := httptest.NewRecorder()
	r := httptest.NewRequest(http.MethodGet, "/something", nil)

	require.True(t, handlers.HandleArtifactRequest(result, r, mock.NewMockDomain(mockCtrl)))
}