diff --git a/server/api/api_test.go b/server/api/api_test.go index faca9f9f..2f6e37c2 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,120 @@ func TestSetLink(t *testing.T) { }) } } + +func TestGetLink(t *testing.T) { + for _, tc := range []struct { + name string + prevLinks []autolink.Autolink + autoLinkName string + expectStatus int + expectReturn string + }{ + { + name: "get the autolink", + autoLinkName: "test", + prevLinks: []autolink.Autolink{{ + Name: "test", + Pattern: ".*1", + Template: "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", + prevLinks: []autolink.Autolink{{ + Name: "test1", + Pattern: ".*1", + Template: "test", + }}, + expectStatus: http.StatusInternalServerError, + expectReturn: "{\"error\":\"An internal error has occurred. Check app server logs for details.\",\"details\":\"no autolink found with name test\"}", + }, + } { + 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.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) { + for _, tc := range []struct { + name string + prevLinks []autolink.Autolink + autoLinkName string + expectStatus int + }{ + { + name: "delete the autolink", + autoLinkName: "test", + prevLinks: []autolink.Autolink{{ + Name: "test", + Pattern: ".*1", + Template: "test", + }}, + expectStatus: http.StatusOK, + }, + { + name: "not found", + autoLinkName: "test", + prevLinks: []autolink.Autolink{{ + Name: "test1", + Pattern: ".*1", + Template: "test", + }}, + expectStatus: http.StatusNotModified, + }, + } { + 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", 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) + + require.Equal(t, tc.expectStatus, w.Code) + }) + } +} diff --git a/server/autolinkclient/client_test.go b/server/autolinkclient/client_test.go index 3d25e625..c8f219f4 100644 --- a/server/autolinkclient/client_test.go +++ b/server/autolinkclient/client_test.go @@ -1,7 +1,9 @@ package autolinkclient import ( + "io/ioutil" "net/http" + "strings" "testing" "github.com/mattermost/mattermost-server/v6/plugin/plugintest" @@ -53,3 +55,45 @@ func TestAddAutolinksErr(t *testing.T) { err := client.Add(autolink.Autolink{}) require.Error(t, err) } + +func TestDeleteAutolinks(t *testing.T) { + mockPluginAPI := &plugintest.API{} + + mockPluginAPI.On("PluginHTTP", mock.AnythingOfType("*http.Request")).Return(&http.Response{StatusCode: http.StatusOK, Body: http.NoBody}) + + client := NewClientPlugin(mockPluginAPI) + err := client.Delete("") + require.Nil(t, err) +} + +func TestDeleteAutolinksErr(t *testing.T) { + mockPluginAPI := &plugintest.API{} + + mockPluginAPI.On("PluginHTTP", mock.AnythingOfType("*http.Request")).Return(nil) + + client := NewClientPlugin(mockPluginAPI) + err := client.Delete("") + require.Error(t, err) +} + +func TestGetAutolinks(t *testing.T) { + mockPluginAPI := &plugintest.API{} + + r := ioutil.NopCloser(strings.NewReader("{}")) + + mockPluginAPI.On("PluginHTTP", mock.AnythingOfType("*http.Request")).Return(&http.Response{StatusCode: http.StatusOK, Body: r}) + + client := NewClientPlugin(mockPluginAPI) + _, err := client.Get("") + require.Nil(t, err) +} + +func TestGetAutolinksErr(t *testing.T) { + mockPluginAPI := &plugintest.API{} + + mockPluginAPI.On("PluginHTTP", mock.AnythingOfType("*http.Request")).Return(nil) + + client := NewClientPlugin(mockPluginAPI) + _, err := client.Get("") + require.Error(t, err) +}