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