Skip to content

Commit

Permalink
[SDK] Nullable credentials (#841)
Browse files Browse the repository at this point in the history
* Add nullable credentials in storage SDK

* Update StorageClient references
  • Loading branch information
flopez7 authored Aug 29, 2023
1 parent 8615c14 commit 3039dfc
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export class JobService {
};

this.storageClient = new StorageClient(
storageCredentials,
this.storageParams,
storageCredentials,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ export class JobService {
this.bucket = this.configService.get<string>(ConfigNames.S3_BUCKET)!;

this.storageClient = new StorageClient(
storageCredentials,
this.storageParams,
storageCredentials,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { ConfigNames } from '../../common/config';
import { WebhookStatus } from '../../common/enums';
import { JobRequestType } from '../../common/enums';
import { ReputationEntityType } from '../../common/enums';
import {isInstance} from "class-validator";
import { isInstance } from 'class-validator';

@Injectable()
export class WebhookService {
Expand Down Expand Up @@ -66,8 +66,8 @@ export class WebhookService {
this.bucket = this.configService.get<string>(ConfigNames.S3_BUCKET)!;

this.storageClient = new StorageClient(
storageCredentials,
this.storageParams,
storageCredentials,
);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/typescript/human-protocol-sdk/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ export class StorageClient {
/**
* **Storage client constructor**
*
* @param {StorageCredentials} credentials - Cloud storage access data
* @param {StorageParams} params - Cloud storage params
* @param {StorageCredentials} credentials - Optional. Cloud storage access data. If credentials is not provided - use an anonymous access to the bucket
*/
constructor(credentials: StorageCredentials, params: StorageParams) {
constructor(params: StorageParams, credentials?: StorageCredentials) {
try {
this.clientParams = params;

this.client = new Minio.Client({
...params,
accessKey: credentials.accessKey,
secretKey: credentials.secretKey,
accessKey: credentials?.accessKey ?? '',
secretKey: credentials?.secretKey ?? '',
});
} catch (e) {
throw ErrorStorageClientNotInitialized;
Expand Down
33 changes: 3 additions & 30 deletions packages/sdk/typescript/human-protocol-sdk/test/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,56 +80,29 @@ describe('Storage tests', () => {
});

test('should init client with empty credentials', async () => {
const storageCredentials: StorageCredentials = {
accessKey: '',
secretKey: '',
};

const storageParams: StorageParams = {
endPoint: DEFAULT_ENDPOINT,
port: DEFAULT_PORT,
useSSL: DEFAULT_USE_SSL,
};

const storageClient = new StorageClient(
storageCredentials,
storageParams
);
const storageClient = new StorageClient(storageParams);

expect(storageClient).toBeInstanceOf(StorageClient);
});

test('should not init client with an error', async () => {
// TODO: Adapt it for particular test case
/* vi.mock('../src/storage', () => {
const StorageClient = vi.fn().mockImplementation(() => {
throw ErrorStorageClientNotInitialized;
});
return {
default: StorageClient
}
}) */
// expect(() => new StorageClient(storageCredentials, storageParams)).toThrow(ErrorStorageClientNotInitialized);
});
});

describe('Client anonymous access', () => {
let storageClient: StorageClient;

beforeAll(async () => {
const storageCredentials: StorageCredentials = {
accessKey: '',
secretKey: '',
};

const storageParams: StorageParams = {
endPoint: DEFAULT_ENDPOINT,
port: DEFAULT_PORT,
useSSL: DEFAULT_USE_SSL,
};

storageClient = new StorageClient(storageCredentials, storageParams);
storageClient = new StorageClient(storageParams);
});

test('should return the bucket exists', async () => {
Expand Down Expand Up @@ -305,7 +278,7 @@ describe('Storage tests', () => {
useSSL: DEFAULT_USE_SSL,
};

storageClient = new StorageClient(storageCredentials, storageParams);
storageClient = new StorageClient(storageParams, storageCredentials);
});

test('should return the bucket exists', async () => {
Expand Down

1 comment on commit 3039dfc

@vercel
Copy link

@vercel vercel bot commented on 3039dfc Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

job-launcher-server – ./packages/apps/job-launcher/server

job-launcher-server-nine.vercel.app
job-launcher-server-git-develop-humanprotocol.vercel.app
job-launcher-server-humanprotocol.vercel.app

Please sign in to comment.