Skip to content

Commit 41be9dc

Browse files
authored
G Suite: Fix white screen while trying to add G Suite for a WPcom primary sub-domain (#36857)
* G Suite: Fix white screen while trying to add G Suite for a WPcom primary sub-domain * G Suite: Refine the logic that retrieves a domain eligible for G Suite This also adds more unit tests.
1 parent 4db3529 commit 41be9dc

File tree

2 files changed

+70
-17
lines changed

2 files changed

+70
-17
lines changed

client/lib/gsuite/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* External dependencies
33
*/
44
import formatCurrency from '@automattic/format-currency';
5-
import { get, includes, some, endsWith, find } from 'lodash';
5+
import { endsWith, get, includes, some, sortBy } from 'lodash';
66

77
/**
88
* Internal dependencies
@@ -74,25 +74,25 @@ function getAnnualPrice( cost, currencyCode ) {
7474
}
7575

7676
/**
77-
* Retrieves the first domain that is eligible to G Suite either from, and in that order:
77+
* Retrieves the first domain that is eligible to G Suite in this order:
7878
*
79-
* - The domain from the site currently selected
80-
* - The primary domain of the site
79+
* - The domain from the site currently selected, if eligible
80+
* - The primary domain of the site, if eligible
81+
* - The first non-primary domain eligible found
8182
*
8283
* @param {String} selectedDomainName - domain name for the site currently selected by the user
8384
* @param {Array} domains - list of domain objects
84-
* @returns {String} - Eligible domain name
85+
* @returns {String} - the name of the first eligible domain found
8586
*/
8687
function getEligibleGSuiteDomain( selectedDomainName, domains ) {
8788
if ( selectedDomainName && canDomainAddGSuite( selectedDomainName ) ) {
8889
return selectedDomainName;
8990
}
9091

91-
const supportedDomains = getGSuiteSupportedDomains( domains );
92+
// Orders domains with the primary domain in first position, if any
93+
const supportedDomains = sortBy( getGSuiteSupportedDomains( domains ), ( domain ) => ! domain.isPrimary );
9294

93-
const primaryDomain = find( supportedDomains, 'isPrimary' );
94-
95-
return get( primaryDomain, 'name', '' );
95+
return get( supportedDomains, '[0].name', '' );
9696
}
9797

9898
/**

client/lib/gsuite/test/index.js

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,68 @@ describe( 'index', () => {
3737
} );
3838

3939
describe( '#getEligibleGSuiteDomain', () => {
40-
test( 'Returns selected domain name if valid', () => {
41-
expect( getEligibleGSuiteDomain( 'foobar.blog', [] ) ).toEqual( 'foobar.blog' );
40+
test( 'Returns empty string if selected domain and domains are empty', () => {
41+
expect( getEligibleGSuiteDomain( '', [] ) ).toEqual( '' );
4242
} );
4343

44-
test( 'Returns empty string if no selected site and empty domains array', () => {
45-
expect( getEligibleGSuiteDomain( '', [] ) ).toEqual( '' );
44+
test( 'Returns empty string if selected domain is invalid and domains are empty', () => {
45+
expect( getEligibleGSuiteDomain( 'domain-with-google-banned-term.blog', [] ) ).toEqual( '' );
46+
} );
47+
48+
test( 'Returns selected domain if selected domain is valid and domains are empty', () => {
49+
expect( getEligibleGSuiteDomain( 'valid-domain.blog', [] ) ).toEqual( 'valid-domain.blog' );
50+
} );
51+
52+
const domains = [
53+
{
54+
name: 'domain-with-google-banned-term.blog',
55+
type: 'REGISTERED',
56+
},
57+
{
58+
name: 'account-with-another-provider.blog',
59+
type: 'REGISTERED',
60+
googleAppsSubscription: { status: 'other_provider' },
61+
},
62+
{
63+
name: 'mapped-domain-without-wpcom-nameservers.blog',
64+
type: 'MAPPED',
65+
hasWpcomNameservers: false,
66+
},
67+
{
68+
name: 'mapped-domain-with-wpcom-nameservers.blog',
69+
type: 'MAPPED',
70+
hasWpcomNameservers: true,
71+
},
72+
{
73+
name: 'secondary-domain.blog',
74+
type: 'REGISTERED',
75+
isPrimary: false,
76+
},
77+
{
78+
name: 'primary-domain.blog',
79+
type: 'REGISTERED',
80+
isPrimary: true,
81+
}
82+
];
83+
84+
test( 'Returns selected domain if selected domain is valid', () => {
85+
expect( getEligibleGSuiteDomain( 'selected-valid-domain.blog', domains ) ).toEqual( 'selected-valid-domain.blog' );
86+
} );
87+
88+
test( 'Returns primary domain if no selected domain', () => {
89+
expect( getEligibleGSuiteDomain( '', domains ) ).toEqual( 'primary-domain.blog' );
4690
} );
4791

48-
test( 'Returns empty string if selected site is invalid and empty domains array', () => {
49-
expect( getEligibleGSuiteDomain( 'foogoogle.blog', [] ) ).toEqual( '' );
92+
test( 'Returns first non-primary domain if no selected domain and no primary domain in domains', () => {
93+
const domainsWithoutPrimaryDomain = domains.slice( 0, -1 );
94+
95+
expect( getEligibleGSuiteDomain( '', domainsWithoutPrimaryDomain ) ).toEqual( 'mapped-domain-with-wpcom-nameservers.blog' );
5096
} );
5197

52-
test( 'Returns empty string if no selected site and domains array does not contain a valid domain', () => {
53-
expect( getEligibleGSuiteDomain( '', [ 'foogoogle.blog' ] ) ).toEqual( '' );
98+
test( 'Returns empty string if no selected domain and no valid domain in domains', () => {
99+
const domainsWithoutValidDomain = domains.slice( 0, -3 );
100+
101+
expect( getEligibleGSuiteDomain( '', domainsWithoutValidDomain ) ).toEqual( '' );
54102
} );
55103
} );
56104

@@ -74,11 +122,13 @@ describe( 'index', () => {
74122
hasWpcomNameservers: true,
75123
googleAppsSubscription: {},
76124
};
125+
77126
expect( getGSuiteSupportedDomains( [ registered ] ) ).toEqual( [ registered ] );
78127
} );
79128

80129
test( 'returns empty array if domain is valid and type of mapped without our nameservers', () => {
81130
const mapped = { name: 'foo.blog', type: 'MAPPED', googleAppsSubscription: {} };
131+
82132
expect( getGSuiteSupportedDomains( [ mapped ] ) ).toEqual( [] );
83133
} );
84134

@@ -89,11 +139,13 @@ describe( 'index', () => {
89139
googleAppsSubscription: {},
90140
hasWpcomNameservers: true,
91141
};
142+
92143
expect( getGSuiteSupportedDomains( [ mapped ] ) ).toEqual( [ mapped ] );
93144
} );
94145

95146
test( 'returns empty array if domain is valid and type of site redirected', () => {
96147
const siteRedirect = { name: 'foo.blog', type: 'SITE_REDIRECT', googleAppsSubscription: {} };
148+
97149
expect( getGSuiteSupportedDomains( [ siteRedirect ] ) ).toEqual( [] );
98150
} );
99151
} );
@@ -151,6 +203,7 @@ describe( 'index', () => {
151203
false
152204
);
153205
} );
206+
154207
test( 'returns true if googleAppsSubscription.pendingUsers has an non-empty array', () => {
155208
expect(
156209
hasPendingGSuiteUsers( { googleAppsSubscription: { pendingUsers: [ 'foo' ] } } )

0 commit comments

Comments
 (0)