Fixed ticket test with JWT middleware

This commit is contained in:
Maxim Lebedev 2022-02-01 23:26:30 +05:00
parent c4bd8ebd4d
commit 10eb8c252d
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 18 additions and 10 deletions

View File

@ -6,7 +6,6 @@ import (
"github.com/fasthttp/router" "github.com/fasthttp/router"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
http "github.com/valyala/fasthttp" http "github.com/valyala/fasthttp"
"golang.org/x/text/language" "golang.org/x/text/language"
"golang.org/x/text/message" "golang.org/x/text/message"
@ -29,7 +28,8 @@ func TestUpdate(t *testing.T) {
userRouter := router.New() userRouter := router.New()
// NOTE(toby3d): private resource // NOTE(toby3d): private resource
userRouter.GET(ticket.Resource.URL().EscapedPath(), func(ctx *http.RequestCtx) { userRouter.GET(ticket.Resource.URL().EscapedPath(), func(ctx *http.RequestCtx) {
ctx.SuccessString(common.MIMETextHTMLCharsetUTF8, ctx.SuccessString(
common.MIMETextHTMLCharsetUTF8,
`<link rel="token_endpoint" href="https://auth.example.org/token">`, `<link rel="token_endpoint" href="https://auth.example.org/token">`,
) )
}) })
@ -46,30 +46,38 @@ func TestUpdate(t *testing.T) {
userClient, _, userCleanup := httptest.New(t, userRouter.Handler) userClient, _, userCleanup := httptest.New(t, userRouter.Handler)
t.Cleanup(userCleanup) t.Cleanup(userCleanup)
router := router.New() r := router.New()
delivery.NewRequestHandler( delivery.NewRequestHandler(
ucase.NewTicketUseCase(ticketrepo.NewMemoryTicketRepository(new(sync.Map), config), userClient, config), ucase.NewTicketUseCase(ticketrepo.NewMemoryTicketRepository(new(sync.Map), config), userClient, config),
language.NewMatcher(message.DefaultCatalog.Languages()), config, language.NewMatcher(message.DefaultCatalog.Languages()), config,
).Register(router) ).Register(r)
client, _, cleanup := httptest.New(t, router.Handler) client, _, cleanup := httptest.New(t, r.Handler)
t.Cleanup(cleanup) t.Cleanup(cleanup)
req := httptest.NewRequest(http.MethodPost, "https://example.com/ticket", []byte( const requestURI string = "https://example.com/ticket"
req := httptest.NewRequest(http.MethodPost, requestURI, []byte(
`ticket=`+ticket.Ticket+ `ticket=`+ticket.Ticket+
`&resource=`+ticket.Resource.String()+ `&resource=`+ticket.Resource.String()+
`&subject=`+ticket.Subject.String(), `&subject=`+ticket.Subject.String(),
)) ))
defer http.ReleaseRequest(req) defer http.ReleaseRequest(req)
req.Header.SetContentType(common.MIMEApplicationForm) req.Header.SetContentType(common.MIMEApplicationForm)
domain.TestToken(t).SetAuthHeader(req)
resp := http.AcquireResponse() resp := http.AcquireResponse()
defer http.ReleaseResponse(resp) defer http.ReleaseResponse(resp)
require.NoError(t, client.Do(req, resp)) if err := client.Do(req, resp); err != nil {
assert.Condition(t, func() bool { t.Fatal(err)
return resp.StatusCode() == http.StatusOK || resp.StatusCode() == http.StatusAccepted }
}, "the ticket endpoint MUST return an HTTP 200 OK code or HTTP 202 Accepted")
if resp.StatusCode() != http.StatusOK && resp.StatusCode() != http.StatusAccepted {
t.Errorf("POST %s = %d, want %d or %d", requestURI, resp.StatusCode(), http.StatusOK,
http.StatusAccepted)
}
// TODO(toby3d): print the result as part of the debugging. Instead, you // TODO(toby3d): print the result as part of the debugging. Instead, you
// need to send or save the token to the recipient for later use. // need to send or save the token to the recipient for later use.
assert.NotNil(t, resp.Body()) assert.NotNil(t, resp.Body())