Skip to content

Commit

Permalink
fix: avoid pgid errors when a non-anonymous apiToken is available
Browse files Browse the repository at this point in the history
  • Loading branch information
Eisie96 committed Aug 15, 2023
1 parent 2b9d8ea commit 1459e38
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/app/core/services/api/api.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TestBed } from '@angular/core/testing';
import { Action, Store } from '@ngrx/store';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { noop } from 'rxjs';
import { anything, capture, spy, verify } from 'ts-mockito';
import { anything, capture, instance, mock, spy, verify } from 'ts-mockito';

import { HttpError } from 'ish-core/models/http-error/http-error.model';
import { Link } from 'ish-core/models/link/link.model';
Expand All @@ -19,6 +19,7 @@ import { CoreStoreModule } from 'ish-core/store/core/core-store.module';
import { serverError } from 'ish-core/store/core/error';
import { isServerConfigurationLoaded, loadServerConfigSuccess } from 'ish-core/store/core/server-config';
import { getPGID } from 'ish-core/store/customer/user';
import { CookiesService } from 'ish-core/utils/cookies/cookies.service';

import { ApiService, unpackEnvelope } from './api.service';

Expand All @@ -37,6 +38,7 @@ describe('Api Service', () => {
// https://angular.io/guide/http#testing-http-requests
imports: [HttpClientTestingModule],
providers: [
{ provide: CookiesService, useFactory: () => instance(mock(CookiesService)) },
provideMockStore({
selectors: [
{ selector: isServerConfigurationLoaded, value: true },
Expand Down Expand Up @@ -197,6 +199,7 @@ describe('Api Service', () => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
{ provide: CookiesService, useFactory: () => instance(mock(CookiesService)) },
provideMockStore({
selectors: [
{ selector: isServerConfigurationLoaded, value: true },
Expand Down Expand Up @@ -402,6 +405,7 @@ describe('Api Service', () => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
{ provide: CookiesService, useFactory: () => instance(mock(CookiesService)) },
provideMockStore({
selectors: [
{ selector: isServerConfigurationLoaded, value: true },
Expand Down Expand Up @@ -557,6 +561,7 @@ describe('Api Service', () => {
TestBed.configureTestingModule({
// https://angular.io/guide/http#testing-http-requests
imports: [CoreStoreModule.forTesting(['configuration', 'serverConfig']), HttpClientTestingModule],
providers: [{ provide: CookiesService, useFactory: () => instance(mock(CookiesService)) }],
});

apiService = TestBed.inject(ApiService);
Expand Down Expand Up @@ -680,6 +685,7 @@ describe('Api Service', () => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
{ provide: CookiesService, useFactory: () => instance(mock(CookiesService)) },
provideMockStore({
selectors: [
{ selector: isServerConfigurationLoaded, value: true },
Expand Down
38 changes: 35 additions & 3 deletions src/app/core/services/api/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
import { communicationTimeoutError, serverError } from 'ish-core/store/core/error';
import { isServerConfigurationLoaded } from 'ish-core/store/core/server-config';
import { getLoggedInCustomer, getLoggedInUser, getPGID } from 'ish-core/store/customer/user';
import { ApiTokenCookie } from 'ish-core/utils/api-token/api-token.service';
import { CookiesService } from 'ish-core/utils/cookies/cookies.service';
import { whenTruthy } from 'ish-core/utils/operators';
import { encodeResourceID } from 'ish-core/utils/url-resource-ids';

Expand Down Expand Up @@ -68,7 +70,7 @@ export class ApiService {
static TOKEN_HEADER_KEY = 'authentication-token';
static AUTHORIZATION_HEADER_KEY = 'Authorization';

constructor(private httpClient: HttpClient, private store: Store) {}
constructor(private httpClient: HttpClient, private store: Store, private cookiesService: CookiesService) {}

/**
- * sets the request header for the appropriate captcha service
Expand Down Expand Up @@ -131,15 +133,29 @@ export class ApiService {
if (path.startsWith('http://') || path.startsWith('https://')) {
return of(path);
}

// get current apiToken cookie information
const apiToken = this.getApiTokenFromCookie();

return combineLatest([
this.store.pipe(select(getRestEndpoint), whenTruthy()),
this.getLocale$(options),
this.getCurrency$(options),
of('/'),
of(path.includes('/') ? path.split('/')[0] : path),
// pgid
this.store.pipe(
select(getPGID),
iif(
() => !!apiToken,
// when an apiToken is available, then the pgid has to be set when the options are enabled
of(true).pipe(
withLatestFrom(
this.store.pipe(select(getPGID), options?.sendPGID || options?.sendSPGID ? whenTruthy() : identity)
),
map(([, pgid]) => pgid)
),
// when no apiToken is available, the stream does not to wait for a truthy pgid
this.store.pipe(select(getPGID))
).pipe(
map(pgid => (options?.sendPGID && pgid ? `;pgid=${pgid}` : options?.sendSPGID && pgid ? `;spgid=${pgid}` : ''))
),
// remaining path
Expand Down Expand Up @@ -200,6 +216,22 @@ export class ApiService {
]);
}

/**
* retrieve an apiToken when it is assigned to an authenticated user
*/
private getApiTokenFromCookie(): string {
const cookieContent = this.cookiesService.get('apiToken');
if (cookieContent) {
try {
const apiTokenCookie: ApiTokenCookie = JSON.parse(cookieContent);
return !apiTokenCookie?.isAnonymous ? apiTokenCookie.apiToken : undefined;
} catch (err) {
// ignore
}
}
return;
}

/**
* http get request
*/
Expand Down
2 changes: 1 addition & 1 deletion src/app/core/utils/api-token/api-token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { mapToProperty, whenTruthy } from 'ish-core/utils/operators';

type ApiTokenCookieType = 'user' | 'order' | 'anonymous';

interface ApiTokenCookie {
export interface ApiTokenCookie {
apiToken: string;
type: ApiTokenCookieType;
isAnonymous?: boolean;
Expand Down

0 comments on commit 1459e38

Please sign in to comment.