Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
domenipavec committed Apr 14, 2022
1 parent 43f45ed commit 805c0d1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
32 changes: 28 additions & 4 deletions internal/pkg/api/clients_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/cloudradar-monitoring/rportcli/internal/pkg/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var clientsStub = []*models.Client{
Expand Down Expand Up @@ -99,10 +100,33 @@ func TestClientsList(t *testing.T) {
})

clientsResp, err := cl.Clients(context.Background(), NewPaginationWithLimit(ClientsLimitMax), NewFilters("name", "abc"))
assert.NoError(t, err)
if err != nil {
return
}
require.NoError(t, err)

assert.Equal(t, clientsStub, clientsResp.Data)
}

func TestClientGet(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Authorization")
assert.Equal(t, "Basic bG9nMTE2Njo1NjQzMjI=", authHeader)

assert.Equal(t, ClientsURL+"/test-client", r.URL.String())
jsonEnc := json.NewEncoder(rw)
e := jsonEnc.Encode(ClientResponse{Data: clientsStub[0]})
assert.NoError(t, e)
}))
defer srv.Close()

cl := New(srv.URL, &utils.StorageBasicAuth{
AuthProvider: func() (login, pass string, err error) {
login = "log1166"
pass = "564322"
return
},
})

client, err := cl.Client(context.Background(), "test-client")
require.NoError(t, err)

assert.Equal(t, clientsStub[0], client)
}
1 change: 1 addition & 0 deletions internal/pkg/api/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

type Filters map[string]string

// NewFilters constructs filters from key value pairs. Keys with empty values are ignored.
func NewFilters(keyValues ...string) Filters {
f := make(map[string]string)
for i := 0; 2*i+1 < len(keyValues); i++ {
Expand Down
23 changes: 23 additions & 0 deletions internal/pkg/api/filters_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package api

import (
"net/url"
"testing"

"github.com/stretchr/testify/assert"
)

func TestFilters(t *testing.T) {
filters := NewFilters(
"name", "johny",
"id", "",
"*", "*abc*",
)

q := url.Values{}
filters.Apply(q)

assert.Equal(t, q.Get("filter[name]"), "johny")
assert.False(t, q.Has("id"))
assert.Equal(t, q.Get("filter[*]"), "*abc*")
}

0 comments on commit 805c0d1

Please sign in to comment.