2019-09-11 09:36:44 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package authorization
|
|
|
|
|
|
|
|
import (
|
2022-10-11 12:39:08 +01:00
|
|
|
"io"
|
2019-09-11 09:36:44 +01:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/errs2"
|
|
|
|
"storj.io/common/testcontext"
|
2019-09-11 09:36:44 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEndpoint_Run_httpSuccess(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, listener)
|
|
|
|
|
|
|
|
log := zaptest.NewLogger(t)
|
2019-11-13 15:32:44 +00:00
|
|
|
authDB := newTestAuthDB(t, ctx)
|
|
|
|
defer ctx.Check(authDB.Close)
|
|
|
|
|
|
|
|
service := NewService(log, authDB)
|
2019-09-11 09:36:44 +01:00
|
|
|
endpoint := NewEndpoint(log, service, listener)
|
|
|
|
require.NotNil(t, endpoint)
|
|
|
|
|
|
|
|
ctx.Go(func() error {
|
|
|
|
return errs2.IgnoreCanceled(endpoint.Run(ctx))
|
|
|
|
})
|
|
|
|
defer ctx.Check(endpoint.Close)
|
|
|
|
|
|
|
|
userID := "user@mail.test"
|
|
|
|
url := "http://" + listener.Addr().String() + "/v1/authorizations/" + userID
|
2021-05-14 16:05:42 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, nil)
|
2019-09-11 09:36:44 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, req)
|
|
|
|
|
|
|
|
client := http.Client{}
|
|
|
|
res, err := client.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, res)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusCreated, res.StatusCode)
|
|
|
|
|
2022-10-11 12:39:08 +01:00
|
|
|
tokenBytes, err := io.ReadAll(res.Body)
|
2019-09-11 09:36:44 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, tokenBytes)
|
|
|
|
require.NoError(t, res.Body.Close())
|
|
|
|
|
|
|
|
token, err := ParseToken(string(tokenBytes))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, token)
|
|
|
|
|
|
|
|
require.Equal(t, userID, token.UserID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEndpoint_Run_httpErrors(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, listener)
|
|
|
|
|
|
|
|
log := zaptest.NewLogger(t)
|
2019-11-13 15:32:44 +00:00
|
|
|
authDB := newTestAuthDB(t, ctx)
|
|
|
|
defer ctx.Check(authDB.Close)
|
|
|
|
|
|
|
|
service := NewService(log, authDB)
|
2019-09-11 09:36:44 +01:00
|
|
|
endpoint := NewEndpoint(log, service, listener)
|
|
|
|
require.NotNil(t, endpoint)
|
|
|
|
|
|
|
|
ctx.Go(func() error {
|
|
|
|
return errs2.IgnoreCanceled(endpoint.Run(ctx))
|
|
|
|
})
|
|
|
|
defer ctx.Check(endpoint.Close)
|
|
|
|
|
|
|
|
baseURL := "http://" + listener.Addr().String()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
userID string
|
|
|
|
urlPath string
|
|
|
|
httpMethod string
|
|
|
|
statusCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"missing user ID",
|
|
|
|
"",
|
|
|
|
"/v1/authorizations/",
|
|
|
|
http.MethodPut,
|
|
|
|
http.StatusUnprocessableEntity,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"unsupported http method (GET)",
|
|
|
|
"user@mail.test",
|
|
|
|
"/v1/authorizations/",
|
|
|
|
http.MethodGet,
|
|
|
|
http.StatusMethodNotAllowed,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"unsupported http method (PUT)",
|
|
|
|
"user@mail.test",
|
|
|
|
"/v1/authorizations/",
|
|
|
|
http.MethodPost,
|
|
|
|
http.StatusMethodNotAllowed,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"not found",
|
|
|
|
"",
|
|
|
|
"/",
|
|
|
|
http.MethodPut,
|
|
|
|
http.StatusNotFound,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Log(testCase.name)
|
|
|
|
url := baseURL + testCase.urlPath + testCase.userID
|
2021-05-14 16:05:42 +01:00
|
|
|
req, err := http.NewRequestWithContext(ctx, testCase.httpMethod, url, nil)
|
2019-09-11 09:36:44 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
client := http.Client{}
|
|
|
|
res, err := client.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, res)
|
|
|
|
require.NoError(t, res.Body.Close())
|
|
|
|
|
|
|
|
require.Equal(t, testCase.statusCode, res.StatusCode)
|
|
|
|
}
|
|
|
|
}
|