(t *testing.T)
| 379 | } |
| 380 | |
| 381 | func TestIsNotAuthenticated(t *testing.T) { |
| 382 | t.Parallel() |
| 383 | |
| 384 | ctx := context.Background() |
| 385 | conf, reg := pkg.NewFastRegistryWithMocks(t) |
| 386 | r := httprouterx.NewTestRouterPublic(t) |
| 387 | |
| 388 | reg.WithCSRFHandler(new(nosurfx.FakeCSRFHandler)) |
| 389 | h, _ := testhelpers.MockSessionCreateHandler(t, reg) |
| 390 | r.GET("/set", h) |
| 391 | r.GET("/public/with-callback", reg.SessionHandler().IsNotAuthenticated(send(http.StatusOK), send(http.StatusBadRequest))) |
| 392 | r.GET("/public/without-callback", reg.SessionHandler().IsNotAuthenticated(send(http.StatusOK), nil)) |
| 393 | ts := httptest.NewServer(r) |
| 394 | defer ts.Close() |
| 395 | |
| 396 | conf.MustSet(ctx, config.ViperKeyPublicBaseURL, ts.URL) |
| 397 | |
| 398 | sessionClient := testhelpers.NewClientWithCookies(t) |
| 399 | testhelpers.MockHydrateCookieClient(t, sessionClient, ts.URL+"/set") |
| 400 | |
| 401 | for k, tc := range []struct { |
| 402 | c *http.Client |
| 403 | call string |
| 404 | code int |
| 405 | }{ |
| 406 | { |
| 407 | c: sessionClient, |
| 408 | call: "/public/with-callback", |
| 409 | code: http.StatusBadRequest, |
| 410 | }, |
| 411 | { |
| 412 | c: http.DefaultClient, |
| 413 | call: "/public/with-callback", |
| 414 | code: http.StatusOK, |
| 415 | }, |
| 416 | |
| 417 | { |
| 418 | c: sessionClient, |
| 419 | call: "/public/without-callback", |
| 420 | code: http.StatusForbidden, |
| 421 | }, |
| 422 | { |
| 423 | c: http.DefaultClient, |
| 424 | call: "/public/without-callback", |
| 425 | code: http.StatusOK, |
| 426 | }, |
| 427 | } { |
| 428 | t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) { |
| 429 | res, err := tc.c.Get(ts.URL + tc.call) |
| 430 | require.NoError(t, err) |
| 431 | |
| 432 | assert.EqualValues(t, tc.code, res.StatusCode) |
| 433 | }) |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | func TestIsAuthenticated(t *testing.T) { |
| 438 | t.Parallel() |
nothing calls this directly
no test coverage detected