Skip to content

Commit

Permalink
refactor(APIClient): use mock cells config for non-cells instances
Browse files Browse the repository at this point in the history
  • Loading branch information
olafsulich committed Feb 26, 2025
1 parent 6f7b80c commit de5740d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
47 changes: 18 additions & 29 deletions packages/api-client/src/APIClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,12 @@ import {AuthAPI} from './auth/AuthAPI';
import {ClientType} from './client';
import {Config} from './Config';
import {BackendErrorLabel, StatusCode} from './http';
import {cellsConfigMock} from './mocks/cells';
import {Self, SelfAPI} from './self';
import {UserAPI} from './user/UserAPI';

const testConfig = {
cells: {
s3: {
apiKey: 'test-api-key',
bucket: 'test-bucket',
endpoint: 'test-endpoint',
region: 'test-region',
},
pydio: {
url: 'https://test-pydio.wire.com',
segment: '/cells/api',
apiKey: 'test-pydio-api-key',
},
},
cells: cellsConfigMock,
} as Config;

describe('APIClient', () => {
Expand Down Expand Up @@ -80,7 +69,7 @@ describe('APIClient', () => {

describe('constructor', () => {
it('constructs a client with production backend and StoreEngine by default', () => {
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);
expect(client.transport.http['client'].defaults.baseURL).toBe(APIClient.BACKEND.PRODUCTION.rest);
expect(client.transport.ws['baseUrl']).toBe(APIClient.BACKEND.PRODUCTION.ws);
});
Expand All @@ -92,7 +81,7 @@ describe('APIClient', () => {
});

it('constructs URLs when only the StoreEngine is provided', () => {
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);
expect(client.transport.http['client'].defaults.baseURL).toBe(APIClient.BACKEND.PRODUCTION.rest);
expect(client.transport.ws['baseUrl']).toBe(APIClient.BACKEND.PRODUCTION.ws);
});
Expand All @@ -103,7 +92,7 @@ describe('APIClient', () => {
nock(baseUrl)
.get('/api-version')
.reply(200, {supported: [0, 1]});
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);
let errorMessage;
try {
await client.useVersion(2, 3);
Expand All @@ -116,7 +105,7 @@ describe('APIClient', () => {

it("uses version 0 if backend doesn't support /api-version endpoint", async () => {
nock(baseUrl).get('/api-version').reply(404);
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);
const {version} = await client.useVersion(0, 3);
expect(version).toBe(0);
});
Expand All @@ -125,7 +114,7 @@ describe('APIClient', () => {
nock(baseUrl)
.get('/api-version')
.reply(200, {supported: [0, 1]});
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);
const {version, isFederated, federationEndpoints} = await client.useVersion(0, 2);
expect(version).toBe(1);
expect(isFederated).toBe(false);
Expand All @@ -136,7 +125,7 @@ describe('APIClient', () => {
nock(baseUrl)
.get('/api-version')
.reply(200, {supported: [0, 1], development: [2]});
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);
const {version, isFederated, federationEndpoints} = await client.useVersion(0, 2, true);
expect(version).toBe(2);
expect(isFederated).toBe(false);
Expand All @@ -147,7 +136,7 @@ describe('APIClient', () => {
nock(baseUrl)
.get('/api-version')
.reply(200, {supported: [0, 1], development: [2]});
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);
const {version, isFederated, federationEndpoints} = await client.useVersion(0, 2, false);
expect(version).toBe(1);
expect(isFederated).toBe(false);
Expand All @@ -158,7 +147,7 @@ describe('APIClient', () => {
nock(baseUrl)
.get('/api-version')
.reply(200, {supported: [0, 1], development: [2]});
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);
const {version, isFederated, federationEndpoints} = await client.useVersion(0, 1, true);
expect(version).toBe(1);
expect(isFederated).toBe(false);
Expand All @@ -168,7 +157,7 @@ describe('APIClient', () => {
it('returns the backend federation state', async () => {
const response: BackendVersionResponse = {supported: [0, 1], federation: true};
nock(baseUrl).get('/api-version').reply(200, response);
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);
const {isFederated} = await client.useVersion(0, 2);
expect(isFederated).toBe(true);
});
Expand Down Expand Up @@ -248,15 +237,15 @@ describe('APIClient', () => {
});

it('creates a context from a successful login', async () => {
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);
const context = await client.login(loginData);
expect(context.userId).toBe(accessTokenData.user);
expect(client['accessTokenStore'].accessToken?.access_token).toBe(accessTokenData.access_token);
});

// eslint-disable-next-line jest/expect-expect
it('can login after a logout', async () => {
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);
await client.login(loginData);
return client.logout();
});
Expand All @@ -274,7 +263,7 @@ describe('APIClient', () => {

nock(baseUrl).post(AuthAPI.URL.ACCESS).reply(StatusCode.OK, accessTokenData);

const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);
const context = await client.login(loginData);
expect(context.userId).toBe(accessTokenData.user);
// Make access token invalid
Expand All @@ -292,15 +281,15 @@ describe('APIClient', () => {

// eslint-disable-next-line jest/expect-expect
it('can logout a user', async () => {
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);

client.context = await client['createContext']('3721e5d3-558d-45ac-b476-b4a64a8f74c1', ClientType.TEMPORARY);

await client.logout();
});

it('ignores errors', async () => {
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);
const testError = new Error('Test rejection');

jest.spyOn(client.api.auth, 'postLogout').mockReturnValue(Promise.reject(testError));
Expand All @@ -313,7 +302,7 @@ describe('APIClient', () => {
});

it('skips request when told to', async () => {
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);

jest.spyOn(client.api.auth, 'postLogout');
jest.spyOn(client, 'disconnect').mockReturnValue();
Expand Down Expand Up @@ -343,7 +332,7 @@ describe('APIClient', () => {
});

it('automatically gets an access token after registration', async () => {
const client = new APIClient({...testConfig});
const client = new APIClient(testConfig);

const context = await client.register(registerData);
expect(context.userId).toBe(registerData.id);
Expand Down
1 change: 1 addition & 0 deletions packages/api-client/src/APIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ export class APIClient extends EventEmitter {

const assetAPI = new AssetAPI(this.transport.http, backendFeatures);

// To not mark "cells" as optional, we use the mock config for APIClient instances that are not using the cells feature.
const cellsConfig = this.config.cells || cellsConfigMock;

return {
Expand Down

0 comments on commit de5740d

Please sign in to comment.