|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +import { describe, test, expect } from "bun:test"; |
| 4 | +import { checkHumanActor } from "../src/github/validation/actor"; |
| 5 | +import type { Octokit } from "@octokit/rest"; |
| 6 | +import { createMockContext } from "./mockContext"; |
| 7 | + |
| 8 | +function createMockOctokit(userType: string): Octokit { |
| 9 | + return { |
| 10 | + users: { |
| 11 | + getByUsername: async () => ({ |
| 12 | + data: { |
| 13 | + type: userType, |
| 14 | + }, |
| 15 | + }), |
| 16 | + }, |
| 17 | + } as unknown as Octokit; |
| 18 | +} |
| 19 | + |
| 20 | +describe("checkHumanActor", () => { |
| 21 | + test("should pass for human actor", async () => { |
| 22 | + const mockOctokit = createMockOctokit("User"); |
| 23 | + const context = createMockContext(); |
| 24 | + context.actor = "human-user"; |
| 25 | + |
| 26 | + await expect( |
| 27 | + checkHumanActor(mockOctokit, context), |
| 28 | + ).resolves.toBeUndefined(); |
| 29 | + }); |
| 30 | + |
| 31 | + test("should throw error for bot actor when not allowed", async () => { |
| 32 | + const mockOctokit = createMockOctokit("Bot"); |
| 33 | + const context = createMockContext(); |
| 34 | + context.actor = "test-bot[bot]"; |
| 35 | + context.inputs.allowedBots = ""; |
| 36 | + |
| 37 | + await expect(checkHumanActor(mockOctokit, context)).rejects.toThrow( |
| 38 | + "Workflow initiated by non-human actor: test-bot (type: Bot). Add bot to allowed_bots list or use '*' to allow all bots.", |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + test("should pass for bot actor when all bots allowed", async () => { |
| 43 | + const mockOctokit = createMockOctokit("Bot"); |
| 44 | + const context = createMockContext(); |
| 45 | + context.actor = "test-bot[bot]"; |
| 46 | + context.inputs.allowedBots = "*"; |
| 47 | + |
| 48 | + await expect( |
| 49 | + checkHumanActor(mockOctokit, context), |
| 50 | + ).resolves.toBeUndefined(); |
| 51 | + }); |
| 52 | + |
| 53 | + test("should pass for specific bot when in allowed list", async () => { |
| 54 | + const mockOctokit = createMockOctokit("Bot"); |
| 55 | + const context = createMockContext(); |
| 56 | + context.actor = "dependabot[bot]"; |
| 57 | + context.inputs.allowedBots = "dependabot[bot],renovate[bot]"; |
| 58 | + |
| 59 | + await expect( |
| 60 | + checkHumanActor(mockOctokit, context), |
| 61 | + ).resolves.toBeUndefined(); |
| 62 | + }); |
| 63 | + |
| 64 | + test("should pass for specific bot when in allowed list (without [bot])", async () => { |
| 65 | + const mockOctokit = createMockOctokit("Bot"); |
| 66 | + const context = createMockContext(); |
| 67 | + context.actor = "dependabot[bot]"; |
| 68 | + context.inputs.allowedBots = "dependabot,renovate"; |
| 69 | + |
| 70 | + await expect( |
| 71 | + checkHumanActor(mockOctokit, context), |
| 72 | + ).resolves.toBeUndefined(); |
| 73 | + }); |
| 74 | + |
| 75 | + test("should throw error for bot not in allowed list", async () => { |
| 76 | + const mockOctokit = createMockOctokit("Bot"); |
| 77 | + const context = createMockContext(); |
| 78 | + context.actor = "other-bot[bot]"; |
| 79 | + context.inputs.allowedBots = "dependabot[bot],renovate[bot]"; |
| 80 | + |
| 81 | + await expect(checkHumanActor(mockOctokit, context)).rejects.toThrow( |
| 82 | + "Workflow initiated by non-human actor: other-bot (type: Bot). Add bot to allowed_bots list or use '*' to allow all bots.", |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + test("should throw error for bot not in allowed list (without [bot])", async () => { |
| 87 | + const mockOctokit = createMockOctokit("Bot"); |
| 88 | + const context = createMockContext(); |
| 89 | + context.actor = "other-bot[bot]"; |
| 90 | + context.inputs.allowedBots = "dependabot,renovate"; |
| 91 | + |
| 92 | + await expect(checkHumanActor(mockOctokit, context)).rejects.toThrow( |
| 93 | + "Workflow initiated by non-human actor: other-bot (type: Bot). Add bot to allowed_bots list or use '*' to allow all bots.", |
| 94 | + ); |
| 95 | + }); |
| 96 | +}); |
0 commit comments