-
Notifications
You must be signed in to change notification settings - Fork 310
Enhancement/9621 update siwg client id url #9689
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
Merged
aaemnnosttv
merged 15 commits into
develop
from
enhancement/9621-update-siwg-client-id-url
Nov 15, 2024
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ceb50ac
Add getServiceURL selector for SiwG.
jimmymadon c31ece6
Use getServiceURL to build url to fetch the clientID.
jimmymadon f7d08f2
Combine the new datastore partial.
jimmymadon 44d6205
Use the new getClientIDURL selector in the front end.
jimmymadon 4f9f670
Add standard tests for the siwg getServiceURL selector.
jimmymadon bbf8c79
Add a test for the getClientIDURL selector.
jimmymadon d124ff9
Update service URLs generic tests.
jimmymadon 577d14a
Update the query params for SiwG client ID URL.
jimmymadon 402b092
Rename selector to follow pattern.
jimmymadon b7d2aeb
Update selector name in front end component.
jimmymadon 7c481f4
Update selector name in test.
jimmymadon f4205b9
Remove unnecessary untrailingslash modification.
jimmymadon 79cbf08
Update test for siteorigin change.
jimmymadon 43b7552
Fix undefined homeURL.
jimmymadon 54f1f37
Update variable name for consistency.
aaemnnosttv 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
This file contains hidden or 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 hidden or 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 hidden or 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
101 changes: 101 additions & 0 deletions
101
assets/js/modules/sign-in-with-google/datastore/service.js
This file contains hidden or 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,101 @@ | ||
/** | ||
* `modules/sign-in-with-google` data store: service. | ||
* | ||
* Site Kit by Google, Copyright 2024 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 { addQueryArgs } from '@wordpress/url'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { createRegistrySelector } from 'googlesitekit-data'; | ||
import { CORE_USER } from '../../../googlesitekit/datastore/user/constants'; | ||
import { CORE_SITE } from '../../../googlesitekit/datastore/site/constants'; | ||
import { MODULES_SIGN_IN_WITH_GOOGLE } from './constants'; | ||
|
||
export const selectors = { | ||
/** | ||
* Gets a URL to the service. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param {Object} state Data store's state. | ||
* @param {Object} [args] Object containing optional path and query args. | ||
* @param {string} [args.path] A path to append to the base url. | ||
* @param {Object} [args.query] Object of query params to be added to the URL. | ||
* @return {(string|undefined)} The URL to the service, or `undefined` if not loaded. | ||
*/ | ||
getServiceURL: createRegistrySelector( | ||
( select ) => | ||
( state, { path, query } = {} ) => { | ||
let serviceURL = | ||
'https://developers.google.com/identity/site-kit'; | ||
|
||
if ( query ) { | ||
serviceURL = addQueryArgs( serviceURL, query ); | ||
} | ||
|
||
if ( path ) { | ||
const sanitizedPath = `/${ path.replace( /^\//, '' ) }`; | ||
serviceURL = `${ serviceURL }#${ sanitizedPath }`; | ||
} | ||
|
||
const accountChooserBaseURI = | ||
select( CORE_USER ).getAccountChooserURL( serviceURL ); | ||
|
||
if ( accountChooserBaseURI === undefined ) { | ||
return undefined; | ||
} | ||
|
||
return accountChooserBaseURI; | ||
} | ||
), | ||
/** | ||
* Gets a URL to fetch the client ID. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param {Object} state Data store's state. | ||
* @return {(string|undefined)} The URL to the clientID, or `undefined` if not loaded. | ||
*/ | ||
getServiceClientIDProvisioningURL: createRegistrySelector( | ||
( select ) => () => { | ||
const siteName = select( CORE_SITE ).getSiteName(); | ||
const homeURL = select( CORE_SITE ).getHomeURL(); | ||
const supportEmail = select( CORE_USER ).getEmail(); | ||
|
||
const query = { | ||
appname: siteName, | ||
sitename: siteName, | ||
siteorigin: homeURL ? new URL( homeURL ).origin : homeURL, | ||
supportemail: supportEmail, | ||
}; | ||
|
||
return select( MODULES_SIGN_IN_WITH_GOOGLE ).getServiceURL( { | ||
query, | ||
} ); | ||
} | ||
), | ||
}; | ||
|
||
const store = { | ||
selectors, | ||
}; | ||
|
||
export default store; |
110 changes: 110 additions & 0 deletions
110
assets/js/modules/sign-in-with-google/datastore/service.test.js
This file contains hidden or 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,110 @@ | ||
/** | ||
* `modules/sign-in-with-google` data store: service tests. | ||
* | ||
* Site Kit by Google, Copyright 2024 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 { decodeServiceURL } from '../../../../../tests/js/mock-accountChooserURL-utils'; | ||
import { | ||
createTestRegistry, | ||
provideSiteInfo, | ||
provideUserInfo, | ||
} from '../../../../../tests/js/utils'; | ||
import { MODULES_SIGN_IN_WITH_GOOGLE } from './constants'; | ||
|
||
describe( 'module/sign-in-with-google service store', () => { | ||
const baseURI = 'https://developers.google.com/identity/site-kit'; | ||
|
||
const userData = { | ||
id: 1, | ||
email: '[email protected]', | ||
name: 'admin', | ||
picture: 'https://path/to/image', | ||
}; | ||
|
||
let registry; | ||
|
||
beforeEach( () => { | ||
registry = createTestRegistry(); | ||
provideUserInfo( registry, userData ); | ||
provideSiteInfo( registry ); | ||
} ); | ||
|
||
describe( 'selectors', () => { | ||
describe( 'getServiceURL', () => { | ||
it( 'retrieves the correct URL with no arguments', () => { | ||
const serviceURL = registry | ||
.select( MODULES_SIGN_IN_WITH_GOOGLE ) | ||
.getServiceURL(); | ||
|
||
expect( serviceURL ).toMatchInlineSnapshot( | ||
'"https://accounts.google.com/accountchooser?continue=https%3A%2F%2Fdevelopers.google.com%2Fidentity%2Fsite-kit&Email=admin%40example.com"' | ||
); | ||
} ); | ||
|
||
it( 'adds the path parameter', () => { | ||
const serviceURLNoSlashes = registry | ||
.select( MODULES_SIGN_IN_WITH_GOOGLE ) | ||
.getServiceURL( { path: 'test/path/to/deeplink' } ); | ||
|
||
expect( serviceURLNoSlashes ).toMatchInlineSnapshot( | ||
'"https://accounts.google.com/accountchooser?continue=https%3A%2F%2Fdevelopers.google.com%2Fidentity%2Fsite-kit%23%2Ftest%2Fpath%2Fto%2Fdeeplink&Email=admin%40example.com"' | ||
); | ||
|
||
const serviceURLWithLeadingSlash = registry | ||
.select( MODULES_SIGN_IN_WITH_GOOGLE ) | ||
.getServiceURL( { path: '/test/path/to/deeplink' } ); | ||
|
||
expect( serviceURLWithLeadingSlash ).toMatchInlineSnapshot( | ||
'"https://accounts.google.com/accountchooser?continue=https%3A%2F%2Fdevelopers.google.com%2Fidentity%2Fsite-kit%23%2Ftest%2Fpath%2Fto%2Fdeeplink&Email=admin%40example.com"' | ||
); | ||
} ); | ||
|
||
it( 'adds query args', () => { | ||
const path = '/test/path/to/deeplink'; | ||
const query = { | ||
authuser: userData.email, | ||
param1: '1', | ||
param2: '2', | ||
}; | ||
const serviceURL = registry | ||
.select( MODULES_SIGN_IN_WITH_GOOGLE ) | ||
.getServiceURL( { path, query } ); | ||
const decodedServiceURL = decodeServiceURL( serviceURL ); | ||
|
||
expect( decodedServiceURL.startsWith( baseURI ) ).toBe( true ); | ||
expect( decodedServiceURL.endsWith( `#${ path }` ) ).toBe( | ||
true | ||
); | ||
expect( decodedServiceURL ).toMatchQueryParameters( query ); | ||
} ); | ||
} ); | ||
describe( 'getServiceClientIDProvisioningURL', () => { | ||
it( 'retrieves the correct Client ID URL', () => { | ||
const clientIDURL = registry | ||
.select( MODULES_SIGN_IN_WITH_GOOGLE ) | ||
.getServiceClientIDProvisioningURL(); | ||
|
||
expect( clientIDURL ).toMatchInlineSnapshot( | ||
'"https://accounts.google.com/accountchooser?continue=https%3A%2F%2Fdevelopers.google.com%2Fidentity%2Fsite-kit%3Fappname%3DMy%2520Site%2520Name%26sitename%3DMy%2520Site%2520Name%26siteorigin%3Dhttp%253A%252F%252Fexample.com%26supportemail%3Dadmin%2540example.com&Email=admin%40example.com"' | ||
); | ||
} ); | ||
} ); | ||
} ); | ||
} ); |
Oops, something went wrong.
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.
👍