|
| 1 | +import { |
| 2 | + Inputs, |
| 3 | + Command, |
| 4 | + SlashCommandPayload, |
| 5 | + commandDefaults, |
| 6 | + getCommandsConfigFromInputs, |
| 7 | + getCommandsConfigFromJson, |
| 8 | + actorHasPermission, |
| 9 | + configIsValid, |
| 10 | + getSlashCommandPayload |
| 11 | +} from '../lib/command-helper' |
| 12 | + |
| 13 | +describe('command-helper tests', () => { |
| 14 | + test('building config with required inputs only', async () => { |
| 15 | + const inputs: Inputs = { |
| 16 | + token: '', |
| 17 | + reactionToken: '', |
| 18 | + reactions: true, |
| 19 | + commands: 'list, of, slash, commands', |
| 20 | + permission: 'write', |
| 21 | + issueType: 'both', |
| 22 | + allowEdits: false, |
| 23 | + // Should be process.env.GITHUB_REPOSITORY, but '' for tests |
| 24 | + repository: '', |
| 25 | + eventTypeSuffix: '-command', |
| 26 | + namedArgs: false, |
| 27 | + config: '', |
| 28 | + configFromFile: '' |
| 29 | + } |
| 30 | + const commands = inputs.commands.replace(/\s+/g, '').split(',') |
| 31 | + const config = getCommandsConfigFromInputs(inputs) |
| 32 | + expect(config.length).toEqual(4) |
| 33 | + for (var i = 0; i < config.length; i++) { |
| 34 | + expect(config[i].command).toEqual(commands[i]) |
| 35 | + expect(config[i].permission).toEqual(commandDefaults.permission) |
| 36 | + expect(config[i].issue_type).toEqual(commandDefaults.issue_type) |
| 37 | + expect(config[i].allow_edits).toEqual(commandDefaults.allow_edits) |
| 38 | + expect(config[i].repository).toEqual(commandDefaults.repository) |
| 39 | + expect(config[i].event_type_suffix).toEqual( |
| 40 | + commandDefaults.event_type_suffix |
| 41 | + ) |
| 42 | + expect(config[i].named_args).toEqual(commandDefaults.named_args) |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + test('building config with optional inputs', async () => { |
| 47 | + const inputs: Inputs = { |
| 48 | + token: '', |
| 49 | + reactionToken: '', |
| 50 | + reactions: true, |
| 51 | + commands: 'list, of, slash, commands', |
| 52 | + permission: 'admin', |
| 53 | + issueType: 'pull-request', |
| 54 | + allowEdits: true, |
| 55 | + repository: 'owner/repo', |
| 56 | + eventTypeSuffix: '-cmd', |
| 57 | + namedArgs: true, |
| 58 | + config: '', |
| 59 | + configFromFile: '' |
| 60 | + } |
| 61 | + const commands = inputs.commands.replace(/\s+/g, '').split(',') |
| 62 | + const config = getCommandsConfigFromInputs(inputs) |
| 63 | + expect(config.length).toEqual(4) |
| 64 | + for (var i = 0; i < config.length; i++) { |
| 65 | + expect(config[i].command).toEqual(commands[i]) |
| 66 | + expect(config[i].permission).toEqual(inputs.permission) |
| 67 | + expect(config[i].issue_type).toEqual(inputs.issueType) |
| 68 | + expect(config[i].allow_edits).toEqual(inputs.allowEdits) |
| 69 | + expect(config[i].repository).toEqual(inputs.repository) |
| 70 | + expect(config[i].event_type_suffix).toEqual(inputs.eventTypeSuffix) |
| 71 | + expect(config[i].named_args).toEqual(inputs.namedArgs) |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + test('building config with required JSON only', async () => { |
| 76 | + const json = `[ |
| 77 | + { |
| 78 | + "command": "do-stuff" |
| 79 | + }, |
| 80 | + { |
| 81 | + "command": "test-all-the-things" |
| 82 | + } |
| 83 | + ]` |
| 84 | + const commands = ['do-stuff', 'test-all-the-things'] |
| 85 | + const config = getCommandsConfigFromJson(json) |
| 86 | + expect(config.length).toEqual(2) |
| 87 | + for (var i = 0; i < config.length; i++) { |
| 88 | + expect(config[i].command).toEqual(commands[i]) |
| 89 | + expect(config[i].permission).toEqual(commandDefaults.permission) |
| 90 | + expect(config[i].issue_type).toEqual(commandDefaults.issue_type) |
| 91 | + expect(config[i].allow_edits).toEqual(commandDefaults.allow_edits) |
| 92 | + expect(config[i].repository).toEqual(commandDefaults.repository) |
| 93 | + expect(config[i].event_type_suffix).toEqual( |
| 94 | + commandDefaults.event_type_suffix |
| 95 | + ) |
| 96 | + expect(config[i].named_args).toEqual(commandDefaults.named_args) |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + test('building config with optional JSON properties', async () => { |
| 101 | + const json = `[ |
| 102 | + { |
| 103 | + "command": "do-stuff", |
| 104 | + "permission": "admin", |
| 105 | + "issue_type": "pull-request", |
| 106 | + "allow_edits": true, |
| 107 | + "repository": "owner/repo", |
| 108 | + "event_type_suffix": "-cmd", |
| 109 | + "named_args": true |
| 110 | + }, |
| 111 | + { |
| 112 | + "command": "test-all-the-things", |
| 113 | + "permission": "read" |
| 114 | + } |
| 115 | + ]` |
| 116 | + const commands = ['do-stuff', 'test-all-the-things'] |
| 117 | + const config = getCommandsConfigFromJson(json) |
| 118 | + expect(config.length).toEqual(2) |
| 119 | + expect(config[0].command).toEqual(commands[0]) |
| 120 | + expect(config[0].permission).toEqual('admin') |
| 121 | + expect(config[0].issue_type).toEqual('pull-request') |
| 122 | + expect(config[0].allow_edits).toBeTruthy() |
| 123 | + expect(config[0].repository).toEqual('owner/repo') |
| 124 | + expect(config[0].event_type_suffix).toEqual('-cmd') |
| 125 | + expect(config[0].named_args).toBeTruthy() |
| 126 | + expect(config[1].command).toEqual(commands[1]) |
| 127 | + expect(config[1].permission).toEqual('read') |
| 128 | + expect(config[1].issue_type).toEqual(commandDefaults.issue_type) |
| 129 | + }) |
| 130 | + |
| 131 | + test('valid config', async () => { |
| 132 | + const config: Command[] = [ |
| 133 | + { |
| 134 | + command: 'test', |
| 135 | + permission: 'write', |
| 136 | + issue_type: 'both', |
| 137 | + allow_edits: false, |
| 138 | + repository: 'peter-evans/slash-command-dispatch', |
| 139 | + event_type_suffix: '-command', |
| 140 | + named_args: false |
| 141 | + } |
| 142 | + ] |
| 143 | + expect(configIsValid(config)).toBeTruthy() |
| 144 | + }) |
| 145 | + |
| 146 | + test('invalid permission level in config', async () => { |
| 147 | + const config: Command[] = [ |
| 148 | + { |
| 149 | + command: 'test', |
| 150 | + permission: 'test-case-invalid-permission', |
| 151 | + issue_type: 'both', |
| 152 | + allow_edits: false, |
| 153 | + repository: 'peter-evans/slash-command-dispatch', |
| 154 | + event_type_suffix: '-command', |
| 155 | + named_args: false |
| 156 | + } |
| 157 | + ] |
| 158 | + expect(configIsValid(config)).toBeFalsy() |
| 159 | + }) |
| 160 | + |
| 161 | + test('invalid issue type in config', async () => { |
| 162 | + const config: Command[] = [ |
| 163 | + { |
| 164 | + command: 'test', |
| 165 | + permission: 'write', |
| 166 | + issue_type: 'test-case-invalid-issue-type', |
| 167 | + allow_edits: false, |
| 168 | + repository: 'peter-evans/slash-command-dispatch', |
| 169 | + event_type_suffix: '-command', |
| 170 | + named_args: false |
| 171 | + } |
| 172 | + ] |
| 173 | + expect(configIsValid(config)).toBeFalsy() |
| 174 | + }) |
| 175 | + |
| 176 | + test('actor does not have permission', async () => { |
| 177 | + expect(actorHasPermission('none', 'read')).toBeFalsy() |
| 178 | + expect(actorHasPermission('read', 'write')).toBeFalsy() |
| 179 | + expect(actorHasPermission('write', 'admin')).toBeFalsy() |
| 180 | + }) |
| 181 | + |
| 182 | + test('actor has permission', async () => { |
| 183 | + expect(actorHasPermission('read', 'none')).toBeTruthy() |
| 184 | + expect(actorHasPermission('write', 'read')).toBeTruthy() |
| 185 | + expect(actorHasPermission('admin', 'write')).toBeTruthy() |
| 186 | + expect(actorHasPermission('write', 'write')).toBeTruthy() |
| 187 | + }) |
| 188 | + |
| 189 | + test('slash command payload', async () => { |
| 190 | + const commandWords = ['test', 'arg1', 'arg2', 'arg3'] |
| 191 | + const namedArgs = false |
| 192 | + const payload: SlashCommandPayload = { |
| 193 | + command: 'test', |
| 194 | + args: 'arg1 arg2 arg3', |
| 195 | + arg1: 'arg1', |
| 196 | + arg2: 'arg2', |
| 197 | + arg3: 'arg3' |
| 198 | + } |
| 199 | + expect(getSlashCommandPayload(commandWords, namedArgs)).toEqual(payload) |
| 200 | + }) |
| 201 | + |
| 202 | + test('slash command payload with named args', async () => { |
| 203 | + const commandWords = ['test', 'branch=master', 'arg1', 'env=prod', 'arg2'] |
| 204 | + const namedArgs = true |
| 205 | + const payload: SlashCommandPayload = { |
| 206 | + command: 'test', |
| 207 | + args: 'branch=master arg1 env=prod arg2', |
| 208 | + unnamed_args: 'arg1 arg2', |
| 209 | + branch: 'master', |
| 210 | + env: 'prod', |
| 211 | + arg1: 'arg1', |
| 212 | + arg2: 'arg2' |
| 213 | + } |
| 214 | + expect(getSlashCommandPayload(commandWords, namedArgs)).toEqual(payload) |
| 215 | + }) |
| 216 | + |
| 217 | + test('slash command payload with malformed named args', async () => { |
| 218 | + const commandWords = ['test', 'branch=', 'arg1', 'e-nv=prod', 'arg2'] |
| 219 | + const namedArgs = true |
| 220 | + const payload: SlashCommandPayload = { |
| 221 | + command: 'test', |
| 222 | + args: 'branch= arg1 e-nv=prod arg2', |
| 223 | + unnamed_args: 'branch= arg1 e-nv=prod arg2', |
| 224 | + arg1: 'branch=', |
| 225 | + arg2: 'arg1', |
| 226 | + arg3: 'e-nv=prod', |
| 227 | + arg4: 'arg2' |
| 228 | + } |
| 229 | + expect(getSlashCommandPayload(commandWords, namedArgs)).toEqual(payload) |
| 230 | + }) |
| 231 | +}) |
0 commit comments