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-1324-migration-page-buttons-fix #2834

Merged
merged 13 commits into from
Oct 13, 2023
Merged
4 changes: 2 additions & 2 deletions src/components/administration/AdminMigrationSection.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ describe("AdminMigrationSection", () => {
userLoginMigrationModule.startUserLoginMigration
).toHaveBeenCalled();
expect(
userLoginMigrationModule.fetchLatestUserLoginMigrationForCurrentUser
userLoginMigrationModule.fetchLatestUserLoginMigrationForSchool
).toHaveBeenCalled();
});
});
Expand Down Expand Up @@ -676,7 +676,7 @@ describe("AdminMigrationSection", () => {
userLoginMigrationModule.closeUserLoginMigration
).toHaveBeenCalled();
expect(
userLoginMigrationModule.fetchLatestUserLoginMigrationForCurrentUser
userLoginMigrationModule.fetchLatestUserLoginMigrationForSchool
).toHaveBeenCalled();
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/components/administration/AdminMigrationSection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default defineComponent({
onMounted(async () => {
// TODO remove in https://ticketsystem.dbildungscloud.de/browse/N21-820
await schoolsModule.fetchSchoolOAuthMigration();
await userLoginMigrationModule.fetchLatestUserLoginMigrationForCurrentUser();
await userLoginMigrationModule.fetchLatestUserLoginMigrationForSchool();
});

const userLoginMigration: ComputedRef<UserLoginMigration | undefined> =
Expand Down
44 changes: 43 additions & 1 deletion src/store/user-login-migrations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { $axios, mapAxiosErrorToResponseError } from "@/utils/api";
import { AxiosError, AxiosResponse } from "axios";
import { AxiosResponse, HttpStatusCode } from "axios";
import { Action, Module, Mutation, VuexModule } from "vuex-module-decorators";
import {
PageContentResponse,
Expand Down Expand Up @@ -173,6 +173,48 @@ export default class UserLoginMigrationModule extends VuexModule {
this.setLoading(false);
}

@Action
async fetchLatestUserLoginMigrationForSchool(): Promise<void> {
this.setLoading(true);

this.resetBusinessError();

if (authModule.getUser?.schoolId) {
try {
const response: AxiosResponse<UserLoginMigrationResponse> =
await this.userLoginMigrationApi.userLoginMigrationControllerFindUserLoginMigrationBySchool(
authModule.getUser?.schoolId
);

if (response.data.startedAt) {
const userLoginMigration: UserLoginMigration =
UserLoginMigrationMapper.mapToUserLoginMigration(response.data);

this.setUserLoginMigration(userLoginMigration);
}
} catch (error: unknown) {
const apiError = mapAxiosErrorToResponseError(error);

if (apiError.code === HttpStatusCode.NotFound) {
this.setUserLoginMigration(undefined);
return;
}

console.log(apiError);

this.setBusinessError({
error: apiError,
statusCode: apiError.code,
message: apiError.message,
});

throw createApplicationError(apiError.code);
}
}

this.setLoading(false);
}

@Action
async startUserLoginMigration(): Promise<void> {
this.setLoading(true);
Expand Down
164 changes: 162 additions & 2 deletions src/store/user-login-migrations.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "./user-login-migration";
import UserLoginMigrationModule from "./user-login-migrations";
import {
apiResponseErrorFactory,
axiosErrorFactory,
businessErrorFactory,
mockApiResponse,
Expand Down Expand Up @@ -248,7 +249,7 @@ describe("UserLoginMigrationModule", () => {
authModule.setUser({ ...mockUser, id: "" });
};

it("should not call the userMigrationApi.userMigrationControllerGetLatestUserLoginMigrationForCurrentUser", async () => {
it("should not get latest user login migration ", async () => {
mrikallab marked this conversation as resolved.
Show resolved Hide resolved
setup();

await module.fetchLatestUserLoginMigrationForCurrentUser();
Expand Down Expand Up @@ -370,7 +371,7 @@ describe("UserLoginMigrationModule", () => {
};
};

it("should call the userMigrationApi.userMigrationControllerGetLatestUserLoginMigrationForCurrentUser", async () => {
it("should get latest user login migrations", async () => {
setup();

await module.fetchLatestUserLoginMigrationForCurrentUser();
Expand Down Expand Up @@ -454,6 +455,165 @@ describe("UserLoginMigrationModule", () => {
});
});

describe("getLatestUserLoginMigrationForSchool", () => {
describe("when school id is not available", () => {
const setup = () => {
authModule.setUser({ ...mockUser, schoolId: "" });
};

it("should not get latest user login migrations", async () => {
setup();

await module.fetchLatestUserLoginMigrationForSchool();

expect(
apiMock.userLoginMigrationControllerFindUserLoginMigrationBySchool
).not.toHaveBeenCalled();
});
});

describe("when school is available", () => {
describe("when there is no migration for a school", () => {
const setup = () => {
authModule.setUser({ ...mockUser, schoolId: "schoolId" });

const error = axiosErrorFactory.build({
response: {
data: apiResponseErrorFactory.build({
code: HttpStatusCode.NotFound,
}),
},
});

apiMock.userLoginMigrationControllerFindUserLoginMigrationBySchool.mockRejectedValue(
error
);
};

it("should set the user login migration to undefined", async () => {
setup();

await module.fetchLatestUserLoginMigrationForSchool();

expect(module.getUserLoginMigration).toBeUndefined();
});
});

describe("when there is a migration for the school", () => {
const setup = () => {
authModule.setUser({ ...mockUser, schoolId: "schoolId" });

const userLoginMigrationResponse: UserLoginMigrationResponse =
userLoginMigrationResponseFactory.build({
sourceSystemId: "sourceSystemId",
targetSystemId: "targetSystemId",
startedAt: new Date(2000, 1, 1, 0, 0).toString(),
closedAt: new Date(2000, 1, 1, 0, 0).toString(),
finishedAt: new Date(2000, 1, 14, 0, 0).toString(),
mandatorySince: new Date(2000, 1, 1, 0, 0).toString(),
});

const userLoginMigration: UserLoginMigration = {
sourceSystemId: "sourceSystemId",
targetSystemId: "targetSystemId",
startedAt: new Date(2000, 1, 1, 0, 0),
closedAt: new Date(2000, 1, 1, 0, 0),
finishedAt: new Date(2000, 1, 14, 0, 0),
mandatorySince: new Date(2000, 1, 1, 0, 0),
};

apiMock.userLoginMigrationControllerFindUserLoginMigrationBySchool.mockResolvedValue(
mockApiResponse({ data: userLoginMigrationResponse })
);

return {
userLoginMigration,
userLoginMigrationResponse,
};
};

it("should get latest user login migrations", async () => {
setup();

await module.fetchLatestUserLoginMigrationForSchool();

expect(
apiMock.userLoginMigrationControllerFindUserLoginMigrationBySchool
).toHaveBeenCalled();
});

it("should set the UserLoginMigration", async () => {
const { userLoginMigration } = setup();

await module.fetchLatestUserLoginMigrationForSchool();

expect(module.getUserLoginMigration).toEqual(userLoginMigration);
});
});
});

describe("when the api throws an error", () => {
const setup = () => {
authModule.setUser({ ...mockUser, schoolId: "schoolId" });

const error = axiosErrorFactory.build();
const apiError = mapAxiosErrorToResponseError(error);

apiMock.userLoginMigrationControllerFindUserLoginMigrationBySchool.mockRejectedValue(
error
);

return {
apiError,
};
};

it("should set the businessError", async () => {
const { apiError } = setup();

await expect(
module.fetchLatestUserLoginMigrationForSchool()
).rejects.toThrow();

expect(module.getBusinessError).toEqual<BusinessError>({
error: apiError,
statusCode: apiError.code,
message: apiError.message,
});
});

it("should throw application error", async () => {
setup();

const func = () => module.fetchLatestUserLoginMigrationForSchool();

await expect(func()).rejects.toEqual(
createApplicationError(HttpStatusCode.BadRequest)
);
});
});

describe("when the api returns a bad request", () => {
const setup = () => {
authModule.setUser({ ...mockUser, schoolId: "schoolId" });

apiMock.userLoginMigrationControllerFindUserLoginMigrationBySchool.mockRejectedValue(
createApplicationError(HttpStatusCode.BadRequest)
);
};

it("should throw an error with status code BadRequest when an ApplicationError is thrown", async () => {
setup();

const func = () => module.fetchLatestUserLoginMigrationForSchool();

await expect(func()).rejects.toEqual(
createApplicationError(HttpStatusCode.BadRequest)
);
});
});
});

describe("startUserLoginMigration", () => {
describe("when it successfully calls the api", () => {
const setup = () => {
Expand Down