2020-11-30 15:15:07 +00:00
|
|
|
package apiclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-17 11:36:47 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-11-30 15:15:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestApiAuth(t *testing.T) {
|
|
|
|
log.SetLevel(log.TraceLevel)
|
|
|
|
|
|
|
|
mux, urlx, teardown := setup()
|
|
|
|
mux.HandleFunc("/decisions", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
testMethod(t, r, "GET")
|
|
|
|
if r.Header.Get("X-Api-Key") == "ixu" {
|
|
|
|
assert.Equal(t, r.URL.RawQuery, "ip=1.2.3.4")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte(`null`))
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
w.Write([]byte(`{"message":"access forbidden"}`))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
log.Printf("URL is %s", urlx)
|
|
|
|
apiURL, err := url.Parse(urlx + "/")
|
|
|
|
if err != nil {
|
2022-10-13 08:42:46 +00:00
|
|
|
t.Fatalf("parsing api url: %s", apiURL)
|
2020-11-30 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer teardown()
|
|
|
|
|
|
|
|
//ok no answer
|
|
|
|
auth := &APIKeyTransport{
|
|
|
|
APIKey: "ixu",
|
|
|
|
}
|
|
|
|
|
|
|
|
newcli, err := NewDefaultClient(apiURL, "v1", "toto", auth.Client())
|
|
|
|
if err != nil {
|
2022-10-13 08:42:46 +00:00
|
|
|
t.Fatalf("new api client: %s", err)
|
2020-11-30 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
alert := DecisionsListOpts{IPEquals: new(string)}
|
|
|
|
*alert.IPEquals = "1.2.3.4"
|
|
|
|
_, resp, err := newcli.Decisions.List(context.Background(), alert)
|
2021-03-17 11:36:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-30 15:15:07 +00:00
|
|
|
|
|
|
|
if resp.Response.StatusCode != http.StatusOK {
|
|
|
|
t.Errorf("Alerts.List returned status: %d, want %d", resp.Response.StatusCode, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
//ko bad token
|
|
|
|
auth = &APIKeyTransport{
|
|
|
|
APIKey: "bad",
|
|
|
|
}
|
|
|
|
|
|
|
|
newcli, err = NewDefaultClient(apiURL, "v1", "toto", auth.Client())
|
|
|
|
if err != nil {
|
2022-10-13 08:42:46 +00:00
|
|
|
t.Fatalf("new api client: %s", err)
|
2020-11-30 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, resp, err = newcli.Decisions.List(context.Background(), alert)
|
|
|
|
|
|
|
|
log.Infof("--> %s", err)
|
|
|
|
if resp.Response.StatusCode != http.StatusForbidden {
|
|
|
|
t.Errorf("Alerts.List returned status: %d, want %d", resp.Response.StatusCode, http.StatusOK)
|
|
|
|
}
|
|
|
|
assert.Contains(t, err.Error(), "API error: access forbidden")
|
|
|
|
//ko empty token
|
|
|
|
auth = &APIKeyTransport{}
|
|
|
|
newcli, err = NewDefaultClient(apiURL, "v1", "toto", auth.Client())
|
|
|
|
if err != nil {
|
2022-10-13 08:42:46 +00:00
|
|
|
t.Fatalf("new api client: %s", err)
|
2020-11-30 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 14:12:21 +00:00
|
|
|
_, _, err = newcli.Decisions.List(context.Background(), alert)
|
2021-03-17 11:36:47 +00:00
|
|
|
require.Error(t, err)
|
2020-11-30 15:15:07 +00:00
|
|
|
|
|
|
|
log.Infof("--> %s", err)
|
|
|
|
assert.Contains(t, err.Error(), "APIKey is empty")
|
|
|
|
|
|
|
|
}
|