diff --git a/server/api/api.go b/server/api/api.go index df8ea444..40f77f12 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -144,7 +144,7 @@ func (h *Handler) deleteLink(w http.ResponseWriter, r *http.Request) { } } - status := http.StatusNotModified + status := http.StatusNotFound if found { if err := h.store.SaveLinks(links); err != nil { h.handleError(w, errors.Wrap(err, "unable to save the link")) diff --git a/server/api/api_test.go b/server/api/api_test.go index faca9f9f..2ade64c6 100644 --- a/server/api/api_test.go +++ b/server/api/api_test.go @@ -3,6 +3,8 @@ package api import ( "bytes" "encoding/json" + "fmt" + "io/ioutil" "net/http" "net/http/httptest" "testing" @@ -168,3 +170,113 @@ func TestSetLink(t *testing.T) { }) } } + +func TestGetLink(t *testing.T) { + prevLinks := []autolink.Autolink{{ + Name: "test", + Pattern: ".*1", + Template: "test", + }} + + for _, tc := range []struct { + name string + autoLinkName string + expectStatus int + expectReturn string + }{ + { + name: "get the autolink", + autoLinkName: "test", + expectStatus: http.StatusOK, + expectReturn: `{"Name":"test","Disabled":false,"Pattern":".*1","Template":"test","Scope":null,"WordMatch":false,"DisableNonWordPrefix":false,"DisableNonWordSuffix":false,"ProcessBotPosts":false}`, + }, + { + name: "not found", + autoLinkName: "test-1", + expectStatus: http.StatusInternalServerError, + expectReturn: `{"error":"An internal error has occurred. Check app server logs for details.","details":"no autolink found with name test-1"}`, + }, + } { + t.Run(tc.name, func(t *testing.T) { + var saved []autolink.Autolink + var saveCalled bool + + h := NewHandler( + &linkStore{ + prev: prevLinks, + saveCalled: &saveCalled, + saved: &saved, + }, + authorizeAll{}, + ) + + w := httptest.NewRecorder() + r, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/link?autolinkName=%s", tc.autoLinkName), nil) + require.NoError(t, err) + + r.Header.Set("Mattermost-Plugin-ID", "testfrom") + r.Header.Set("Mattermost-User-ID", "testuser") + + h.ServeHTTP(w, r) + + respBody, err := ioutil.ReadAll(w.Body) + require.NoError(t, err) + + require.Equal(t, tc.expectStatus, w.Code) + require.Equal(t, tc.expectReturn, string(respBody)) + }) + } +} + +func TestDeleteLink(t *testing.T) { + autoLinkName := "test" + for _, tc := range []struct { + name string + prevLinks []autolink.Autolink + expectStatus int + }{ + { + name: "delete the autolink", + prevLinks: []autolink.Autolink{{ + Name: "test", + Pattern: ".*1", + Template: "test", + }}, + expectStatus: http.StatusOK, + }, + { + name: "not found", + prevLinks: []autolink.Autolink{{ + Name: "test1", + Pattern: ".*1", + Template: "test", + }}, + expectStatus: http.StatusNotFound, + }, + } { + t.Run(tc.name, func(t *testing.T) { + var saved []autolink.Autolink + var saveCalled bool + + h := NewHandler( + &linkStore{ + prev: tc.prevLinks, + saveCalled: &saveCalled, + saved: &saved, + }, + authorizeAll{}, + ) + + w := httptest.NewRecorder() + r, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("/api/v1/link?autolinkName=%s", autoLinkName), nil) + require.NoError(t, err) + + r.Header.Set("Mattermost-Plugin-ID", "testfrom") + r.Header.Set("Mattermost-User-ID", "testuser") + + h.ServeHTTP(w, r) + + require.Equal(t, tc.expectStatus, w.Code) + }) + } +} diff --git a/server/autolinkclient/client_test.go b/server/autolinkclient/client_test.go index 3d25e625..e0c62338 100644 --- a/server/autolinkclient/client_test.go +++ b/server/autolinkclient/client_test.go @@ -1,7 +1,10 @@ package autolinkclient import ( + "errors" + "io/ioutil" "net/http" + "strings" "testing" "github.com/mattermost/mattermost-server/v6/plugin/plugintest" @@ -53,3 +56,77 @@ func TestAddAutolinksErr(t *testing.T) { err := client.Add(autolink.Autolink{}) require.Error(t, err) } + +func TestDeleteAutolinks(t *testing.T) { + for _, tc := range []struct { + name string + setupAPI func(*plugintest.API) + err error + }{ + { + name: "delete the autolink", + setupAPI: func(api *plugintest.API) { + body := ioutil.NopCloser(strings.NewReader("{}")) + api.On("PluginHTTP", mock.AnythingOfType("*http.Request")).Return(&http.Response{StatusCode: http.StatusOK, Body: body}) + }, + }, + { + name: "got error", + setupAPI: func(api *plugintest.API) { + api.On("PluginHTTP", mock.AnythingOfType("*http.Request")).Return(nil) + }, + err: errors.New("not able to delete the autolink"), + }, + } { + t.Run(tc.name, func(t *testing.T) { + mockPluginAPI := &plugintest.API{} + tc.setupAPI(mockPluginAPI) + + client := NewClientPlugin(mockPluginAPI) + err := client.Delete("") + + if tc.err != nil { + require.Error(t, err) + } else { + require.Nil(t, err) + } + }) + } +} + +func TestGetAutolinks(t *testing.T) { + for _, tc := range []struct { + name string + setupAPI func(*plugintest.API) + err error + }{ + { + name: "get the autolink", + setupAPI: func(api *plugintest.API) { + body := ioutil.NopCloser(strings.NewReader("{}")) + api.On("PluginHTTP", mock.AnythingOfType("*http.Request")).Return(&http.Response{StatusCode: http.StatusOK, Body: body}) + }, + }, + { + name: "got error", + setupAPI: func(api *plugintest.API) { + api.On("PluginHTTP", mock.AnythingOfType("*http.Request")).Return(nil) + }, + err: errors.New("not able to get the autolink"), + }, + } { + t.Run(tc.name, func(t *testing.T) { + mockPluginAPI := &plugintest.API{} + tc.setupAPI(mockPluginAPI) + + client := NewClientPlugin(mockPluginAPI) + _, err := client.Get("") + + if tc.err != nil { + require.Error(t, err) + } else { + require.Nil(t, err) + } + }) + } +}