Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N21-2203 handle policies-info error responses #5262

Merged
merged 7 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/server/src/infra/schulconnex-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export { SchulconnexRestClientOptions } from './schulconnex-rest-client-options'
export { SchulconnexClientModule } from './schulconnex-client.module';
export { SchulconnexRestClient } from './schulconnex-rest-client';
export * from './response';
export { schulconnexResponseFactory, schulconnexPoliciesInfoResponseFactory } from './testing';
export { schulconnexResponseFactory, schulconnexPoliciesInfoLicenseResponseFactory } from './testing';
export { SchulconnexClientConfig } from './schulconnex-client-config';
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ export { SchulconnexPoliciesInfoTargetResponse } from './schulconnex-policies-in
export { SchulconnexPoliciesInfoResponse } from './schulconnex-policies-info-response';
export { SchulconnexPoliciesInfoActionType } from './schulconnex-policies-info-action-type';
export { SchulconnexPoliciesInfoPermissionResponse } from './schulconnex-policies-info-permission-response';
export { SchulconnexPoliciesInfoAccessControlResponse } from './schulconnex-policies-info-access-control-response';
export { SchulconnexPoliciesInfoErrorDescriptionResponse } from './schulconnex-policies-info-error-description-response';
export { SchulconnexPoliciesInfoLicenseResponse } from './schulconnex-policies-info-license-response';
export { SchulconnexPoliciesInfoErrorResponse } from './schulconnex-policies-info-error-response';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { SchulconnexPoliciesInfoErrorDescriptionResponse } from '@infra/schulconnex-client/';
import { Type } from 'class-transformer';
import { IsObject, IsString, ValidateNested } from 'class-validator';

export class SchulconnexPoliciesInfoAccessControlResponse {
@IsString()
'@type'!: string;

@IsObject()
@ValidateNested()
@Type(() => SchulconnexPoliciesInfoErrorDescriptionResponse)
error!: SchulconnexPoliciesInfoErrorDescriptionResponse;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IsString } from 'class-validator';

export class SchulconnexPoliciesInfoErrorDescriptionResponse {
@IsString()
code!: string;

@IsString()
value!: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { SchulconnexPoliciesInfoAccessControlResponse } from '@infra/schulconnex-client';
import { Type } from 'class-transformer';
import { IsObject, ValidateNested } from 'class-validator';

export class SchulconnexPoliciesInfoErrorResponse {
@IsObject()
@ValidateNested()
@Type(() => SchulconnexPoliciesInfoAccessControlResponse)
access_control!: SchulconnexPoliciesInfoAccessControlResponse;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
SchulconnexPoliciesInfoPermissionResponse,
SchulconnexPoliciesInfoTargetResponse,
} from '@infra/schulconnex-client';
import { Type } from 'class-transformer';
import { IsArray, IsObject, ValidateNested } from 'class-validator';

export class SchulconnexPoliciesInfoLicenseResponse {
@IsObject()
@ValidateNested()
@Type(() => SchulconnexPoliciesInfoTargetResponse)
target!: SchulconnexPoliciesInfoTargetResponse;

@IsArray()
@ValidateNested({ each: true })
@Type(() => SchulconnexPoliciesInfoPermissionResponse)
permission!: SchulconnexPoliciesInfoPermissionResponse[];
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { Type } from 'class-transformer';
import { IsArray, IsObject, ValidateNested } from 'class-validator';
import { SchulconnexPoliciesInfoPermissionResponse } from './schulconnex-policies-info-permission-response';
import { SchulconnexPoliciesInfoTargetResponse } from './schulconnex-policies-info-target-response';
import {
SchulconnexPoliciesInfoErrorResponse,
SchulconnexPoliciesInfoLicenseResponse,
} from '@infra/schulconnex-client';
import { PolymorphicArrayTransform } from '@shared/controller/transformer/polymorphic-array.transformer';
import { ClassConstructor } from 'class-transformer';
IgorCapCoder marked this conversation as resolved.
Show resolved Hide resolved
import { IsArray, ValidateNested } from 'class-validator';

export class SchulconnexPoliciesInfoResponse {
@IsObject()
@ValidateNested()
@Type(() => SchulconnexPoliciesInfoTargetResponse)
target!: SchulconnexPoliciesInfoTargetResponse;
const policiesInfoDiscriminator = (
obj: unknown
): ClassConstructor<SchulconnexPoliciesInfoLicenseResponse | SchulconnexPoliciesInfoErrorResponse> =>
typeof obj === 'object' && obj !== null && 'target' in obj && 'permission' in obj
? SchulconnexPoliciesInfoLicenseResponse
: SchulconnexPoliciesInfoErrorResponse;

export class SchulconnexPoliciesInfoResponse {
@IsArray()
@ValidateNested({ each: true })
@Type(() => SchulconnexPoliciesInfoPermissionResponse)
permission!: SchulconnexPoliciesInfoPermissionResponse[];
@PolymorphicArrayTransform(policiesInfoDiscriminator)
data!: (SchulconnexPoliciesInfoLicenseResponse | SchulconnexPoliciesInfoErrorResponse)[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export interface SchulconnexApiInterface {

getPersonenInfo(params: SchulconnexPersonenInfoParams): Promise<SchulconnexResponse[]>;

getPoliciesInfo(accessToken: string, options?: { overrideUrl: string }): Promise<SchulconnexPoliciesInfoResponse[]>;
getPoliciesInfo(accessToken: string, options?: { overrideUrl: string }): Promise<SchulconnexPoliciesInfoResponse>;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { SchulconnexPoliciesInfoLicenseResponse } from '@infra/schulconnex-client/response/policies-info/schulconnex-policies-info-response';
IgorCapCoder marked this conversation as resolved.
Show resolved Hide resolved
import { OauthAdapterService, OAuthTokenDto } from '@modules/oauth';
import { HttpService } from '@nestjs/axios';
import { axiosResponseFactory } from '@shared/testing';
Expand All @@ -8,7 +9,7 @@ import { SchulconnexConfigurationMissingLoggable } from './loggable';
import { SchulconnexPoliciesInfoResponse, SchulconnexResponse } from './response';
import { SchulconnexRestClient } from './schulconnex-rest-client';
import { SchulconnexRestClientOptions } from './schulconnex-rest-client-options';
import { schulconnexPoliciesInfoResponseFactory, schulconnexResponseFactory } from './testing';
import { schulconnexPoliciesInfoLicenseResponseFactory, schulconnexResponseFactory } from './testing';

describe(SchulconnexRestClient.name, () => {
let client: SchulconnexRestClient;
Expand Down Expand Up @@ -199,7 +200,8 @@ describe(SchulconnexRestClient.name, () => {
describe('when requesting policies-info', () => {
const setup = () => {
const accessToken = 'accessToken';
const response: SchulconnexPoliciesInfoResponse[] = schulconnexPoliciesInfoResponseFactory.buildList(1);
const response: SchulconnexPoliciesInfoLicenseResponse[] =
schulconnexPoliciesInfoLicenseResponseFactory.buildList(1);

httpService.get.mockReturnValueOnce(of(axiosResponseFactory.build({ data: response })));

Expand All @@ -224,7 +226,7 @@ describe(SchulconnexRestClient.name, () => {
it('should return the response', async () => {
const { accessToken } = setup();

const result: SchulconnexPoliciesInfoResponse[] = await client.getPoliciesInfo(accessToken);
const result: SchulconnexPoliciesInfoResponse = await client.getPoliciesInfo(accessToken);

expect(result).toBeDefined();
});
Expand All @@ -234,7 +236,8 @@ describe(SchulconnexRestClient.name, () => {
const setup = () => {
const accessToken = 'accessToken';
const customUrl = 'https://override.url/policies-info';
const response: SchulconnexPoliciesInfoResponse[] = schulconnexPoliciesInfoResponseFactory.buildList(1);
const response: SchulconnexPoliciesInfoLicenseResponse[] =
schulconnexPoliciesInfoLicenseResponseFactory.buildList(1);

httpService.get.mockReturnValueOnce(of(axiosResponseFactory.build({ data: response })));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
SchulconnexPoliciesInfoErrorResponse,
SchulconnexPoliciesInfoLicenseResponse,
} from '@infra/schulconnex-client';
import { OauthAdapterService, OAuthTokenDto } from '@modules/oauth';
import { OAuthGrantType } from '@modules/oauth/interface/oauth-grant-type.enum';
import { ClientCredentialsGrantTokenRequest } from '@modules/oauth/service/dto';
Expand Down Expand Up @@ -51,15 +55,17 @@ export class SchulconnexRestClient implements SchulconnexApiInterface {
public async getPoliciesInfo(
accessToken: string,
options?: { overrideUrl: string }
): Promise<SchulconnexPoliciesInfoResponse[]> {
): Promise<SchulconnexPoliciesInfoResponse> {
const url: URL = new URL(options?.overrideUrl ?? `${this.SCHULCONNEX_API_BASE_URL}/policies-info`);

const response: Promise<SchulconnexPoliciesInfoResponse[]> = this.getRequest<SchulconnexPoliciesInfoResponse[]>(
url,
accessToken
);
const response: (SchulconnexPoliciesInfoLicenseResponse | SchulconnexPoliciesInfoErrorResponse)[] =
await this.getRequest<(SchulconnexPoliciesInfoLicenseResponse | SchulconnexPoliciesInfoErrorResponse)[]>(
url,
accessToken
);

return response;
const responseObject: SchulconnexPoliciesInfoResponse = { data: response };
IgorCapCoder marked this conversation as resolved.
Show resolved Hide resolved
return responseObject;
}

private checkOptions(): boolean {
Expand Down
6 changes: 5 additions & 1 deletion apps/server/src/infra/schulconnex-client/testing/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export { schulconnexResponseFactory } from './schulconnex-response-factory';
export { schulconnexPoliciesInfoResponseFactory } from './schulconnex-policies-info-response-factory';
export {
schulconnexPoliciesInfoLicenseResponseFactory,
schulconnexPoliciesInfoErrorResponseFactory,
schulconnexPoliciesInfoResponseFactory,
} from './schulconnex-policies-info-response.factory';

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
SchulconnexPoliciesInfoErrorResponse,
SchulconnexPoliciesInfoLicenseResponse,
} from '@infra/schulconnex-client';
import { Factory } from 'fishery';
import { SchulconnexPoliciesInfoActionType, SchulconnexPoliciesInfoResponse } from '../response';

export const schulconnexPoliciesInfoLicenseResponseFactory = Factory.define<SchulconnexPoliciesInfoLicenseResponse>(
() => {
return {
target: {
uid: 'bildungscloud',
partOf: '',
},
permission: [
{
action: [SchulconnexPoliciesInfoActionType.EXECUTE],
},
],
};
}
);

export const schulconnexPoliciesInfoErrorResponseFactory = Factory.define<SchulconnexPoliciesInfoErrorResponse>(() => {
return {
access_control: {
'@type': 'bilo error mock',
error: {
code: '500',
value: 'something went wrong',
},
},
};
});

export const schulconnexPoliciesInfoResponseFactory = Factory.define<SchulconnexPoliciesInfoResponse>(() => {
return {
data: [schulconnexPoliciesInfoLicenseResponseFactory.build(), schulconnexPoliciesInfoErrorResponseFactory.build()],
};
});
Loading
Loading