crowdsec/pkg/cwapi/pull_test.go
AlteredCoder 851ad300cb
Add unitest in pkg/acquisition and pkg/cwapi (#145)
* ci for acquisition and cwapi

* update README


Co-authored-by: AlteredCoder <AlteredCoder>
2020-07-27 12:18:55 +02:00

81 lines
1.9 KiB
Go

package cwapi
import (
"encoding/json"
"testing"
"github.com/dghubble/sling"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestPullTop(t *testing.T) {
tests := []struct {
name string
givenAPICtx *ApiCtx
expectedErr bool
expectedResult string
}{
{
name: "basic api pull",
expectedErr: false,
givenAPICtx: &ApiCtx{
ApiVersion: "v1",
PullPath: "pull",
SigninPath: "signin",
BaseURL: "https://my_testendpoint.com",
CfgUser: "machine_id",
CfgPassword: "machine_password",
Creds: ApiCreds{
User: "machine_id",
Password: "machine_password",
Profile: "crowdsec/test1,crowdsec/test2",
},
Http: sling.New().Client(newMockClient()).Base(apiBaseURL),
},
expectedResult: pullResponse,
},
{
name: "basic api pull return non 200 Code",
expectedErr: true,
givenAPICtx: &ApiCtx{
ApiVersion: "v1",
PullPath: "unknown_path",
SigninPath: "signin",
BaseURL: "https://my_testendpoint.com",
CfgUser: "machine_id",
CfgPassword: "machine_password",
Creds: ApiCreds{
User: "machine_id",
Password: "machine_password",
Profile: "crowdsec/test1,crowdsec/test2",
},
Http: sling.New().Client(newMockClient()).Base(apiBaseURL),
},
expectedResult: pullResponse,
},
}
for _, test := range tests {
apiResponse := &PullResp{}
err := json.Unmarshal([]byte(test.expectedResult), apiResponse)
if err != nil {
t.Fatalf("unable to unmarshall expected result : %s", err)
}
result, err := test.givenAPICtx.PullTop()
if !test.expectedErr && err != nil {
t.Fatalf("test '%s' failed : %s", test.name, err)
}
if test.expectedErr && err == nil {
t.Fatalf("test '%s' should return an err", test.name)
}
if test.expectedErr {
continue
}
assert.Equal(t, apiResponse.Body, result)
log.Printf("test '%s' : OK", test.name)
}
}