Skip to content

Commit 2c6445c

Browse files
Added testcase for CheckPluginRequest middleware function
1 parent 2dd8a12 commit 2c6445c

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

server/plugin/api_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,61 @@ func TestPlugin_ServeHTTP(t *testing.T) {
132132
}
133133
}
134134

135+
func TestCheckPluginRequest(t *testing.T) {
136+
tests := []struct {
137+
name string
138+
headers map[string]string
139+
setup func()
140+
assertions func(t *testing.T, rec *httptest.ResponseRecorder)
141+
}{
142+
{
143+
name: "Missing Mattermost-Plugin-ID header",
144+
headers: map[string]string{},
145+
setup: func() {},
146+
assertions: func(t *testing.T, rec *httptest.ResponseRecorder) {
147+
assert.Equal(t, http.StatusUnauthorized, rec.Result().StatusCode)
148+
body, _ := io.ReadAll(rec.Body)
149+
assert.Equal(t, "Not authorized\n", string(body))
150+
},
151+
},
152+
{
153+
name: "Valid Mattermost-Plugin-ID header",
154+
headers: map[string]string{
155+
"Mattermost-Plugin-ID": "validPluginID",
156+
},
157+
setup: func() {},
158+
assertions: func(t *testing.T, rec *httptest.ResponseRecorder) {
159+
assert.Equal(t, http.StatusOK, rec.Result().StatusCode)
160+
body, _ := io.ReadAll(rec.Body)
161+
assert.Equal(t, "Success\n", string(body))
162+
},
163+
},
164+
}
165+
for _, tc := range tests {
166+
t.Run(tc.name, func(t *testing.T) {
167+
tc.setup()
168+
169+
nextHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
170+
w.WriteHeader(http.StatusOK)
171+
_, err := w.Write([]byte("Success\n"))
172+
assert.NoError(t, err)
173+
})
174+
175+
handler := checkPluginRequest(nextHandler)
176+
177+
req := httptest.NewRequest(http.MethodGet, "/test", nil)
178+
for key, value := range tc.headers {
179+
req.Header.Set(key, value)
180+
}
181+
rec := httptest.NewRecorder()
182+
183+
handler(rec, req)
184+
185+
tc.assertions(t, rec)
186+
})
187+
}
188+
}
189+
135190
func TestGetToken(t *testing.T) {
136191
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t)
137192
p := getPluginTest(mockAPI, mockKvStore)

0 commit comments

Comments
 (0)