Skip to content

Commit

Permalink
Merge pull request #9 from fantasticrabbit/setup-tests
Browse files Browse the repository at this point in the history
Setup tests
  • Loading branch information
fantasticrabbit committed Nov 11, 2021
2 parents 9a6b486 + 756ae55 commit 769ba24
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
2 changes: 0 additions & 2 deletions cmd/gettask.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ var taskCmd = &cobra.Command{
Subtasks: subtasksFlag,
}
t.WriteOut(t.GetJSON(t.BuildPath()))

},
}

Expand All @@ -38,5 +37,4 @@ func init() {
taskCmd.Flags().BoolP("custom", "c", false, "task id provided is a clickup custom task id")
taskCmd.Flags().BoolP("subtasks", "s", false, "include subtasks in output")
taskCmd.Flags().BoolP("file", "f", false, "output to file clickup_<taskID>.json")

}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ require (
github.com/pkg/browser v0.0.0-20201112035734-206646e67786
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.9.0
github.com/stretchr/testify v1.7.0
golang.org/x/oauth2 v0.0.0-20211005180243-6b3c2da341f1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand All @@ -29,4 +32,5 @@ require (
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
26 changes: 14 additions & 12 deletions internal/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,38 @@ import (
"github.com/spf13/viper"
)

var (
Client HTTPClient
)

//Requester interface needs an API path to request JSON data
type Requester interface {
BuildPath() string
GetJSON(string) []byte
WriteOut([]byte)
}

// HTTPClient interface
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}

func init() {
Client = &http.Client{}
}

//Gets JSON data for any struct that implements Requester interface
func getJSON(apiPath string) []byte {
token := viper.GetString("ctoken")
client := &http.Client{}

req, _ := http.NewRequest(http.MethodGet, apiPath, nil)

req.Header.Add("Authorization", token)
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
resp, err := Client.Do(req)
if err != nil {
log.Fatalln("Errored when sending request to the server")
}
defer resp.Body.Close()
resp_body, _ := ioutil.ReadAll(resp.Body)

return resp_body
// fileFlag := viper.GetBool("file")
// if !fileFlag {
// fmt.Println(string(resp_body))
// }
}

func (t TaskRequest) GetJSON(apiPath string) []byte {
return getJSON(apiPath)
}
29 changes: 29 additions & 0 deletions internal/requester_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package internal

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

"github.com/fantasticrabbit/ClickupCLI/mocks"
"github.com/stretchr/testify/assert"
)

func init() {
Client = &mocks.MockClient{}
}

func TestGetJSON(t *testing.T) {
json := `{"name":"Test Name","full_name":"test full name","owner":{"login": "admin"}}`
mocks.GetDoFunc = func(*http.Request) (*http.Response, error) {
r := ioutil.NopCloser(bytes.NewReader([]byte(json)))
return &http.Response{
StatusCode: 200,
Body: r,
}, nil
}
request := getJSON("http://localhost/testpath")
assert.NotNil(t, request)
assert.EqualValues(t, json, request)
}
18 changes: 18 additions & 0 deletions mocks/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package mocks

import "net/http"

var (
// GetDoFunc fetches the mock client's `Do` func
GetDoFunc func(req *http.Request) (*http.Response, error)
)

// MockClient is the mock client
type MockClient struct {
DoFunc func(req *http.Request) (*http.Response, error)
}

// Do is the mock client's `Do` func
func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
return GetDoFunc(req)
}

0 comments on commit 769ba24

Please sign in to comment.