summaryrefslogtreecommitdiff
path: root/internal/suites/scenario_redirection_check_test.go
blob: 2587c350b7df30dae833c8510ace421cfafc4c9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package suites

import (
	"context"
	"log"
	"testing"
	"time"

	"github.com/stretchr/testify/suite"
)

type RedirectionCheckScenario struct {
	*RodSuite
}

func NewRedirectionCheckScenario() *RedirectionCheckScenario {
	return &RedirectionCheckScenario{
		RodSuite: NewRodSuite(""),
	}
}

func (s *RedirectionCheckScenario) SetupSuite() {
	browser, err := NewRodSession(RodSessionWithCredentials(s))
	if err != nil {
		log.Fatal(err)
	}

	s.RodSession = browser
}

func (s *RedirectionCheckScenario) TearDownSuite() {
	err := s.RodSession.Stop()

	if err != nil {
		log.Fatal(err)
	}
}

func (s *RedirectionCheckScenario) SetupTest() {
	s.Page = s.doCreateTab(s.T(), HomeBaseURL)
	s.verifyIsHome(s.T(), s.Page)
}

func (s *RedirectionCheckScenario) TearDownTest() {
	s.collectCoverage(s.Page)
	s.MustClose()
}

var redirectionAuthorizations = map[string]bool{
	// external website.
	"https://www.google.fr": false,
	// Not the right domain.
	"https://public.example.com.a:8080/secret.html": false,
	// Not https.
	"http://secure.example.com:8080/secret.html": false,
	// Domain handled by Authelia.
	"https://secure.example.com:8080/secret.html": true,
}

func (s *RedirectionCheckScenario) TestShouldRedirectOnLoginOnlyWhenDomainIsSafe() {
	ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
	defer func() {
		cancel()
		s.collectScreenshot(ctx.Err(), s.Page)
	}()

	s.doLoginAndRegisterTOTPThenLogout(s.T(), s.Context(ctx), "john", "password")

	for url, redirected := range redirectionAuthorizations {
		s.T().Run(url, func(t *testing.T) {
			s.doLoginSecondFactorTOTP(t, s.Context(ctx), "john", "password", false, url)

			if redirected {
				s.verifySecretAuthorized(t, s.Context(ctx))
			} else {
				s.verifyIsAuthenticatedPage(t, s.Context(ctx))
			}

			s.doLogout(t, s.Context(ctx))
		})
	}
}

var logoutRedirectionURLs = map[string]bool{
	// external website.
	"https://www.google.fr": false,
	// Not the right domain.
	"https://public.example-not-right.com:8080/index.html": false,
	// Not https.
	"http://public.example.com:8080/index.html": false,
	// Domain handled by Authelia.
	"https://public.example.com:8080/index.html": true,
}

func (s *RedirectionCheckScenario) TestShouldRedirectOnLogoutOnlyWhenDomainIsSafe() {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer func() {
		cancel()
		s.collectScreenshot(ctx.Err(), s.Page)
	}()

	for url, success := range logoutRedirectionURLs {
		s.T().Run(url, func(t *testing.T) {
			s.doLogoutWithRedirect(t, s.Context(ctx), url, !success)
		})
	}
}

func TestRedirectionCheckScenario(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping suite test in short mode")
	}

	suite.Run(t, NewRedirectionCheckScenario())
}