Merge branch '19421-restore-old-redirect-key' into main. Closes #19421
[arvados.git] / src / services / favorite-service / favorite-service.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { LinkService } from "../link-service/link-service";
6 import { GroupsService } from "../groups-service/groups-service";
7 import { FavoriteService } from "./favorite-service";
8 import { LinkClass } from "models/link";
9 import { mockResourceService } from "services/common-service/common-resource-service.test";
10 import { FilterBuilder } from "services/api/filter-builder";
11
12 describe("FavoriteService", () => {
13
14     let linkService: LinkService;
15     let groupService: GroupsService;
16
17     beforeEach(() => {
18         linkService = mockResourceService(LinkService);
19         groupService = mockResourceService(GroupsService);
20     });
21
22     it("marks resource as favorite", async () => {
23         linkService.create = jest.fn().mockReturnValue(Promise.resolve({ uuid: "newUuid" }));
24         const favoriteService = new FavoriteService(linkService, groupService);
25
26         const newFavorite = await favoriteService.create({ userUuid: "userUuid", resource: { uuid: "resourceUuid", name: "resource" } });
27
28         expect(linkService.create).toHaveBeenCalledWith({
29             ownerUuid: "userUuid",
30             tailUuid: "userUuid",
31             headUuid: "resourceUuid",
32             linkClass: LinkClass.STAR,
33             name: "resource"
34         });
35         expect(newFavorite.uuid).toEqual("newUuid");
36
37     });
38
39     it("unmarks resource as favorite", async () => {
40         const list = jest.fn().mockReturnValue(Promise.resolve({ items: [{ uuid: "linkUuid" }] }));
41         const filters = new FilterBuilder()
42             .addEqual('owner_uuid', "userUuid")
43             .addEqual('head_uuid', "resourceUuid")
44             .addEqual('link_class', LinkClass.STAR);
45         linkService.list = list;
46         linkService.delete = jest.fn().mockReturnValue(Promise.resolve({ uuid: "linkUuid" }));
47         const favoriteService = new FavoriteService(linkService, groupService);
48
49         const newFavorite = await favoriteService.delete({ userUuid: "userUuid", resourceUuid: "resourceUuid" });
50
51         expect(list.mock.calls[0][0].filters).toEqual(filters.getFilters());
52         expect(linkService.delete).toHaveBeenCalledWith("linkUuid");
53         expect(newFavorite[0].uuid).toEqual("linkUuid");
54     });
55
56     it("lists favorite resources", async () => {
57         const list = jest.fn().mockReturnValue(Promise.resolve({ items: [{ headUuid: "headUuid" }] }));
58         const listFilters = new FilterBuilder()
59             .addEqual('owner_uuid', "userUuid")
60             .addEqual('link_class', LinkClass.STAR);
61         const contents = jest.fn().mockReturnValue(Promise.resolve({ items: [{ uuid: "resourceUuid" }] }));
62         const contentFilters = new FilterBuilder().addIn('uuid', ["headUuid"]);
63         linkService.list = list;
64         groupService.contents = contents;
65         const favoriteService = new FavoriteService(linkService, groupService);
66
67         const favorites = await favoriteService.list("userUuid");
68
69         expect(list.mock.calls[0][0].filters).toEqual(listFilters.getFilters());
70         expect(contents.mock.calls[0][0]).toEqual("userUuid");
71         expect(contents.mock.calls[0][1].filters).toEqual(contentFilters.getFilters());
72         expect(favorites).toEqual({ items: [{ uuid: "resourceUuid" }] });
73     });
74
75     it("checks if resources are present in favorites", async () => {
76         const list = jest.fn().mockReturnValue(Promise.resolve({ items: [{ headUuid: "foo" }] }));
77         const listFilters = new FilterBuilder()
78             .addIn("head_uuid", ["foo", "oof"])
79             .addEqual("owner_uuid", "userUuid")
80             .addEqual("link_class", LinkClass.STAR);
81         linkService.list = list;
82         const favoriteService = new FavoriteService(linkService, groupService);
83
84         const favorites = await favoriteService.checkPresenceInFavorites("userUuid", ["foo", "oof"]);
85
86         expect(list.mock.calls[0][0].filters).toEqual(listFilters.getFilters());
87         expect(favorites).toEqual({ foo: true, oof: false });
88     });
89
90 });