| 14 | ) |
| 15 | |
| 16 | func TestAutolinkCreator_Create(t *testing.T) { |
| 17 | repo := ghrepo.New("OWNER", "REPO") |
| 18 | |
| 19 | tests := []struct { |
| 20 | name string |
| 21 | req AutolinkCreateRequest |
| 22 | stubStatus int |
| 23 | stubRespJSON string |
| 24 | |
| 25 | expectedAutolink *shared.Autolink |
| 26 | expectErr bool |
| 27 | expectedErrMsg string |
| 28 | }{ |
| 29 | { |
| 30 | name: "201 successful creation", |
| 31 | req: AutolinkCreateRequest{ |
| 32 | IsAlphanumeric: true, |
| 33 | KeyPrefix: "TICKET-", |
| 34 | URLTemplate: "https://example.com/TICKET?query=<num>", |
| 35 | }, |
| 36 | stubStatus: http.StatusCreated, |
| 37 | stubRespJSON: `{ |
| 38 | "id": 1, |
| 39 | "is_alphanumeric": true, |
| 40 | "key_prefix": "TICKET-", |
| 41 | "url_template": "https://example.com/TICKET?query=<num>" |
| 42 | }`, |
| 43 | expectedAutolink: &shared.Autolink{ |
| 44 | ID: 1, |
| 45 | KeyPrefix: "TICKET-", |
| 46 | URLTemplate: "https://example.com/TICKET?query=<num>", |
| 47 | IsAlphanumeric: true, |
| 48 | }, |
| 49 | }, |
| 50 | { |
| 51 | name: "422 URL template not valid URL", |
| 52 | req: AutolinkCreateRequest{ |
| 53 | IsAlphanumeric: true, |
| 54 | KeyPrefix: "TICKET-", |
| 55 | URLTemplate: "foo/<num>", |
| 56 | }, |
| 57 | stubStatus: http.StatusUnprocessableEntity, |
| 58 | stubRespJSON: `{ |
| 59 | "message": "Validation Failed", |
| 60 | "errors": [ |
| 61 | { |
| 62 | "resource": "KeyLink", |
| 63 | "code": "custom", |
| 64 | "field": "url_template", |
| 65 | "message": "url_template must be an absolute URL" |
| 66 | } |
| 67 | ], |
| 68 | "documentation_url": "https://docs.github.com/rest/repos/autolinks#create-an-autolink-reference-for-a-repository", |
| 69 | "status": "422" |
| 70 | }`, |
| 71 | expectErr: true, |
| 72 | expectedErrMsg: heredoc.Doc(` |
| 73 | HTTP 422: Validation Failed (https://api.github.com/repos/OWNER/REPO/autolinks) |