Skip to content

Commit

Permalink
feat(cart-customer): check token for cart mutation failures (#2450)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 committed Jul 26, 2023
1 parent 7d6b62d commit f14af85
Show file tree
Hide file tree
Showing 5 changed files with 742 additions and 89 deletions.
76 changes: 0 additions & 76 deletions libs/cart-customer/state/src/effects/auth.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {

import {
DaffAuthLoginSuccess,
DaffAuthLogoutSuccess,
DaffAuthRegisterSuccess,
DaffResetPasswordSuccess,
} from '@daffodil/auth/state';
Expand All @@ -22,14 +21,11 @@ import {
import {
DaffCartServiceInterface,
DaffCartDriver,
DaffCartDriverErrorCodes,
} from '@daffodil/cart/driver';
import { DaffTestingCartDriverModule } from '@daffodil/cart/driver/testing';
import {
DaffResolveCartFailure,
DaffResolveCartSuccess,
DaffCartCreate,
DaffCartLoadFailure,
} from '@daffodil/cart/state';
import { DaffCartFactory } from '@daffodil/cart/testing';
import { DaffStorageServiceError } from '@daffodil/core';
Expand Down Expand Up @@ -410,76 +406,4 @@ describe('@daffodil/cart-customer/state | DaffCartCustomerAuthEffects', () => {
});
});
});

describe('when ResolveCartFailureAction is triggered', () => {
let expected;

describe('and the error is a DaffUnauthorizedForCartError', () => {
beforeEach(() => {
const error: DaffStateError = { code: DaffCartDriverErrorCodes.UNAUTHORIZED_FOR_CART, recoverable: false, message: 'Unauthorized' };
const resolveCartFailureAction = new DaffResolveCartFailure([error]);
const cartCreateAction = new DaffCartCreate();
actions$ = hot('--a', { a: resolveCartFailureAction });
expected = cold('--b', { b: cartCreateAction });
});

it('should dispatch cart create', () => {
expect(effects.createWhenUnathorized$).toBeObservable(expected);
});

it('should remove the cart ID from storage', () => {
expect(effects.createWhenUnathorized$).toBeObservable(expected);
expect(removeCartIdSpy).toHaveBeenCalledWith();
});
});

describe('and the error is not a DaffUnauthorizedForCartError', () => {
beforeEach(() => {
const error: DaffStateError = { code: 'code', recoverable: false, message: 'Something went wrong' };
const resolveCartFailureAction = new DaffResolveCartFailure([error]);
actions$ = hot('--a', { a: resolveCartFailureAction });
expected = cold('---');
});

it('should not dispatch anything', () => {
expect(effects.createWhenUnathorized$).toBeObservable(expected);
});
});
});

describe('when CartLoadFailureAction is triggered', () => {
let expected;

describe('and the error is a DaffUnauthorizedForCartError', () => {
beforeEach(() => {
const error: DaffStateError = { code: DaffCartDriverErrorCodes.UNAUTHORIZED_FOR_CART, recoverable: false, message: 'Unauthorized' };
const resolveCartFailureAction = new DaffCartLoadFailure([error]);
const cartCreateAction = new DaffCartCreate();
actions$ = hot('--a', { a: resolveCartFailureAction });
expected = cold('--b', { b: cartCreateAction });
});

it('should dispatch cart create', () => {
expect(effects.createWhenUnathorized$).toBeObservable(expected);
});

it('should remove the cart ID from storage', () => {
expect(effects.createWhenUnathorized$).toBeObservable(expected);
expect(removeCartIdSpy).toHaveBeenCalledWith();
});
});

describe('and the error is not a DaffUnauthorizedForCartError', () => {
beforeEach(() => {
const error: DaffStateError = { code: 'code', recoverable: false, message: 'Something went wrong' };
const resolveCartFailureAction = new DaffCartLoadFailure([error]);
actions$ = hot('--a', { a: resolveCartFailureAction });
expected = cold('---');
});

it('should not dispatch anything', () => {
expect(effects.createWhenUnathorized$).toBeObservable(expected);
});
});
});
});
13 changes: 0 additions & 13 deletions libs/cart-customer/state/src/effects/auth.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,11 @@ import {
import { DAFF_CART_CUSTOMER_ERROR_MATCHER } from '@daffodil/cart-customer';
import {
DaffCartDriver,
DaffCartDriverErrorCodes,
DaffCartServiceInterface,
} from '@daffodil/cart/driver';
import {
DaffResolveCartSuccess,
DaffResolveCartFailure,
DaffCartCreate,
DaffCartLoadFailure,
DaffCartActionTypes,
} from '@daffodil/cart/state';
import { DaffError } from '@daffodil/core';
import { ErrorTransformer } from '@daffodil/core/state';
Expand Down Expand Up @@ -97,13 +93,4 @@ export class DaffCartCustomerAuthEffects<T extends DaffCart = DaffCart> {
),
),
));

createWhenUnathorized$ = createEffect(() => this.actions$.pipe(
ofType<DaffResolveCartFailure | DaffCartLoadFailure>(DaffCartActionTypes.ResolveCartFailureAction, DaffCartActionTypes.CartLoadFailureAction),
filter(action => !!action.payload.find(err => err.code === DaffCartDriverErrorCodes.UNAUTHORIZED_FOR_CART)),
tap(() => {
this.cartStorage.removeCartId();
}),
map(() => new DaffCartCreate()),
));
}
Loading

0 comments on commit f14af85

Please sign in to comment.