Skip to content

Commit

Permalink
[MI-3267]:Added test cases for PR mattermost-community#213 'Add CRUD …
Browse files Browse the repository at this point in the history
…operations'
  • Loading branch information
Kshitij-Katiyar committed Jul 6, 2023
1 parent 0a0cf02 commit 78c7e8b
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
119 changes: 119 additions & 0 deletions server/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package api
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -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)
})
}
}
44 changes: 44 additions & 0 deletions server/autolinkclient/client_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package autolinkclient

import (
"io/ioutil"
"net/http"
"strings"
"testing"

"github.com/mattermost/mattermost-server/v6/plugin/plugintest"
Expand Down Expand Up @@ -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)
}

0 comments on commit 78c7e8b

Please sign in to comment.