Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MI-3267]:Added test cases for PR #213 'Add CRUD operations' #4

Merged
merged 4 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
112 changes: 112 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,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)
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)

r.Header.Set("Mattermost-Plugin-ID", "testfrom")
r.Header.Set("Mattermost-User-ID", "testuser")
ayusht2810 marked this conversation as resolved.
Show resolved Hide resolved

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",
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
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)
})
}
}
77 changes: 77 additions & 0 deletions server/autolinkclient/client_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package autolinkclient

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

"github.com/mattermost/mattermost-server/v6/plugin/plugintest"
Expand Down Expand Up @@ -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)
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
},
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)
}
})
}
}