File size: 4,630 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";

vi.mock("../request", () => ({
  request: vi.fn(),
}));

import { accessControlApi } from "./accessControl";
import { request } from "../request";

describe("accessControlApi", () => {
  beforeEach(() => {
    vi.mocked(request).mockResolvedValue(undefined);
  });

  afterEach(() => {
    vi.clearAllMocks();
  });

  it("getAclAll calls GET /access-control", async () => {
    const data = { chan: { whitelist: {}, blacklist: {}, pending: [] } };
    vi.mocked(request).mockResolvedValue(data);
    const result = await accessControlApi.getAclAll();
    expect(request).toHaveBeenCalledWith("/access-control");
    expect(result).toEqual(data);
  });

  it("getAclChannel calls GET /access-control/:channel", async () => {
    await accessControlApi.getAclChannel("dingtalk");
    expect(request).toHaveBeenCalledWith("/access-control/dingtalk");
  });

  it("addAclWhitelist sends POST with entries body", async () => {
    const entries = [{ channel: "chan", user_id: "u1", remark: "r" }];
    await accessControlApi.addAclWhitelist(entries);
    expect(request).toHaveBeenCalledWith("/access-control/whitelist/add", {
      method: "POST",
      body: JSON.stringify({ entries }),
    });
  });

  it("removeAclWhitelist sends POST with entries body", async () => {
    const entries = [{ channel: "chan", user_id: "u1" }];
    await accessControlApi.removeAclWhitelist(entries);
    expect(request).toHaveBeenCalledWith("/access-control/whitelist/remove", {
      method: "POST",
      body: JSON.stringify({ entries }),
    });
  });

  it("addAclBlacklist sends POST with entries body", async () => {
    const entries = [{ channel: "chan", user_id: "u1" }];
    await accessControlApi.addAclBlacklist(entries);
    expect(request).toHaveBeenCalledWith("/access-control/blacklist/add", {
      method: "POST",
      body: JSON.stringify({ entries }),
    });
  });

  it("removeAclBlacklist sends POST with entries body", async () => {
    const entries = [{ channel: "chan", user_id: "u1" }];
    await accessControlApi.removeAclBlacklist(entries);
    expect(request).toHaveBeenCalledWith("/access-control/blacklist/remove", {
      method: "POST",
      body: JSON.stringify({ entries }),
    });
  });

  it("updateAclRemark sends POST with channel, user_id, remark", async () => {
    await accessControlApi.updateAclRemark("chan", "u1", "team lead");
    expect(request).toHaveBeenCalledWith("/access-control/remark", {
      method: "POST",
      body: JSON.stringify({
        channel: "chan",
        user_id: "u1",
        remark: "team lead",
      }),
    });
  });

  it("getAclAllPending calls GET /access-control/pending/all", async () => {
    await accessControlApi.getAclAllPending();
    expect(request).toHaveBeenCalledWith("/access-control/pending/all");
  });

  it("approveAclPending sends POST with entries body", async () => {
    const entries = [{ channel: "chan", user_id: "u1" }];
    await accessControlApi.approveAclPending(entries);
    expect(request).toHaveBeenCalledWith("/access-control/pending/approve", {
      method: "POST",
      body: JSON.stringify({ entries }),
    });
  });

  it("denyAclPending sends POST with entries body", async () => {
    const entries = [{ channel: "chan", user_id: "u1" }];
    await accessControlApi.denyAclPending(entries);
    expect(request).toHaveBeenCalledWith("/access-control/pending/deny", {
      method: "POST",
      body: JSON.stringify({ entries }),
    });
  });

  it("dismissAclPending sends POST with entries body", async () => {
    const entries = [{ channel: "chan", user_id: "u1" }];
    await accessControlApi.dismissAclPending(entries);
    expect(request).toHaveBeenCalledWith("/access-control/pending/dismiss", {
      method: "POST",
      body: JSON.stringify({ entries }),
    });
  });

  it("updatePendingRemark sends POST with channel, user_id, remark", async () => {
    await accessControlApi.updatePendingRemark("chan", "u1", "reviewing");
    expect(request).toHaveBeenCalledWith("/access-control/pending/remark", {
      method: "POST",
      body: JSON.stringify({
        channel: "chan",
        user_id: "u1",
        remark: "reviewing",
      }),
    });
  });

  it("updateUsername sends POST with channel, user_id, username", async () => {
    await accessControlApi.updateUsername("chan", "u1", "alice");
    expect(request).toHaveBeenCalledWith("/access-control/username", {
      method: "POST",
      body: JSON.stringify({
        channel: "chan",
        user_id: "u1",
        username: "alice",
      }),
    });
  });
});