1
1
import { AuthenticatorsFixture } from 'sentry-fixture/authenticators' ;
2
+ import { OrganizationFixture } from 'sentry-fixture/organization' ;
2
3
import { RouterContextFixture } from 'sentry-fixture/routerContextFixture' ;
3
4
import { RouterFixture } from 'sentry-fixture/routerFixture' ;
4
5
5
6
import { render , screen , userEvent } from 'sentry-test/reactTestingLibrary' ;
6
7
8
+ import OrganizationsStore from 'sentry/stores/organizationsStore' ;
7
9
import AccountSecurityEnroll from 'sentry/views/settings/account/accountSecurity/accountSecurityEnroll' ;
8
10
9
11
const ENDPOINT = '/users/me/authenticators/' ;
12
+ const usorg = OrganizationFixture ( {
13
+ slug : 'us-org' ,
14
+ links : {
15
+ organizationUrl : 'https://us-org.example.test' ,
16
+ regionUrl : 'https://us.example.test' ,
17
+ } ,
18
+ } ) ;
10
19
11
20
describe ( 'AccountSecurityEnroll' , function ( ) {
21
+ jest . spyOn ( window . location , 'assign' ) . mockImplementation ( ( ) => { } ) ;
22
+
12
23
describe ( 'Totp' , function ( ) {
13
24
const authenticator = AuthenticatorsFixture ( ) . Totp ( {
14
25
isEnrolled : false ,
@@ -32,14 +43,31 @@ describe('AccountSecurityEnroll', function () {
32
43
} ,
33
44
] ) ;
34
45
46
+ let location ;
35
47
beforeEach ( function ( ) {
48
+ location = window . location ;
49
+ window . location . href = 'https://example.test' ;
50
+ window . __initialData = {
51
+ ...window . __initialData ,
52
+ links : {
53
+ organizationUrl : undefined ,
54
+ regionUrl : undefined ,
55
+ sentryUrl : 'https://example.test' ,
56
+ } ,
57
+ } ;
58
+ OrganizationsStore . load ( [ usorg ] ) ;
59
+
36
60
MockApiClient . clearMockResponses ( ) ;
37
61
MockApiClient . addMockResponse ( {
38
62
url : `${ ENDPOINT } ${ authenticator . authId } /enroll/` ,
39
63
body : authenticator ,
40
64
} ) ;
41
65
} ) ;
42
66
67
+ beforeEach ( function ( ) {
68
+ window . location = location ;
69
+ } ) ;
70
+
43
71
it ( 'does not have enrolled circle indicator' , function ( ) {
44
72
render ( < AccountSecurityEnroll /> , { context : routerContext } ) ;
45
73
@@ -54,11 +82,63 @@ describe('AccountSecurityEnroll', function () {
54
82
expect ( screen . getByLabelText ( 'Enrollment QR Code' ) ) . toBeInTheDocument ( ) ;
55
83
} ) ;
56
84
57
- it ( 'can enroll' , async function ( ) {
85
+ it ( 'can enroll from org subdomain' , async function ( ) {
86
+ window . location . href = 'https://us-org.example.test' ;
87
+ window . __initialData = {
88
+ ...window . __initialData ,
89
+ links : {
90
+ organizationUrl : 'https://us-org.example.test' ,
91
+ regionUrl : 'https://us.example.test' ,
92
+ sentryUrl : 'https://example.test' ,
93
+ } ,
94
+ } ;
95
+
96
+ const enrollMock = MockApiClient . addMockResponse ( {
97
+ url : `${ ENDPOINT } ${ authenticator . authId } /enroll/` ,
98
+ method : 'POST' ,
99
+ } ) ;
100
+ const fetchOrgsMock = MockApiClient . addMockResponse ( {
101
+ url : `/organizations/` ,
102
+ body : [ usorg ] ,
103
+ } ) ;
104
+
105
+ render ( < AccountSecurityEnroll /> , { context : routerContext } ) ;
106
+
107
+ await userEvent . type ( screen . getByRole ( 'textbox' , { name : 'OTP Code' } ) , 'otp{enter}' ) ;
108
+
109
+ expect ( enrollMock ) . toHaveBeenCalledWith (
110
+ `${ ENDPOINT } 15/enroll/` ,
111
+ expect . objectContaining ( {
112
+ method : 'POST' ,
113
+ data : expect . objectContaining ( {
114
+ secret : 'secret' ,
115
+ otp : 'otp' ,
116
+ } ) ,
117
+ } )
118
+ ) ;
119
+ expect ( fetchOrgsMock ) . not . toHaveBeenCalled ( ) ;
120
+ expect ( window . location . assign ) . not . toHaveBeenCalled ( ) ;
121
+ } ) ;
122
+
123
+ it ( 'can enroll from main domain' , async function ( ) {
124
+ OrganizationsStore . load ( [ ] ) ;
125
+ window . __initialData = {
126
+ ...window . __initialData ,
127
+ links : {
128
+ organizationUrl : 'https://us-org.example.test' ,
129
+ regionUrl : 'https://us.example.test' ,
130
+ sentryUrl : 'https://example.test' ,
131
+ } ,
132
+ } ;
133
+
58
134
const enrollMock = MockApiClient . addMockResponse ( {
59
135
url : `${ ENDPOINT } ${ authenticator . authId } /enroll/` ,
60
136
method : 'POST' ,
61
137
} ) ;
138
+ const fetchOrgsMock = MockApiClient . addMockResponse ( {
139
+ url : `/organizations/` ,
140
+ body : [ usorg ] ,
141
+ } ) ;
62
142
63
143
render ( < AccountSecurityEnroll /> , { context : routerContext } ) ;
64
144
@@ -74,6 +154,9 @@ describe('AccountSecurityEnroll', function () {
74
154
} ) ,
75
155
} )
76
156
) ;
157
+ expect ( fetchOrgsMock ) . toHaveBeenCalledTimes ( 1 ) ;
158
+ expect ( window . location . assign ) . toHaveBeenCalledTimes ( 1 ) ;
159
+ expect ( window . location . assign ) . toHaveBeenCalledWith ( 'http://us-org.example.test/' ) ;
77
160
} ) ;
78
161
79
162
it ( 'can redirect with already enrolled error' , function ( ) {
0 commit comments