From 623bf4c2693bcdda76b5523881f4430f48e1e582 Mon Sep 17 00:00:00 2001 From: Pavan Gorti Date: Fri, 21 May 2021 11:01:04 +0530 Subject: [PATCH] chore(tests): add tests for mastodon service --- service/mastodon/mastodon.go | 20 ++++++-- service/mastodon/mastodon_test.go | 81 +++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 service/mastodon/mastodon_test.go diff --git a/service/mastodon/mastodon.go b/service/mastodon/mastodon.go index abdd407..9315b66 100644 --- a/service/mastodon/mastodon.go +++ b/service/mastodon/mastodon.go @@ -19,6 +19,20 @@ type Mastodon struct { Message string } +// HTTPClient interface +type HTTPClient interface { + Do(req *http.Request) (*http.Response, error) +} + +var Client HTTPClient + +func initialize() { + // create a new http client + Client = &http.Client{ + Timeout: 10 * time.Second, + } +} + // Send parse values from *cli.context and return *cli.Command // and sets a status message for mastodon. func Send() *cli.Command { @@ -62,6 +76,7 @@ func Send() *cli.Command { }, }, Action: func(ctx *cli.Context) error { + initialize() endPointURL := "https://" + mastodonOpts.ServerURL + "/api/v1/statuses/" // Create a Bearer string by appending string access token @@ -98,9 +113,8 @@ func sendMastodon(url string, token string, msg string) error { req.Header.Set("Authorization", token) req.Header.Set("Content-Type", "application/json; charset=UTF-8") - // create a new http client and send request to server - c := &http.Client{Timeout: 10 * time.Second} - resp, err := c.Do(req) + // send request to server + resp, err := Client.Do(req) if err != nil { return err } diff --git a/service/mastodon/mastodon_test.go b/service/mastodon/mastodon_test.go new file mode 100644 index 0000000..5369a41 --- /dev/null +++ b/service/mastodon/mastodon_test.go @@ -0,0 +1,81 @@ +package mastodon + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" +) + +const url, token = "server-url", "token" + +// MockClient is the mock client +type MockClient struct { + MockDo func(req *http.Request) (*http.Response, error) +} + +// Do function implements HTTPClient +func (m *MockClient) Do(req *http.Request) (*http.Response, error) { + return m.MockDo(req) +} + +func TestSendMessage_Success(t *testing.T) { + successResponse, _ := json.Marshal(map[string]interface{}{ + "success": true, + }) + + r := ioutil.NopCloser(bytes.NewReader(successResponse)) + + Client = &MockClient{ + MockDo: func(req *http.Request) (*http.Response, error) { + assert.Equal(t, url, req.URL.Path) + assert.Equal(t, token, req.Header.Get("Authorization")) + + return &http.Response{ + StatusCode: 200, + Body: r, + }, nil + }, + } + + m := Mastodon{ + ServerURL: url, + Token: token, + Message: "message", + } + + err := sendMastodon(m.ServerURL, m.Token, m.Message) + assert.Nil(t, err) +} + +func TestSendMessage_Failure(t *testing.T) { + successResponse, _ := json.Marshal(map[string]interface{}{ + "error": true, + }) + + r := ioutil.NopCloser(bytes.NewReader(successResponse)) + + Client = &MockClient{ + MockDo: func(req *http.Request) (*http.Response, error) { + assert.Equal(t, url, req.URL.Path) + assert.Equal(t, token, req.Header.Get("Authorization")) + + return &http.Response{ + StatusCode: 400, + Body: r, + }, nil + }, + } + + m := Mastodon{ + ServerURL: url, + Token: token, + Message: "message", + } + + err := sendMastodon(m.ServerURL, m.Token, m.Message) + assert.NotNil(t, err) +}