mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Enhance tests for Portainer client user authentication
This commit is contained in:
parent
1efa02abf3
commit
a98dbfdda2
@ -9,43 +9,84 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestClientAuthenticates(t *testing.T) {
|
func Test_portainerClientImp_AuthenticateUser(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
type fields struct {
|
||||||
var body map[string]interface{}
|
server *httptest.Server
|
||||||
err := readRequestBodyAsJSON(req, &body)
|
}
|
||||||
|
type args struct {
|
||||||
|
options AuthenticateUserOptions
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
fields fields
|
||||||
|
args args
|
||||||
|
wantToken string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "valid username and password authenticates (happy path)",
|
||||||
|
fields: fields{
|
||||||
|
server: httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
assert.Equal(t, req.Method, http.MethodPost)
|
||||||
|
assert.Equal(t, req.RequestURI, "/api/auth")
|
||||||
|
|
||||||
assert.Equal(t, req.Method, http.MethodPost)
|
var body map[string]interface{}
|
||||||
assert.Equal(t, req.RequestURI, "/api/auth")
|
err := readRequestBodyAsJSON(req, &body)
|
||||||
assert.NotNil(t, req.Header["Content-Type"])
|
assert.Nil(t, err)
|
||||||
assert.NotNil(t, req.Header["Content-Type"][0])
|
|
||||||
assert.Equal(t, req.Header["Content-Type"][0], "application/json")
|
|
||||||
assert.NotNil(t, req.Header["User-Agent"])
|
|
||||||
assert.NotNil(t, req.Header["User-Agent"][0])
|
|
||||||
assert.Equal(t, req.Header["User-Agent"][0], "GE007")
|
|
||||||
assert.Nil(t, err)
|
|
||||||
assert.NotNil(t, body["Username"])
|
|
||||||
assert.Equal(t, body["Username"], "admin")
|
|
||||||
assert.NotNil(t, body["Password"])
|
|
||||||
assert.Equal(t, body["Password"], "a")
|
|
||||||
|
|
||||||
writeResponseBodyAsJSON(w, map[string]interface{}{
|
assert.NotNil(t, body["Username"])
|
||||||
"jwt": "somerandomtoken",
|
assert.Equal(t, body["Username"], "admin")
|
||||||
|
assert.NotNil(t, body["Password"])
|
||||||
|
assert.Equal(t, body["Password"], "a")
|
||||||
|
|
||||||
|
writeResponseBodyAsJSON(w, map[string]interface{}{
|
||||||
|
"jwt": "token",
|
||||||
|
})
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
options: AuthenticateUserOptions{
|
||||||
|
Username: "admin",
|
||||||
|
Password: "a",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantToken: "token",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid username and password does not authenticate",
|
||||||
|
fields: fields{
|
||||||
|
server: httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||||
|
writeResponseBodyAsJSON(w, map[string]interface{}{
|
||||||
|
"Err": "Invalid credentials",
|
||||||
|
"Details": "Unauthorized",
|
||||||
|
})
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
args: args{
|
||||||
|
options: AuthenticateUserOptions{
|
||||||
|
Username: "admin",
|
||||||
|
Password: "a",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
tt.fields.server.Start()
|
||||||
|
defer tt.fields.server.Close()
|
||||||
|
|
||||||
|
apiURL, _ := url.Parse(tt.fields.server.URL + "/api/")
|
||||||
|
|
||||||
|
n := &portainerClientImp{
|
||||||
|
httpClient: tt.fields.server.Client(),
|
||||||
|
url: apiURL,
|
||||||
|
}
|
||||||
|
|
||||||
|
gotToken, err := n.AuthenticateUser(tt.args.options)
|
||||||
|
assert.Equal(t, tt.wantErr, err != nil)
|
||||||
|
assert.Equal(t, tt.wantToken, gotToken)
|
||||||
})
|
})
|
||||||
}))
|
}
|
||||||
defer ts.Close()
|
|
||||||
|
|
||||||
apiURL, _ := url.Parse(ts.URL + "/api/")
|
|
||||||
|
|
||||||
customClient := NewClient(ts.Client(), Config{
|
|
||||||
URL: apiURL,
|
|
||||||
User: "admin",
|
|
||||||
Password: "a",
|
|
||||||
UserAgent: "GE007",
|
|
||||||
})
|
|
||||||
token, err := customClient.AuthenticateUser(AuthenticateUserOptions{
|
|
||||||
Username: "admin",
|
|
||||||
Password: "a",
|
|
||||||
})
|
|
||||||
assert.Nil(t, err)
|
|
||||||
assert.Equal(t, token, "somerandomtoken")
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user