summaryrefslogtreecommitdiff
path: root/internal/suites/scenario_two_factor_webauthn_test.go
blob: 9f2bce7fac3de0c76ea02411b7a3c70f992319a1 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package suites

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

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

type TwoFactorWebAuthnScenario struct {
	*RodSuite
}

func NewTwoFactorWebAuthnScenario() *TwoFactorWebAuthnScenario {
	return &TwoFactorWebAuthnScenario{
		RodSuite: NewRodSuite(""),
	}
}

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

	s.RodSession = browser

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

		s.collectCoverage(s.Page)
		s.MustClose()
	}()

	s.Page = s.doCreateTab(s.T(), HomeBaseURL)

	s.doWebAuthnInitialize(s.T(), s.Page, false)

	s.doLoginAndRegisterWebAuthn(s.T(), s.Context(ctx), "john", "password", false)
	s.doLogout(s.T(), s.Page)
}

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

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

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

	s.doWebAuthnInitialize(s.T(), s.Page, false)
	s.doWebAuthnRestoreCredentials(s.T(), s.Page)
}

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

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

	username := testUsername
	password := testPassword

	targetURL := fmt.Sprintf("%s/secret.html", AdminBaseURL)
	s.doLoginOneFactor(s.T(), s.Context(ctx), username, password, false, BaseDomain, targetURL)

	s.doWebAuthnMethodMaybeSelect(s.T(), s.Context(ctx))

	// And check if the user is redirected to the secret.
	s.verifySecretAuthorized(s.T(), s.Context(ctx))

	// Leave the secret.
	s.doVisit(s.T(), s.Context(ctx), HomeBaseURL)
	s.verifyIsHome(s.T(), s.Context(ctx))

	// And try to reload it again to check the session is kept.
	s.doVisit(s.T(), s.Context(ctx), targetURL)
	s.verifySecretAuthorized(s.T(), s.Context(ctx))
}

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

	username := testUsername
	password := testPassword

	s.doLoginOneFactor(s.T(), s.Context(ctx), username, password, false, BaseDomain, "")
	s.doOpenSettings(s.T(), s.Context(ctx))
	s.doOpenSettingsMenuClickTwoFactor(s.T(), s.Context(ctx))

	s.Assert().Equal("testing", s.WaitElementLocatedByID(s.T(), s.Context(ctx), "webauthn-credential-0-description").MustText())

	s.doWebAuthnCredentialRename(s.T(), s.Context(ctx), "testing2")

	s.Assert().Equal("testing2", s.WaitElementLocatedByID(s.T(), s.Context(ctx), "webauthn-credential-0-description").MustText())

	s.doWebAuthnCredentialRename(s.T(), s.Context(ctx), "testing")

	s.Assert().Equal("testing", s.WaitElementLocatedByID(s.T(), s.Context(ctx), "webauthn-credential-0-description").MustText())
}

func (s *TwoFactorWebAuthnScenario) TestShouldShowCredentialInformation() {
	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
	defer func() {
		cancel()
		s.collectScreenshot(ctx.Err(), s.Page)
	}()

	username := testUsername
	password := testPassword

	s.doLoginOneFactor(s.T(), s.Context(ctx), username, password, false, BaseDomain, "")
	s.doOpenSettings(s.T(), s.Context(ctx))
	s.doOpenSettingsMenuClickTwoFactor(s.T(), s.Context(ctx))

	s.Require().NoError(s.WaitElementLocatedByID(s.T(), s.Context(ctx), "webauthn-credential-0-information").Click("left", 1))
	s.Require().NoError(s.WaitElementLocatedByID(s.T(), s.Context(ctx), "dialog-close").Click("left", 1))
}

func (s *TwoFactorWebAuthnScenario) TestShouldDeleteAndRegisterCredential() {
	ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
	defer func() {
		cancel()
		s.collectScreenshot(ctx.Err(), s.Page)
	}()

	username := testUsername
	password := testPassword

	s.doLoginOneFactor(s.T(), s.Context(ctx), username, password, false, BaseDomain, "")
	s.doOpenSettings(s.T(), s.Context(ctx))
	s.doOpenSettingsMenuClickTwoFactor(s.T(), s.Context(ctx))

	s.doWebAuthnCredentialMustDelete(s.T(), s.Context(ctx))
	s.doWebAuthnCredentialRegister(s.T(), s.Context(ctx), "testing")
}

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

	suite.Run(t, NewTwoFactorWebAuthnScenario())
}