crowdsec/pkg/cwapi/enroll_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

103 lines
2.5 KiB
Go

package cwapi
import (
"testing"
"github.com/dghubble/sling"
log "github.com/sirupsen/logrus"
)
func TestEnroll(t *testing.T) {
tests := []struct {
name string
givenAPICtx *ApiCtx
expectedErr bool
userID string
}{
{
name: "basic api user enroll",
expectedErr: false,
userID: "1234",
givenAPICtx: &ApiCtx{
ApiVersion: "v1",
EnrollPath: "enroll",
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),
},
},
{
name: "api signin unknown api PATH",
expectedErr: true,
userID: "1234",
givenAPICtx: &ApiCtx{
ApiVersion: "v1",
EnrollPath: "unknown_path",
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),
},
},
{
name: "api signin malformed response",
expectedErr: true,
userID: "1234",
givenAPICtx: &ApiCtx{
ApiVersion: "v1",
EnrollPath: "malformed_response",
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),
},
},
{
name: "api signin bad response",
expectedErr: true,
userID: "1234",
givenAPICtx: &ApiCtx{
ApiVersion: "v1",
EnrollPath: "bad_response",
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),
},
},
}
for _, test := range tests {
err := test.givenAPICtx.Enroll(test.userID)
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)
}
log.Printf("test '%s' : OK", test.name)
}
}