-
Notifications
You must be signed in to change notification settings - Fork 293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance/4819 auth dependent components #5078
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9b779a3
Update ErrorNotifications component.
kuasha420 37101d9
Update PermissionsModal component.
kuasha420 fa5b156
Split component to separate file.
kuasha420 706694b
Add PermissionsModal component test.
kuasha420 c38bf81
Add ErrorNotifications component test.
kuasha420 adeb8bb
Update the JSDoc for getPermissionScopeError.
tofumatt d6dfcfb
Test for text content instead of element count.
tofumatt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
assets/js/components/PermissionsModal/AuthenticatedPermissionsModal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* AuthenticatedPermissionsModal component. | ||
* | ||
* Site Kit by Google, Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useEffect, useCallback } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Data from 'googlesitekit-data'; | ||
import { CORE_USER } from '../../googlesitekit/datastore/user/constants'; | ||
import { CORE_LOCATION } from '../../googlesitekit/datastore/location/constants'; | ||
import { snapshotAllStores } from '../../googlesitekit/data/create-snapshot-store'; | ||
import Dialog from '../Dialog'; | ||
import Portal from '../Portal'; | ||
const { useSelect, useDispatch, useRegistry } = Data; | ||
|
||
const AuthenticatedPermissionsModal = () => { | ||
const registry = useRegistry(); | ||
const permissionsError = useSelect( ( select ) => | ||
select( CORE_USER ).getPermissionScopeError() | ||
); | ||
const connectURL = useSelect( ( select ) => | ||
select( CORE_USER ).getConnectURL( { | ||
additionalScopes: permissionsError?.data?.scopes, | ||
redirectURL: global.location.href, | ||
} ) | ||
); | ||
|
||
const { clearPermissionScopeError } = useDispatch( CORE_USER ); | ||
const { navigateTo } = useDispatch( CORE_LOCATION ); | ||
|
||
const onCancel = useCallback( () => { | ||
clearPermissionScopeError(); | ||
}, [ clearPermissionScopeError ] ); | ||
|
||
const onConfirm = useCallback( async () => { | ||
// If we have a datastores to snapshot before navigating away to the | ||
// authorization page, do that first. | ||
await snapshotAllStores( registry ); | ||
navigateTo( connectURL ); | ||
}, [ registry, connectURL, navigateTo ] ); | ||
|
||
useEffect( () => { | ||
// If error has flag to skip the modal, redirect to the authorization | ||
// page immediately without prompting the user, essentially short- | ||
// circuiting to the confirm step. | ||
const confirmIfSkipModal = async () => { | ||
if ( | ||
permissionsError?.data?.skipModal && | ||
permissionsError?.data?.scopes?.length | ||
) { | ||
await onConfirm(); | ||
} | ||
}; | ||
confirmIfSkipModal(); | ||
}, [ onConfirm, permissionsError ] ); | ||
|
||
if ( ! permissionsError ) { | ||
return null; | ||
} | ||
|
||
// If there aren't any scopes for us to request, there's no reason to show | ||
// the modal. Log a console warning if this happens and return `null`. | ||
if ( ! permissionsError?.data?.scopes?.length ) { | ||
global.console.warn( | ||
'permissionsError lacks scopes array to use for redirect, so not showing the PermissionsModal. permissionsError was:', | ||
permissionsError | ||
); | ||
return null; | ||
} | ||
|
||
if ( permissionsError?.data?.skipModal ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Portal> | ||
<Dialog | ||
title={ __( | ||
'Additional Permissions Required', | ||
'google-site-kit' | ||
) } | ||
subtitle={ permissionsError.message } | ||
confirmButton={ __( 'Proceed', 'google-site-kit' ) } | ||
dialogActive={ true } | ||
handleConfirm={ onConfirm } | ||
handleDialog={ onCancel } | ||
/> | ||
</Portal> | ||
); | ||
}; | ||
|
||
export default AuthenticatedPermissionsModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* PermissionsModal component tests. | ||
* | ||
* Site Kit by Google, Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import PermissionsModal from './'; | ||
import { | ||
render, | ||
createTestRegistry, | ||
provideUserAuthentication, | ||
} from '../../../../tests/js/test-utils'; | ||
import { CORE_USER } from '../../googlesitekit/datastore/user/constants'; | ||
|
||
describe( 'PermissionsModal', () => { | ||
let registry; | ||
|
||
beforeEach( () => { | ||
registry = createTestRegistry(); | ||
registry.dispatch( CORE_USER ).receiveConnectURL( 'test-url' ); | ||
registry.dispatch( CORE_USER ).setPermissionScopeError( { | ||
status: 500, | ||
message: 'Bad', | ||
data: { | ||
scopes: [ | ||
'https://www.googleapis.com/auth/analytics.readonly', | ||
], | ||
}, | ||
} ); | ||
} ); | ||
|
||
it( 'does not render AuthenticatedPermissionsModal when user is not authenticated', () => { | ||
provideUserAuthentication( registry, { authenticated: false } ); | ||
const { baseElement } = render( <PermissionsModal />, { registry } ); | ||
|
||
expect( baseElement ).not.toHaveTextContent( | ||
'Additional Permissions Required' | ||
); | ||
} ); | ||
|
||
it( 'renders AuthenticatedPermissionsModal when user is authenticated', () => { | ||
provideUserAuthentication( registry ); | ||
const { baseElement } = render( <PermissionsModal />, { registry } ); | ||
|
||
expect( baseElement ).toHaveTextContent( | ||
'Additional Permissions Required' | ||
); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
assets/js/components/notifications/ErrorNotifications.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* ErrorNotifications component tests. | ||
* | ||
* Site Kit by Google, Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ErrorNotifications from './ErrorNotifications'; | ||
import { | ||
render, | ||
createTestRegistry, | ||
provideUserAuthentication, | ||
provideModules, | ||
} from '../../../../tests/js/test-utils'; | ||
import { CORE_USER } from '../../googlesitekit/datastore/user/constants'; | ||
|
||
describe( 'ErrorNotifications', () => { | ||
let registry; | ||
|
||
beforeEach( () => { | ||
registry = createTestRegistry(); | ||
provideModules( registry ); | ||
registry.dispatch( CORE_USER ).receiveConnectURL( 'test-url' ); | ||
} ); | ||
|
||
it( 'does not render UnsatisfiedScopesAlert when user is not authenticated', () => { | ||
provideUserAuthentication( registry, { | ||
authenticated: false, | ||
unsatisfiedScopes: [ | ||
'https://www.googleapis.com/auth/analytics.readonly', | ||
], | ||
} ); | ||
const { container } = render( <ErrorNotifications />, { | ||
registry, | ||
} ); | ||
expect( container.childElementCount ).toBe( 0 ); | ||
} ); | ||
|
||
it( 'renders UnsatisfiedScopesAlert when user is authenticated', () => { | ||
provideUserAuthentication( registry, { | ||
unsatisfiedScopes: [ | ||
'https://www.googleapis.com/auth/analytics.readonly', | ||
], | ||
} ); | ||
const { container } = render( <ErrorNotifications />, { | ||
registry, | ||
} ); | ||
|
||
expect( container ).toHaveTextContent( | ||
'Site Kit can’t access necessary data' | ||
); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this can potentially return
undefined
, which I was assuming is from it resolving and returningundefined
during that, but it appears that actually isn't the case… it's eithernull
or an error.But the docs say it can return
undefined
:site-kit-wp/assets/js/googlesitekit/datastore/user/permissions.js
Lines 147 to 159 in d3aed2e
This isn't related to your change, but we should update the JSDoc for
getPermissionScopeError
so it doesn't trip anyone else up. Looks like it should be@return {(Object|undefined)}
not@return {(Object|null)}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worse: I think it was me who wrong that JSDoc. Shame! 😆