2021-09-20 15:45:54 +00:00
|
|
|
package http_test
|
|
|
|
|
|
|
|
import (
|
2022-02-01 19:32:57 +00:00
|
|
|
"bytes"
|
2021-09-20 15:45:54 +00:00
|
|
|
"context"
|
2023-01-14 21:27:37 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
2021-09-20 15:45:54 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
"github.com/goccy/go-json"
|
2021-09-29 20:35:39 +00:00
|
|
|
|
2022-03-13 10:58:34 +00:00
|
|
|
"source.toby3d.me/toby3d/auth/internal/common"
|
|
|
|
"source.toby3d.me/toby3d/auth/internal/domain"
|
|
|
|
"source.toby3d.me/toby3d/auth/internal/profile"
|
|
|
|
profilerepo "source.toby3d.me/toby3d/auth/internal/profile/repository/memory"
|
|
|
|
"source.toby3d.me/toby3d/auth/internal/session"
|
|
|
|
sessionrepo "source.toby3d.me/toby3d/auth/internal/session/repository/memory"
|
|
|
|
"source.toby3d.me/toby3d/auth/internal/ticket"
|
|
|
|
ticketrepo "source.toby3d.me/toby3d/auth/internal/ticket/repository/memory"
|
|
|
|
ticketucase "source.toby3d.me/toby3d/auth/internal/ticket/usecase"
|
|
|
|
"source.toby3d.me/toby3d/auth/internal/token"
|
|
|
|
delivery "source.toby3d.me/toby3d/auth/internal/token/delivery/http"
|
|
|
|
tokenrepo "source.toby3d.me/toby3d/auth/internal/token/repository/memory"
|
|
|
|
tokenucase "source.toby3d.me/toby3d/auth/internal/token/usecase"
|
2021-09-20 15:45:54 +00:00
|
|
|
)
|
|
|
|
|
2022-02-01 22:01:52 +00:00
|
|
|
type Dependencies struct {
|
2022-02-01 21:21:07 +00:00
|
|
|
client *http.Client
|
|
|
|
config *domain.Config
|
2022-02-25 15:34:20 +00:00
|
|
|
profiles profile.Repository
|
2022-02-01 21:21:07 +00:00
|
|
|
sessions session.Repository
|
|
|
|
tickets ticket.Repository
|
|
|
|
ticketService ticket.UseCase
|
|
|
|
token *domain.Token
|
|
|
|
tokens token.Repository
|
|
|
|
tokenService token.UseCase
|
|
|
|
}
|
|
|
|
|
2022-01-29 20:43:53 +00:00
|
|
|
/* TODO(toby3d)
|
|
|
|
func TestExchange(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2022-02-17 16:15:40 +00:00
|
|
|
func TestIntrospection(t *testing.T) {
|
2021-09-20 18:25:08 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-02-01 21:21:07 +00:00
|
|
|
deps := NewDependencies(t)
|
2021-09-20 18:25:08 +00:00
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "https://app.example.com/introspect",
|
|
|
|
strings.NewReader("token="+deps.token.AccessToken))
|
|
|
|
req.Header.Set(common.HeaderAccept, common.MIMEApplicationJSON)
|
|
|
|
req.Header.Set(common.HeaderContentType, common.MIMEApplicationForm)
|
2021-09-20 18:25:08 +00:00
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
delivery.NewHandler(deps.tokenService, deps.ticketService, deps.config).
|
|
|
|
Handler().
|
|
|
|
ServeHTTP(w, req)
|
2021-09-30 22:24:40 +00:00
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
resp := w.Result()
|
2022-02-01 19:32:57 +00:00
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
if result := resp.StatusCode; result != http.StatusOK {
|
|
|
|
t.Errorf("%s %s = %d, want %d", req.Method, req.RequestURI, result, http.StatusOK)
|
2022-02-01 19:32:57 +00:00
|
|
|
}
|
2021-09-20 18:25:08 +00:00
|
|
|
|
2022-02-17 16:15:40 +00:00
|
|
|
result := new(delivery.TokenIntrospectResponse)
|
2023-01-14 21:27:37 +00:00
|
|
|
if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
|
2022-02-01 19:17:22 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-02-01 21:21:07 +00:00
|
|
|
deps.token.AccessToken = ""
|
2022-02-01 19:32:57 +00:00
|
|
|
|
2022-02-17 16:15:40 +00:00
|
|
|
if result.ClientID != deps.token.ClientID.String() ||
|
|
|
|
result.Me != deps.token.Me.String() ||
|
|
|
|
result.Scope != deps.token.Scope.String() {
|
2023-01-14 21:27:37 +00:00
|
|
|
t.Errorf("%s %s = %+v, want %+v", req.Method, req.RequestURI, result, deps.token)
|
2022-02-01 19:32:57 +00:00
|
|
|
}
|
2021-09-20 18:25:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 15:45:54 +00:00
|
|
|
func TestRevocation(t *testing.T) {
|
2021-09-20 18:25:08 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-02-01 21:21:07 +00:00
|
|
|
deps := NewDependencies(t)
|
2021-09-20 15:45:54 +00:00
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "https://app.example.com/revocation",
|
|
|
|
strings.NewReader(`token=`+deps.token.AccessToken))
|
|
|
|
req.Header.Set(common.HeaderContentType, common.MIMEApplicationForm)
|
|
|
|
req.Header.Set(common.HeaderAccept, common.MIMEApplicationJSON)
|
2021-09-30 22:24:40 +00:00
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
delivery.NewHandler(deps.tokenService, deps.ticketService, deps.config).
|
|
|
|
Handler().
|
|
|
|
ServeHTTP(w, req)
|
2022-02-01 19:32:57 +00:00
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
resp := w.Result()
|
2021-09-20 15:45:54 +00:00
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2022-02-01 19:17:22 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-09-30 22:24:40 +00:00
|
|
|
|
2023-01-14 21:27:37 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
t.Errorf("%s %s = %d, want %d", req.Method, req.RequestURI, resp.StatusCode, http.StatusOK)
|
2022-02-01 19:32:57 +00:00
|
|
|
}
|
|
|
|
|
2022-12-26 14:09:34 +00:00
|
|
|
expBody := []byte("{}") //nolint:ifshort
|
2023-01-14 21:27:37 +00:00
|
|
|
if result := bytes.TrimSpace(body); !bytes.Equal(result, expBody) {
|
|
|
|
t.Errorf("%s %s = %s, want %s", req.Method, req.RequestURI, result, expBody)
|
2022-02-01 19:32:57 +00:00
|
|
|
}
|
2021-09-20 15:45:54 +00:00
|
|
|
|
2022-06-09 19:14:54 +00:00
|
|
|
result, err := deps.tokens.Get(context.Background(), deps.token.AccessToken)
|
2022-02-01 19:17:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-02-01 21:21:07 +00:00
|
|
|
if result.String() != deps.token.String() {
|
|
|
|
t.Errorf("Get(%+v) = %s, want %s", deps.token.AccessToken, result, deps.token)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 22:01:52 +00:00
|
|
|
func NewDependencies(tb testing.TB) Dependencies {
|
2022-02-01 21:21:07 +00:00
|
|
|
tb.Helper()
|
|
|
|
|
|
|
|
client := new(http.Client)
|
|
|
|
config := domain.TestConfig(tb)
|
|
|
|
token := domain.TestToken(tb)
|
2023-01-14 21:27:37 +00:00
|
|
|
profiles := profilerepo.NewMemoryProfileRepository()
|
|
|
|
sessions := sessionrepo.NewMemorySessionRepository(*config)
|
|
|
|
tickets := ticketrepo.NewMemoryTicketRepository(*config)
|
|
|
|
tokens := tokenrepo.NewMemoryTokenRepository()
|
2022-02-01 21:21:07 +00:00
|
|
|
ticketService := ticketucase.NewTicketUseCase(tickets, client, config)
|
2022-02-25 15:34:20 +00:00
|
|
|
tokenService := tokenucase.NewTokenUseCase(tokenucase.Config{
|
|
|
|
Config: config,
|
|
|
|
Profiles: profiles,
|
|
|
|
Sessions: sessions,
|
|
|
|
Tokens: tokens,
|
|
|
|
})
|
2022-02-01 21:21:07 +00:00
|
|
|
|
2022-02-01 22:01:52 +00:00
|
|
|
return Dependencies{
|
2022-02-01 21:21:07 +00:00
|
|
|
client: client,
|
|
|
|
config: config,
|
2022-02-25 15:34:20 +00:00
|
|
|
profiles: profiles,
|
2022-02-01 21:21:07 +00:00
|
|
|
sessions: sessions,
|
|
|
|
tickets: tickets,
|
|
|
|
ticketService: ticketService,
|
|
|
|
token: token,
|
|
|
|
tokens: tokens,
|
|
|
|
tokenService: tokenService,
|
2022-02-01 19:32:57 +00:00
|
|
|
}
|
2021-09-20 15:45:54 +00:00
|
|
|
}
|