Skip to content

Commit

Permalink
Add test case for insuficient permissions error.
Browse files Browse the repository at this point in the history
  • Loading branch information
hussain-t committed Oct 22, 2024
1 parent b499eb8 commit eb3233b
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions assets/js/modules/analytics-4/datastore/audiences.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
SITE_KIT_AUDIENCE_DEFINITIONS,
} from './constants';
import { CORE_USER } from '../../../googlesitekit/datastore/user/constants';
import { ERROR_REASON_INSUFFICIENT_PERMISSIONS } from '../../../util/errors';
import {
properties as propertiesFixture,
audiences as audiencesFixture,
Expand Down Expand Up @@ -1573,6 +1574,47 @@ describe( 'modules/analytics-4 audiences', () => {
registry.select( CORE_USER ).getConfiguredAudiences()
).toEqual( expectedConfiguredAudiences );
} );

it( 'should return an insufficient permisions error if "create-audience" request fails', async () => {
const insufficientPermissionsError = {
code: 'test_error',
message: 'Error message.',
data: {
reason: ERROR_REASON_INSUFFICIENT_PERMISSIONS,
},
};

fetchMock.postOnce( syncAvailableAudiencesEndpoint, {
body: [],
status: 200,
} );

// Mocking createAudience API call with insufficient permissions error.
fetchMock.post(
{ url: createAudienceEndpoint, repeat: 2 },
{
body: insufficientPermissionsError,
status: 400,
}
);

const { response, error } = await registry
.dispatch( MODULES_ANALYTICS_4 )
.enableAudienceGroup();

await waitForDefaultTimeouts();

expect( response ).toBeUndefined();
expect( error ).toEqual( insufficientPermissionsError );

// Ensuring no configured audiences are set when all creation attempts fail.
expect(
registry.select( CORE_USER ).getConfiguredAudiences()
).toBeNull();

expect( console ).toHaveErrored();
expect( console ).toHaveErrored();
} );
} );
} );

Expand Down

0 comments on commit eb3233b

Please sign in to comment.