Skip to content

Commit d9fae02

Browse files
Merge pull request #1906 from creative-commoners/pulls/3.0/sudo-401
FIX Handle 401 from server
2 parents 5a4b9e3 + bcb762a commit d9fae02

File tree

6 files changed

+31
-21
lines changed

6 files changed

+31
-21
lines changed

client/dist/js/bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/src/components/SudoModePasswordField/SudoModePasswordField.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ function SudoModePasswordField(props) {
5353
const headers = {
5454
'X-SecurityID': Config.get('SecurityID'),
5555
};
56-
const responseJson = await fetcher(data, headers);
57-
if (responseJson.result) {
58-
onSuccess();
59-
} else {
60-
setResponseMessage(responseJson.message);
61-
}
56+
fetcher(data, headers)
57+
.then(() => onSuccess())
58+
.catch(async (err) => {
59+
const responseJson = await err.response.json();
60+
setResponseMessage(responseJson.message);
61+
});
6262
}
6363

6464
/**

client/src/components/SudoModePasswordField/tests/SudoModePasswordField-test.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,29 @@ window.ss.config = {
1616
};
1717

1818
let doResolve;
19+
let doReject;
20+
21+
beforeEach(() => {
22+
doResolve = undefined;
23+
doReject = undefined;
24+
});
25+
26+
function createJsonError(message) {
27+
return {
28+
response: {
29+
json: () => ({
30+
result: false,
31+
message
32+
}),
33+
},
34+
};
35+
}
1936

2037
jest.mock('lib/Backend', () => ({
2138
createEndpointFetcher: () => () => (
22-
new Promise((resolve) => {
39+
new Promise((resolve, reject) => {
2340
doResolve = resolve;
41+
doReject = reject;
2442
})
2543
)
2644
}));
@@ -68,10 +86,7 @@ test('SudoModePasswordField should show a message on failure', async () => {
6886
passwordField.value = 'password';
6987
const verifyButton = await screen.findByText('Verify');
7088
fireEvent.click(verifyButton);
71-
doResolve({
72-
result: false,
73-
message: 'A big failure'
74-
});
89+
await doReject(createJsonError('A big failure'));
7590
const message = await screen.findByText('A big failure');
7691
expect(message).not.toBeNull();
7792
expect(onSuccess).not.toBeCalled();

code/SudoModeController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,20 @@ public function activate(HTTPRequest $request): HTTPResponse
7979

8080
if (!SecurityToken::inst()->checkRequest($request)) {
8181
return $this->jsonResponse([
82-
'result' => false,
8382
'message' => _t(__CLASS__ . '.TIMEOUT', 'Session timed out, please refresh and try again.'),
8483
], 403);
8584
}
8685

8786
// Validate password
8887
if (!$this->checkPassword($request)) {
8988
return $this->jsonResponse([
90-
'result' => false,
9189
'message' => _t(__CLASS__ . '.INVALID', 'Incorrect password'),
9290
], 401);
9391
}
9492

9593
// Activate sudo mode and return successful result
9694
$this->getSudoModeService()->activate($request->getSession());
97-
return $this->jsonResponse(['result' => true]);
95+
return $this->jsonResponse([]);
9896
}
9997

10098
/**

tests/behat/features/form-sudo-mode.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Feature: Form sudo mode
1919
# CMS profile
2020
When I go to "/admin/myprofile"
2121
Then I should see "Verify to continue"
22-
And I should see a "#action_save[readonly]" element
22+
And I should see a "#Form_EditForm_action_save[readonly]" element
2323

2424
# Security admin - members
2525
When I go to "/admin/security"
@@ -65,7 +65,7 @@ Feature: Form sudo mode
6565
And I fill in "SudoModePassword" with "incorrect-password"
6666
And I click on the ".sudo-mode-password-field__verify-button" element
6767
Then I should see "Incorrect password"
68-
And I should see a "#action_save[readonly]" element
68+
And I should see a "#Form_EditForm_action_save[readonly]" element
6969

7070
Scenario: Sensitive data can be edited after activating sudo mode
7171

@@ -75,7 +75,7 @@ Feature: Form sudo mode
7575
And I fill in "SudoModePassword" with "Secret!123"
7676
And I click on the ".sudo-mode-password-field__verify-button" element
7777
And I wait for 2 seconds
78-
Then I should not see a "#action_save[readonly]" element
78+
Then I should not see a "#Form_EditForm_action_save[readonly]" element
7979

8080
# Security admin - members
8181
When I go to "/admin/security"

tests/php/SudoModeControllerTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ public function testActivateFailsWithIncorrectPassword()
9090

9191
$this->assertSame(401, $response->getStatusCode());
9292
$result = json_decode((string) $response->getBody(), true);
93-
$this->assertFalse($result['result'], 'Should have failed with incorrect password');
9493
$this->assertEquals('Incorrect password', $result['message']);
9594
}
9695

@@ -103,7 +102,6 @@ public function testActivateSudoModeWithValidCredentials()
103102

104103
$this->assertSame(200, $activateResponse->getStatusCode());
105104
$result = json_decode((string) $activateResponse->getBody(), true);
106-
$this->assertTrue($result['result'], 'Should have activated sudo mode');
107105

108106
$checkResponse = $this->get(SudoModeController::singleton()->Link('check'));
109107
$this->assertSame(200, $checkResponse->getStatusCode());
@@ -128,7 +126,6 @@ public function testActivateChecksCSRFToken()
128126

129127
$this->assertSame(403, $activateResponse->getStatusCode());
130128
$result = json_decode((string) $activateResponse->getBody(), true);
131-
$this->assertFalse($result['result'], 'Should have failed on CSRF token validation');
132129
$this->assertSame($result['message'], 'Session timed out, please refresh and try again.');
133130
}
134131

0 commit comments

Comments
 (0)