✅ Refactored token HTTP delivery tests
This commit is contained in:
parent
f381c107b8
commit
0f99ae6dcb
1
internal/token/delivery/http/testdata/TestHandler_ServeHTTP/action.golden
vendored
Executable file
1
internal/token/delivery/http/testdata/TestHandler_ServeHTTP/action.golden
vendored
Executable file
|
@ -0,0 +1 @@
|
||||||
|
{}
|
1
internal/token/delivery/http/testdata/TestHandler_ServeHTTP/introspect.golden
vendored
Executable file
1
internal/token/delivery/http/testdata/TestHandler_ServeHTTP/introspect.golden
vendored
Executable file
|
@ -0,0 +1 @@
|
||||||
|
{"me":"https://user.example.net/","client_id":"https://app.example.com/","scope":"create update delete","exp":1632443647,"iat":1632443147,"active":true}
|
1
internal/token/delivery/http/testdata/TestHandler_ServeHTTP/revocation.golden
vendored
Normal file
1
internal/token/delivery/http/testdata/TestHandler_ServeHTTP/revocation.golden
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{}
|
|
@ -1,140 +1,102 @@
|
||||||
package http_test
|
package http_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"source.toby3d.me/toby3d/auth/internal/common"
|
"source.toby3d.me/toby3d/auth/internal/common"
|
||||||
"source.toby3d.me/toby3d/auth/internal/domain"
|
"source.toby3d.me/toby3d/auth/internal/domain"
|
||||||
"source.toby3d.me/toby3d/auth/internal/profile"
|
"source.toby3d.me/toby3d/auth/internal/domain/scope"
|
||||||
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/token"
|
"source.toby3d.me/toby3d/auth/internal/token"
|
||||||
delivery "source.toby3d.me/toby3d/auth/internal/token/delivery/http"
|
delivery "source.toby3d.me/toby3d/auth/internal/token/delivery/http"
|
||||||
tokenrepo "source.toby3d.me/toby3d/auth/internal/token/repository/memory"
|
testutil "source.toby3d.me/toby3d/auth/internal/util/testing"
|
||||||
tokenucase "source.toby3d.me/toby3d/auth/internal/token/usecase"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Dependencies struct {
|
type Dependencies struct {
|
||||||
client *http.Client
|
|
||||||
config *domain.Config
|
config *domain.Config
|
||||||
profiles profile.Repository
|
|
||||||
sessions session.Repository
|
|
||||||
token *domain.Token
|
token *domain.Token
|
||||||
tokens token.Repository
|
|
||||||
tokenService token.UseCase
|
tokenService token.UseCase
|
||||||
|
profile *domain.Profile
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO(toby3d)
|
func TestHandler_ServeHTTP(t *testing.T) {
|
||||||
func TestExchange(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
func TestIntrospection(t *testing.T) {
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
deps := NewDependencies(t)
|
deps := NewDependencies(t)
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodPost, "https://app.example.com/introspect",
|
for name, tc := range map[string]struct {
|
||||||
strings.NewReader("token="+deps.token.AccessToken))
|
body string
|
||||||
req.Header.Set(common.HeaderAccept, common.MIMEApplicationJSON)
|
target string
|
||||||
req.Header.Set(common.HeaderContentType, common.MIMEApplicationForm)
|
}{
|
||||||
|
"introspect": {
|
||||||
|
target: "https://app.example.com/introspect",
|
||||||
|
body: `token=` + deps.token.AccessToken,
|
||||||
|
},
|
||||||
|
"revocation": {
|
||||||
|
target: "https://app.example.com/revocation",
|
||||||
|
body: `token=` + deps.token.AccessToken,
|
||||||
|
},
|
||||||
|
"action": {
|
||||||
|
target: "https://app.example.com/token",
|
||||||
|
body: `action=revoke&token=` + deps.token.AccessToken,
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
name, tc := name, tc
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
t.Run(name, func(t *testing.T) {
|
||||||
delivery.NewHandler(deps.tokenService, *deps.config).
|
|
||||||
ServeHTTP(w, req)
|
|
||||||
|
|
||||||
resp := w.Result()
|
|
||||||
|
|
||||||
if result := resp.StatusCode; result != http.StatusOK {
|
|
||||||
t.Errorf("%s %s = %d, want %d", req.Method, req.RequestURI, result, http.StatusOK)
|
|
||||||
}
|
|
||||||
|
|
||||||
result := new(delivery.TokenIntrospectResponse)
|
|
||||||
if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
deps.token.AccessToken = ""
|
|
||||||
|
|
||||||
if result.ClientID != deps.token.ClientID.String() ||
|
|
||||||
result.Me != deps.token.Me.String() ||
|
|
||||||
result.Scope != deps.token.Scope.String() {
|
|
||||||
t.Errorf("%s %s = %+v, want %+v", req.Method, req.RequestURI, result, deps.token)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRevocation(t *testing.T) {
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
deps := NewDependencies(t)
|
req := httptest.NewRequest(http.MethodPost, tc.target, strings.NewReader(tc.body))
|
||||||
|
|
||||||
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)
|
req.Header.Set(common.HeaderAccept, common.MIMEApplicationJSON)
|
||||||
|
req.Header.Set(common.HeaderContentType, common.MIMEApplicationForm)
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
delivery.NewHandler(deps.tokenService, *deps.config).
|
delivery.NewHandler(deps.tokenService, *deps.config).ServeHTTP(w, req)
|
||||||
ServeHTTP(w, req)
|
|
||||||
|
|
||||||
resp := w.Result()
|
resp := w.Result()
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
t.Errorf("%s %s = %d, want %d", req.Method, req.RequestURI, resp.StatusCode, http.StatusOK)
|
t.Errorf("%s %s = %d, want %d", req.Method, req.RequestURI, resp.StatusCode,
|
||||||
|
http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
expBody := []byte("{}") //nolint:ifshort
|
testutil.GoldenEqual(t, resp.Body)
|
||||||
if result := bytes.TrimSpace(body); !bytes.Equal(result, expBody) {
|
})
|
||||||
t.Errorf("%s %s = %s, want %s", req.Method, req.RequestURI, result, expBody)
|
|
||||||
}
|
|
||||||
|
|
||||||
result, err := deps.tokens.Get(context.Background(), deps.token.AccessToken)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if result.String() != deps.token.String() {
|
|
||||||
t.Errorf("Get(%+v) = %s, want %s", deps.token.AccessToken, result, deps.token)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDependencies(tb testing.TB) Dependencies {
|
func NewDependencies(tb testing.TB) Dependencies {
|
||||||
tb.Helper()
|
tb.Helper()
|
||||||
|
|
||||||
client := new(http.Client)
|
// WARN(toby3d): clients MUST NOT expect what any IndieAuth tokens is
|
||||||
|
// JWT or something hackable. It can be anything in string
|
||||||
|
// representation. Used static generated random data for safe golden
|
||||||
|
// output comparing.
|
||||||
|
tkn := &domain.Token{
|
||||||
|
CreatedAt: time.Unix(1632443147, 0),
|
||||||
|
Expiry: time.Unix(1632443647, 0),
|
||||||
|
ClientID: *domain.TestClientID(tb, "https://app.example.com/"),
|
||||||
|
Me: *domain.TestMe(tb, "https://user.example.net/"),
|
||||||
|
AccessToken: "BKIt4SVoUDqEpJtUhIONc2plu3iorUYy",
|
||||||
|
RefreshToken: "",
|
||||||
|
Scope: domain.Scopes{
|
||||||
|
scope.Create,
|
||||||
|
scope.Update,
|
||||||
|
scope.Delete,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
config := domain.TestConfig(tb)
|
config := domain.TestConfig(tb)
|
||||||
token := domain.TestToken(tb)
|
profile := domain.TestProfile(tb)
|
||||||
profiles := profilerepo.NewMemoryProfileRepository()
|
tokenService := token.NewStubTokenUseCase(tkn, profile, nil)
|
||||||
sessions := sessionrepo.NewMemorySessionRepository(*config)
|
|
||||||
tokens := tokenrepo.NewMemoryTokenRepository()
|
|
||||||
tokenService := tokenucase.NewTokenUseCase(tokenucase.Config{
|
|
||||||
Config: *config,
|
|
||||||
Profiles: profiles,
|
|
||||||
Sessions: sessions,
|
|
||||||
Tokens: tokens,
|
|
||||||
})
|
|
||||||
|
|
||||||
return Dependencies{
|
return Dependencies{
|
||||||
client: client,
|
profile: profile,
|
||||||
|
token: tkn,
|
||||||
config: config,
|
config: config,
|
||||||
profiles: profiles,
|
|
||||||
sessions: sessions,
|
|
||||||
token: token,
|
|
||||||
tokens: tokens,
|
|
||||||
tokenService: tokenService,
|
tokenService: tokenService,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user