mattermost-mobile/app/client/rest/index.test.js
Miguel Alatzar 134c4a49c5
Integrate react-native-network-client (#5499)
* fix: handle NSMutableData

* feat: integrate react-native-network-client

* fix: typos

* fix: semicolon

* fix: rename to urlVersion

* fix: add returnDataOnly arg

* fix: configure network client

* fix: headers

* fix: handling of serverVersion

* fix: rename requests to actions

* fix: action imports

* fix: no need to stringify body

* fix: sso flow

* fix: address PR feedback

* fix: invalidate client on logout

* fix: address PR feedback take 2

* fix: address PR feedback take 3

* fix: tsc issues

* fix: get csrf token during client creation

* fix: linter

* fix: invalidate client onLogout

* fix: event emitter

* fix: unit tests

* fix: apply linter fixes

* fix lint

* Modify actions to add / update database values

* Rename clien4.d.ts to client.d.ts

* fix empty & missing translations

* cleanup api client

* Cleanup init & squash some TODO's

* Emit certificate errors in NetworkManager

* cleanup user actions

* Fix NetworkManager invalidate client

* Invalidate client when server screen appears

* Update kotlin to 1.4.30 required by network-client

* patch react-native-keychain to remove cached credential

* update react-native-network-client

* Use app.db instead of default.db in native code

* fix use of rnnc on Android

* Init PushNotifications

* No need to reset serverVersion on logout

* fix logout action

* fix deleteServerDatabase

* fix schedule expired session notification

* use safeParseJSON for db json fields

* unsubscribe when database component unmounts

* cleanup init

* session type

* pass launchprops to entire login flow

* Properly remove third party cookies after SSO login

* recreate network client if sso with redirect fails

* add missing launch props from server screen

* use query prefix for database queries

* Add temporary logout function to channel screen

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2021-07-06 11:16:35 -04:00

79 lines
2.6 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import assert from 'assert';
import nock from 'nock';
import {HEADER_X_VERSION_ID} from '@client/rest/constants';
import ClientError from '@client/rest/error';
import TestHelper from '@test/test_helper';
import {isMinimumServerVersion} from '@utils/helpers';
describe('Client', () => {
beforeAll(() => {
if (!nock.isActive()) {
nock.activate();
}
});
afterAll(() => {
nock.restore();
});
describe('doFetch', () => {
it('serverVersion should be set from response header', async () => {
const client = TestHelper.createClient();
assert.equal(client.serverVersion, '');
client.apiClient.get.mockReturnValueOnce({
code: 200,
data: {},
headers: {[HEADER_X_VERSION_ID]: '5.0.0.5.0.0.abc123'},
ok: true,
});
await client.getMe();
assert.equal(client.serverVersion, '5.0.0.5.0.0.abc123');
assert.equal(isMinimumServerVersion(client.serverVersion, 5, 0, 0), true);
assert.equal(isMinimumServerVersion(client.serverVersion, 5, 1, 0), false);
client.apiClient.get.mockReturnValueOnce({
code: 200,
data: {},
headers: {[HEADER_X_VERSION_ID]: '5.3.0.5.3.0.abc123'},
ok: true,
});
await client.getMe();
assert.equal(client.serverVersion, '5.3.0.5.3.0.abc123');
assert.equal(isMinimumServerVersion(client.serverVersion, 5, 0, 0), true);
assert.equal(isMinimumServerVersion(client.serverVersion, 5, 1, 0), true);
});
});
});
describe('ClientError', () => {
it('standard fields should be enumerable', () => {
const error = new ClientError('https://example.com', {
message: 'This is a message',
intl: {
id: 'test.error',
defaultMessage: 'This is a message with a translation',
},
server_error_id: 'test.app_error',
status_code: 418,
url: 'https://example.com/api/v4/error',
});
const copy = {...error};
assert.strictEqual(copy.message, error.message);
assert.strictEqual(copy.intl, error.intl);
assert.strictEqual(copy.server_error_id, error.server_error_id);
assert.strictEqual(copy.status_code, error.status_code);
assert.strictEqual(copy.url, error.url);
});
});