Skip to content

Commit 2114698

Browse files
author
Jérémy Tellaa
committed
Merge branch 'quentin/admind' into 'master'
refactor(functional-tests): use new admind API See merge request Tanker/sdk-js!474
2 parents 8cfc5bb + 67b2f69 commit 2114698

File tree

7 files changed

+86
-94
lines changed

7 files changed

+86
-94
lines changed

config/karma/tanker.test.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const plugin = new webpack.EnvironmentPlugin([
55
'CI',
66

77
'TANKER_ID_TOKEN',
8+
'TANKER_ADMIND_URL',
89
'TANKER_FAKE_AUTH_URL',
910
'TANKER_TRUSTCHAIND_URL',
1011

packages/functional-tests/src/helpers/AppHelper.js

+31-26
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import { tcrypto, utils } from '@tanker/crypto';
77
import { createIdentity } from '@tanker/identity';
88
import { uuid } from '@tanker/test-utils';
99

10-
import { AuthenticatedRequester } from './AuthenticatedRequester';
10+
import { requestTrustchaind, requestAdmindWithAuth } from './request';
1111
import { oidcSettings, storageSettings } from './config';
1212

13+
function toUnpaddedSafeBase64(str: Uint8Array): string {
14+
const b64 = utils.toSafeBase64(str);
15+
return b64.substring(0, b64.indexOf('='));
16+
}
17+
1318
function makeRootBlock(appKeyPair: Object) {
1419
const rootBlock = {
1520
trustchain_id: new Uint8Array(0),
@@ -25,13 +30,11 @@ function makeRootBlock(appKeyPair: Object) {
2530
}
2631

2732
export class AppHelper {
28-
_requester: AuthenticatedRequester;
2933
appId: Uint8Array;
3034
appKeyPair: Object;
3135
authToken: string;
3236

33-
constructor(requester: AuthenticatedRequester, appId: Uint8Array, appKeyPair: Object, authToken: string) {
34-
this._requester = requester;
37+
constructor(appId: Uint8Array, appKeyPair: Object, authToken: string) {
3538
this.appId = appId;
3639
this.appKeyPair = appKeyPair;
3740
this.authToken = authToken;
@@ -40,37 +43,38 @@ export class AppHelper {
4043
static async newApp(): Promise<AppHelper> {
4144
const appKeyPair = tcrypto.makeSignKeyPair();
4245
const rootBlock = makeRootBlock(appKeyPair);
43-
const message = {
46+
const body = {
4447
root_block: utils.toBase64(serializeBlock(rootBlock)),
4548
name: `functest-${uuid.v4()}`,
46-
is_test: true,
4749
private_signature_key: utils.toBase64(appKeyPair.privateKey),
4850
};
49-
const requester = await AuthenticatedRequester.open();
50-
const createResponse = await requester.send('create trustchain', message);
51-
const authToken = createResponse.auth_token;
51+
const createResponse = await requestAdmindWithAuth({ method: 'POST', path: '/apps', body });
52+
const authToken = createResponse.app.auth_token;
5253
const appId = rootBlock.trustchain_id;
53-
return new AppHelper(requester, appId, appKeyPair, authToken);
54+
return new AppHelper(appId, appKeyPair, authToken);
55+
}
56+
57+
async _update(body: Object): Promise<Object> {
58+
await requestAdmindWithAuth({
59+
method: 'PATCH',
60+
path: `/apps/${toUnpaddedSafeBase64(this.appId)}`,
61+
body,
62+
});
5463
}
5564

5665
async setOIDC() {
57-
await this._requester.send('update trustchain', {
58-
id: utils.toBase64(this.appId),
66+
await this._update({
5967
oidc_provider: 'google',
6068
oidc_client_id: oidcSettings.googleAuth.clientId,
6169
});
6270
}
6371

6472
async unsetOIDC() {
65-
await this._requester.send('update trustchain', {
66-
id: utils.toBase64(this.appId),
67-
oidc_provider: 'none',
68-
});
73+
await this._update({ oidc_provider: 'none' });
6974
}
7075

7176
async setS3() {
72-
await this._requester.send('update trustchain', {
73-
id: utils.toBase64(this.appId),
77+
await this._update({
7478
storage_provider: 's3',
7579
storage_bucket_name: storageSettings.s3.bucketName,
7680
storage_bucket_region: storageSettings.s3.bucketRegion,
@@ -80,10 +84,7 @@ export class AppHelper {
8084
}
8185

8286
async unsetS3() {
83-
await this._requester.send('update trustchain', {
84-
id: utils.toBase64(this.appId),
85-
storage_provider: 'none',
86-
});
87+
await this._update({ storage_provider: 'none' });
8788
}
8889

8990
generateIdentity(userId?: string): Promise<b64string> {
@@ -92,11 +93,12 @@ export class AppHelper {
9293
}
9394

9495
async getVerificationCode(email: string): Promise<string> {
95-
const msg = {
96-
trustchain_id: utils.toBase64(this.appId),
96+
const body = {
97+
app_id: utils.toBase64(this.appId),
9798
email,
99+
auth_token: this.authToken,
98100
};
99-
const answer = await this._requester.send('get verification code', msg);
101+
const answer = await requestTrustchaind({ method: 'POST', path: '/verification/email/code', body });
100102
if (!answer.verification_code) {
101103
throw new Error('Invalid response');
102104
}
@@ -113,6 +115,9 @@ export class AppHelper {
113115
}
114116

115117
async cleanup(): Promise<void> {
116-
await this._requester.send('delete trustchain', { id: utils.toBase64(this.appId) });
118+
await requestAdmindWithAuth({
119+
method: 'DELETE',
120+
path: `/apps/${toUnpaddedSafeBase64(this.appId)}`
121+
});
117122
}
118123
}

packages/functional-tests/src/helpers/AuthenticatedRequester.js

-64
This file was deleted.

packages/functional-tests/src/helpers/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Flow trickery, webpack will actually throw for undefined value
33
// https://github.com/webpack/webpack/blob/0740909b901afa69fcc1657a03215d1e011bb5c3/lib/EnvironmentPlugin.js#L41
44
const tankerUrl = process.env.TANKER_TRUSTCHAIND_URL || '';
5+
const admindUrl = process.env.TANKER_ADMIND_URL || '';
56
const fakeAuthUrl = process.env.TANKER_FAKE_AUTH_URL || '';
67
const idToken = process.env.TANKER_ID_TOKEN || '';
78
const oidcSettings = {
@@ -30,4 +31,4 @@ const storageSettings = {
3031
},
3132
};
3233

33-
export { tankerUrl, fakeAuthUrl, idToken, oidcSettings, storageSettings };
34+
export { tankerUrl, admindUrl, fakeAuthUrl, idToken, oidcSettings, storageSettings };

packages/functional-tests/src/helpers/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @flow
2-
export { tankerUrl, fakeAuthUrl, idToken, oidcSettings } from './config';
2+
export { admindUrl, tankerUrl, fakeAuthUrl, idToken, oidcSettings } from './config';
33
export { expectProgressReport, expectType, expectSameType, expectDeepEqual } from './expectations';
44
export { makePrefix } from './makePrefix';
55
export { makeRandomUint8Array } from './makeRandomUint8Array';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// @flow
2+
import fetchPonyfill from 'fetch-ponyfill';
3+
4+
import { admindUrl, tankerUrl, idToken } from './config';
5+
6+
const { fetch } = fetchPonyfill({ Promise });
7+
8+
type stringToAnyMap = { [string]: any, ...};
9+
10+
export type Method = 'DELETE' | 'GET' | 'PATCH' | 'POST' | 'PUT';
11+
export type Request = {| method: Method, path: string, query?: stringToAnyMap, headers?: stringToAnyMap, body?: stringToAnyMap |};
12+
13+
const stringify = (param: stringToAnyMap | string) => (
14+
typeof (param) === 'object'
15+
? JSON.stringify(param)
16+
: param
17+
);
18+
19+
const buildQuery = (params: stringToAnyMap = {}) => (
20+
Object.keys(params)
21+
.map(key => `${key}=${encodeURIComponent(stringify(params[key]))}`)
22+
.join('&')
23+
);
24+
25+
const request = async (url: string, { method, path, query, headers = {}, body }: Request): Promise<stringToAnyMap> => {
26+
const response = await fetch(
27+
url + path + (query ? `?${buildQuery(query)}` : ''),
28+
{
29+
method,
30+
headers: { 'Content-Type': 'application/json', ...headers },
31+
...(body ? { body: JSON.stringify(body) } : {}),
32+
}
33+
);
34+
35+
const parsed = await response.json();
36+
37+
if (parsed.error)
38+
throw new Error(parsed.error.code);
39+
40+
return parsed;
41+
};
42+
43+
export const requestTrustchaind = async (req: Request): Promise<stringToAnyMap> => request(tankerUrl, req);
44+
export const requestAdmind = async (req: Request): Promise<stringToAnyMap> => request(admindUrl, req);
45+
46+
export const requestAdmindWithAuth = async (req: Request): Promise<stringToAnyMap> => requestAdmind({
47+
...req,
48+
headers: { ...req.headers, Authorization: `Bearer ${idToken}` },
49+
});

packages/functional-tests/src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { utils } from '@tanker/crypto';
33
import type { Tanker, b64string } from '@tanker/core';
44
import { silencer } from '@tanker/test-utils';
55

6-
import { AppHelper, tankerUrl, idToken, oidcSettings } from './helpers';
6+
import { AppHelper, admindUrl, tankerUrl, idToken, oidcSettings } from './helpers';
77
import type { TestArgs, TestResources } from './helpers';
88

99
import { generateEncryptorStreamTests } from './encryptorStream';
@@ -23,7 +23,7 @@ export function generateFunctionalTests(
2323
makeTanker: (appId: b64string) => Tanker,
2424
generateTestResources: () => TestResources,
2525
) {
26-
if (!tankerUrl || !idToken || !oidcSettings) {
26+
if (!admindUrl || !tankerUrl || !idToken || !oidcSettings) {
2727
// Those functional tests create an app automatically and require a TANKER_CONFIG_NAME
2828
// and TANKER_CONFIG_FILEPATH to run
2929
if (process.env.CI) {

0 commit comments

Comments
 (0)