Skip to content

Commit

Permalink
Merge pull request #74 from superfaceai/feature/get_profile__authenti…
Browse files Browse the repository at this point in the history
…cate_option

Add authenticate option to `getProfile*` functions
  • Loading branch information
janhalama authored Nov 24, 2021
2 parents 745d585 + ca053c2 commit c69beb1
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 45 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- `authenticate` option to `getProfile`, `getProfileAST`, `getProfileSource` functions

## [1.0.1] - 2021-11-23
### Fixed
Expand Down
153 changes: 116 additions & 37 deletions src/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1076,22 +1076,22 @@ describe('client', () => {
});
});
describe('getProfile', () => {
it('should get one profile', async () => {
const mockResult: ProfileVersionResponse = {
profile_id: 'testId',
profile_name: 'testName',
profile_version: '1.0.0',
url: 'testUrl',
published_at: new Date(),
published_by: 'test',
owner: 'testOwner',
owner_url: 'testOwnerUrl',
};
const mockResult: ProfileVersionResponse = {
profile_id: 'testId',
profile_name: 'testName',
profile_version: '1.0.0',
url: 'testUrl',
published_at: new Date(),
published_by: 'test',
owner: 'testOwner',
owner_url: 'testOwnerUrl',
};
const mockResponse = {
ok: true,
json: async () => mockResult,
};

const mockResponse = {
ok: true,
json: async () => mockResult,
};
it('should get one profile', async () => {
const fetchMock = jest
.spyOn(client, 'fetch')
.mockResolvedValue(mockResponse as Response);
Expand All @@ -1112,6 +1112,29 @@ describe('client', () => {
});
});

it('should authenticate user', async () => {
const fetchMock = jest
.spyOn(client, 'fetch')
.mockResolvedValue(mockResponse as Response);
await client.getProfile(
{
name: 'user-repos',
version: '1.0.0',
scope: 'vcs',
},
{
authenticate: true,
}
);
expect(fetchMock).toBeCalledWith('/vcs/[email protected]', {
authenticate: true,
method: 'GET',
headers: {
Accept: MEDIA_TYPE_JSON,
},
});
});

it('should throw error', async () => {
const payload = {
status: 404,
Expand Down Expand Up @@ -1145,21 +1168,21 @@ describe('client', () => {
});

describe('getProfilesList', () => {
it('should get list of profiles', async () => {
const mockResult: ProfilesListResponse = {
url: '/profiles',
data: [
{
id: 'scope/profile-name',
url: 'https://superface.test/scope/profile-name',
},
],
};
const mockResult: ProfilesListResponse = {
url: '/profiles',
data: [
{
id: 'scope/profile-name',
url: 'https://superface.test/scope/profile-name',
},
],
};
const mockResponse = {
ok: true,
json: async () => mockResult,
};

const mockResponse = {
ok: true,
json: async () => mockResult,
};
it('should get list of profiles', async () => {
const fetchMock = jest
.spyOn(client, 'fetch')
.mockResolvedValue(mockResponse as Response);
Expand Down Expand Up @@ -1192,6 +1215,18 @@ describe('client', () => {
);
});

it('should authenticate user', async () => {
const fetchMock = jest
.spyOn(client, 'fetch')
.mockResolvedValue(mockResponse as Response);
await client.getProfilesList({ authenticate: true });
expect(fetchMock).toBeCalledWith('/profiles', {
authenticate: true,
method: 'GET',
headers: { Accept: MEDIA_TYPE_JSON },
});
});

it('should throw error', async () => {
const payload = {
status: 400,
Expand Down Expand Up @@ -1219,11 +1254,12 @@ describe('client', () => {
});

describe('getProfileSource', () => {
const mockResponse = {
ok: true,
text: async () => 'profileSource',
};

it('should get profile source', async () => {
const mockResponse = {
ok: true,
text: async () => 'profileSource',
};
const fetchMock = jest
.spyOn(client, 'fetch')
.mockResolvedValue(mockResponse as Response);
Expand All @@ -1244,6 +1280,27 @@ describe('client', () => {
});
});

it('should authenticate user', async () => {
const fetchMock = jest
.spyOn(client, 'fetch')
.mockResolvedValue(mockResponse as Response);
await client.getProfileSource(
{
name: 'user-repos',
version: '1.0.0',
scope: 'vcs',
},
{ authenticate: true }
);
expect(fetchMock).toBeCalledWith('/vcs/[email protected]', {
authenticate: true,
method: 'GET',
headers: {
Accept: MEDIA_TYPE_PROFILE,
},
});
});

it('should throw error', async () => {
const payload = {
status: 404,
Expand Down Expand Up @@ -1277,11 +1334,12 @@ describe('client', () => {
});

describe('getProfileAST', () => {
const mockResponse = {
ok: true,
text: async () => 'profileAST',
};

it('should get profile AST', async () => {
const mockResponse = {
ok: true,
text: async () => 'profileAST',
};
const fetchMock = jest
.spyOn(client, 'fetch')
.mockResolvedValue(mockResponse as Response);
Expand All @@ -1302,6 +1360,27 @@ describe('client', () => {
});
});

it('should authenticate user', async () => {
const fetchMock = jest
.spyOn(client, 'fetch')
.mockResolvedValue(mockResponse as Response);
await client.getProfileAST(
{
name: 'user-repos',
version: '1.0.0',
scope: 'vcs',
},
{ authenticate: true }
);
expect(fetchMock).toBeCalledWith('/vcs/[email protected]', {
authenticate: true,
method: 'GET',
headers: {
Accept: MEDIA_TYPE_PROFILE_AST,
},
});
});

it('should throw error', async () => {
const payload = {
status: 404,
Expand Down
24 changes: 17 additions & 7 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
MapsListOptions,
MapsListResponse,
ProfileId,
ProfileOptions,
ProfilesListOptions,
ProfilesListResponse,
ProfileVersionResponse,
Expand Down Expand Up @@ -304,9 +305,12 @@ export class ServiceClient {
return (await this.unwrap(response)).text();
}

async getProfile(profileId: ProfileId): Promise<ProfileVersionResponse> {
async getProfile(
profileId: ProfileId,
options?: ProfileOptions
): Promise<ProfileVersionResponse> {
const response: Response = await this.fetch(buildProfileUrl(profileId), {
authenticate: false,
authenticate: options?.authenticate ?? false,
method: 'GET',
headers: {
Accept: MEDIA_TYPE_JSON,
Expand All @@ -317,9 +321,12 @@ export class ServiceClient {
return (await response.json()) as ProfileVersionResponse;
}

async getProfileSource(profileId: ProfileId): Promise<string> {
async getProfileSource(
profileId: ProfileId,
options?: ProfileOptions
): Promise<string> {
const response: Response = await this.fetch(buildProfileUrl(profileId), {
authenticate: false,
authenticate: options?.authenticate ?? false,
method: 'GET',
headers: {
Accept: MEDIA_TYPE_PROFILE,
Expand All @@ -329,9 +336,12 @@ export class ServiceClient {
return (await this.unwrap(response)).text();
}

async getProfileAST(profileId: ProfileId): Promise<string> {
async getProfileAST(
profileId: ProfileId,
options?: ProfileOptions
): Promise<string> {
const response: Response = await this.fetch(buildProfileUrl(profileId), {
authenticate: false,
authenticate: options?.authenticate ?? false,
method: 'GET',
headers: {
Accept: MEDIA_TYPE_PROFILE_AST,
Expand All @@ -352,7 +362,7 @@ export class ServiceClient {
});

const response: Response = await this.fetch(url, {
authenticate: false,
authenticate: options?.authenticate ?? false,
method: 'GET',
headers: { Accept: 'application/json' },
});
Expand Down
6 changes: 5 additions & 1 deletion src/interfaces/profiles_api_options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export interface ProfilesListOptions {
export interface ProfilesListOptions extends ProfileOptions {
limit?: number;
accountHandle?: string;
}

export interface ProfileOptions {
authenticate?: boolean;
}

0 comments on commit c69beb1

Please sign in to comment.