1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00
wakapi/middlewares/authenticate_test.go

104 lines
2.5 KiB
Go
Raw Normal View History

2020-11-08 14:46:12 +03:00
package middlewares
import (
"encoding/base64"
"fmt"
2021-10-11 10:58:29 +03:00
"net/http"
"net/url"
"testing"
2020-11-08 14:46:12 +03:00
"github.com/muety/wakapi/mocks"
"github.com/muety/wakapi/models"
"github.com/stretchr/testify/assert"
)
2021-10-11 10:58:29 +03:00
func TestAuthenticateMiddleware_tryGetUserByApiKeyHeader_Success(t *testing.T) {
2020-11-08 14:46:12 +03:00
testApiKey := "z5uig69cn9ut93n"
testToken := base64.StdEncoding.EncodeToString([]byte(testApiKey))
testUser := &models.User{ApiKey: testApiKey}
mockRequest := &http.Request{
Header: http.Header{
"Authorization": []string{fmt.Sprintf("Basic %s", testToken)},
},
}
userServiceMock := new(mocks.UserServiceMock)
userServiceMock.On("GetUserByKey", testApiKey).Return(testUser, nil)
sut := NewAuthenticateMiddleware(userServiceMock)
2020-11-08 14:46:12 +03:00
2021-10-11 10:58:29 +03:00
result, err := sut.tryGetUserByApiKeyHeader(mockRequest)
2020-11-08 14:46:12 +03:00
assert.Nil(t, err)
assert.Equal(t, testUser, result)
}
2021-10-11 10:58:29 +03:00
func TestAuthenticateMiddleware_tryGetUserByApiKeyHeader_Invalid(t *testing.T) {
2020-11-08 14:46:12 +03:00
testApiKey := "z5uig69cn9ut93n"
testToken := base64.StdEncoding.EncodeToString([]byte(testApiKey))
mockRequest := &http.Request{
Header: http.Header{
// 'Basic' prefix missing here
"Authorization": []string{fmt.Sprintf("%s", testToken)},
},
}
userServiceMock := new(mocks.UserServiceMock)
sut := NewAuthenticateMiddleware(userServiceMock)
2020-11-08 14:46:12 +03:00
2021-10-11 10:58:29 +03:00
result, err := sut.tryGetUserByApiKeyHeader(mockRequest)
2020-11-08 14:46:12 +03:00
assert.Error(t, err)
assert.Nil(t, result)
}
2021-10-11 10:58:29 +03:00
func TestAuthenticateMiddleware_tryGetUserByApiKeyQuery_Success(t *testing.T) {
testApiKey := "z5uig69cn9ut93n"
testUser := &models.User{ApiKey: testApiKey}
2021-10-11 12:38:55 +03:00
params := url.Values{}
params.Add("api_key", testApiKey)
2021-10-11 10:58:29 +03:00
mockRequest := &http.Request{
URL: &url.URL{
2021-10-11 12:38:55 +03:00
RawQuery: params.Encode(),
2021-10-11 10:58:29 +03:00
},
}
userServiceMock := new(mocks.UserServiceMock)
userServiceMock.On("GetUserByKey", testApiKey).Return(testUser, nil)
sut := NewAuthenticateMiddleware(userServiceMock)
result, err := sut.tryGetUserByApiKeyQuery(mockRequest)
assert.Nil(t, err)
assert.Equal(t, testUser, result)
}
func TestAuthenticateMiddleware_tryGetUserByApiKeyQuery_Invalid(t *testing.T) {
testApiKey := "z5uig69cn9ut93n"
2021-10-11 12:38:55 +03:00
params := url.Values{}
params.Add("token", testApiKey)
2021-10-11 10:58:29 +03:00
mockRequest := &http.Request{
URL: &url.URL{
2021-10-11 12:38:55 +03:00
RawQuery: params.Encode(),
2021-10-11 10:58:29 +03:00
},
}
userServiceMock := new(mocks.UserServiceMock)
sut := NewAuthenticateMiddleware(userServiceMock)
result, actualErr := sut.tryGetUserByApiKeyQuery(mockRequest)
assert.Error(t, actualErr)
assert.Equal(t, errEmptyKey, actualErr)
assert.Nil(t, result)
}
2020-11-08 14:46:12 +03:00
// TODO: somehow test cookie auth function