Skip to content

Commit

Permalink
feat: Add tests, refactored gitlab client
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyshevch committed Aug 24, 2023
1 parent 0946e52 commit 8577210
Show file tree
Hide file tree
Showing 4 changed files with 4,827 additions and 4,289 deletions.
178 changes: 178 additions & 0 deletions packages/gitlab-backend/src/service/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ describe('createRouter', () => {
})
);
}
),
rest.post(
'https://non-existing-example.com/api/graphql',
(req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
url: req.url.toString(),
headers: req.headers.all(),
})
);
}
),
rest.post(
'https://non-existing-example-2.com/api/graphql',
(req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
url: req.url.toString(),
headers: req.headers.all(),
})
);
}
)
);

Expand Down Expand Up @@ -114,6 +138,71 @@ describe('createRouter', () => {
});
});

describe('Graphql requests', () => {
it('First instance should work', async () => {
const agent = request.agent(app);
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.post(
'/api/gitlab/non-existing-example.com/graphql'
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
headers: {
'accept-encoding': 'gzip, deflate',
connection: 'close',
'content-length': '0',
host: 'non-existing-example.com',
'user-agent': 'supertest',
},
url: 'https://non-existing-example.com/api/graphql',
});
});

it('Second instance should work', async () => {
const agent = request.agent(app);
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.post(
'/api/gitlab/non-existing-example-2.com/graphql'
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
headers: {
'accept-encoding': 'gzip, deflate',
connection: 'close',
'content-length': '0',
host: 'non-existing-example-2.com',
'user-agent': 'supertest',
},
url: 'https://non-existing-example-2.com/api/graphql',
});
});

it('Methods different from POST should reject', async () => {
const agent = request.agent(app);
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
for (const method of [
// Get will be received by main server
'delete',
'put',
'options',
'trace',
'patch',
]) {
// @ts-ignore
const response = await agent?.[method](
'/api/gitlab/non-existing-example.com/graphql'
);

console.log(method);
expect(response.status).toEqual(404);
expect(response.body).toEqual({});
}
});
});

describe('Error requests', () => {
it('Methods different from GET should reject', async () => {
const agent = request.agent(app);
Expand Down Expand Up @@ -175,6 +264,30 @@ describe('createRouter with baseUrl', () => {
})
);
}
),
rest.post(
'https://non-existing-example.com/api/graphql',
(req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
url: req.url.toString(),
headers: req.headers.all(),
})
);
}
),
rest.post(
'https://non-existing-example-2.com/api/graphql',
(req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
url: req.url.toString(),
headers: req.headers.all(),
})
);
}
)
);

Expand Down Expand Up @@ -271,6 +384,71 @@ describe('createRouter with baseUrl', () => {
});
});

describe('Graphql requests', () => {
it('First instance should work', async () => {
const agent = request.agent(app);
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.post(
`${basePath}/api/gitlab/non-existing-example.com/graphql`
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
headers: {
'accept-encoding': 'gzip, deflate',
connection: 'close',
'content-length': '0',
host: 'non-existing-example.com',
'user-agent': 'supertest',
},
url: 'https://non-existing-example.com/api/graphql',
});
});

it('Second instance should work', async () => {
const agent = request.agent(app);
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
const response = await agent.post(
`${basePath}/api/gitlab/non-existing-example-2.com/graphql`
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
headers: {
'accept-encoding': 'gzip, deflate',
connection: 'close',
'content-length': '0',
host: 'non-existing-example-2.com',
'user-agent': 'supertest',
},
url: 'https://non-existing-example-2.com/api/graphql',
});
});

it('Methods different from POST should reject', async () => {
const agent = request.agent(app);
// this is set to let msw pass test requests through the mock server
agent.set('User-Agent', 'supertest');
for (const method of [
// Get will be received by main server
'delete',
'put',
'options',
'trace',
'patch',
]) {
// @ts-ignore
const response = await agent?.[method](
`${basePath}/api/gitlab/non-existing-example.com/graphql`
);

console.log(method);
expect(response.status).toEqual(404);
expect(response.body).toEqual({});
}
});
});

describe('Error requests', () => {
it('Methods different from GET should reject', async () => {
const agent = request.agent(app);
Expand Down
1 change: 0 additions & 1 deletion packages/gitlab-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export async function createRouter(
},
secure,
logProvider: () => logger,
logLevel: 'info',
pathRewrite: {
[`^${basePath}/api/gitlab/${host}/graphql`]: `/api/graphql`,
},
Expand Down
76 changes: 40 additions & 36 deletions packages/gitlab/src/api/GitlabCIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import type {
} from '@gitbeaker/rest';
import dayjs from 'dayjs';

type GraphQLQuery = {
variables: Record<string, string>;
query: string;
};

export class GitlabCIClient implements GitlabCIApi {
discoveryApi: DiscoveryApi;
identityApi: IdentityApi;
Expand Down Expand Up @@ -108,6 +113,20 @@ export class GitlabCIClient implements GitlabCIApi {
return undefined;
}

protected async callGraphQLApi<T>(
query: GraphQLQuery
): Promise<T | undefined> {
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(query),
};

return this.callApi<T>('graphql', {}, options);
}

async getPipelineSummary(
projectID?: string | number
): Promise<PipelineSchema[] | undefined> {
Expand Down Expand Up @@ -275,44 +294,29 @@ export class GitlabCIClient implements GitlabCIApi {
): Promise<GitlabProjectCoverageResponse | undefined> {
if (!projectSlug) return undefined;

return await this.callApi<GitlabProjectCoverageResponse>(
'graphql',
{},
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
variables: {
projectSlug,
updatedAfter: dayjs()
.subtract(30, 'days')
.format('YYYY-MM-DD'),
},
query: /* GraphQL */ `
query getProjectCoverage(
$projectSlug: ID!
$updatedAfter: Time
) {
project(fullPath: $projectSlug) {
name
webUrl
pipelines(
ref: "main"
updatedAfter: $updatedAfter
) {
nodes {
coverage
createdAt
}
}
return await this.callGraphQLApi<GitlabProjectCoverageResponse>({
variables: {
projectSlug,
updatedAfter: dayjs().subtract(30, 'days').format('YYYY-MM-DD'),
},
query: /* GraphQL */ `
query getProjectCoverage(
$projectSlug: ID!
$updatedAfter: Time
) {
project(fullPath: $projectSlug) {
name
webUrl
pipelines(ref: "main", updatedAfter: $updatedAfter) {
nodes {
coverage
createdAt
}
}
`,
}),
}
);
}
}
`,
});
}

async getCodeOwners(
Expand Down
Loading

0 comments on commit 8577210

Please sign in to comment.