|
| 1 | +package resource_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + resource "github.com/telia-oss/github-pr-resource" |
| 9 | +) |
| 10 | + |
| 11 | +func TestSource(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + description string |
| 14 | + source resource.Source |
| 15 | + wantErr string |
| 16 | + }{ |
| 17 | + { |
| 18 | + description: "validate passes", |
| 19 | + source: resource.Source{ |
| 20 | + AccessToken: "123456", |
| 21 | + Repository: "test/test", |
| 22 | + }, |
| 23 | + }, |
| 24 | + { |
| 25 | + description: "should have an access_token", |
| 26 | + source: resource.Source{ |
| 27 | + Repository: "test/test", |
| 28 | + }, |
| 29 | + wantErr: "access_token must be set if not using GitHub App authentication", |
| 30 | + }, |
| 31 | + { |
| 32 | + description: "should have a repository", |
| 33 | + source: resource.Source{ |
| 34 | + AccessToken: "123456", |
| 35 | + }, |
| 36 | + wantErr: "repository must be set", |
| 37 | + }, |
| 38 | + { |
| 39 | + description: "should support GitHub App authentication", |
| 40 | + source: resource.Source{ |
| 41 | + Repository: "test/test", |
| 42 | + UseGitHubApp: true, |
| 43 | + PrivateKey: "key.pem", |
| 44 | + ApplicationID: 123456, |
| 45 | + InstallationID: 1, |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + description: "requires a private_key or private_key_file GitHub App configuration values", |
| 50 | + source: resource.Source{ |
| 51 | + Repository: "test/test", |
| 52 | + UseGitHubApp: true, |
| 53 | + ApplicationID: 123456, |
| 54 | + InstallationID: 1, |
| 55 | + }, |
| 56 | + wantErr: "Either private_key or private_key_file should be supplied if using GitHub App authentication", |
| 57 | + }, |
| 58 | + { |
| 59 | + description: "requires an application_id and installation_id GitHub App configuration values", |
| 60 | + source: resource.Source{ |
| 61 | + Repository: "test/test", |
| 62 | + UseGitHubApp: true, |
| 63 | + PrivateKey: "key.pem", |
| 64 | + ApplicationID: 123456, |
| 65 | + }, |
| 66 | + wantErr: "application_id and installation_id must be set if using GitHub App authentication", |
| 67 | + }, |
| 68 | + { |
| 69 | + description: "should not have an access_token when using GitHub App authentication", |
| 70 | + source: resource.Source{ |
| 71 | + Repository: "test/test", |
| 72 | + UseGitHubApp: true, |
| 73 | + PrivateKey: "key.pem", |
| 74 | + ApplicationID: 123456, |
| 75 | + InstallationID: 1, |
| 76 | + AccessToken: "123456", |
| 77 | + }, |
| 78 | + wantErr: "access_token is not required when using GitHub App authentication", |
| 79 | + }, |
| 80 | + { |
| 81 | + description: "requires v3_endpoint when v4_endpoint is set", |
| 82 | + source: resource.Source{ |
| 83 | + AccessToken: "123456", |
| 84 | + Repository: "test/test", |
| 85 | + V3Endpoint: "https://github.com/v3", |
| 86 | + }, |
| 87 | + wantErr: "v4_endpoint must be set together with v3_endpoint", |
| 88 | + }, |
| 89 | + { |
| 90 | + description: "requires v4_endpoint when v3_endpoint is set", |
| 91 | + source: resource.Source{ |
| 92 | + AccessToken: "123456", |
| 93 | + Repository: "test/test", |
| 94 | + V4Endpoint: "https://github.com/v4", |
| 95 | + }, |
| 96 | + wantErr: "v3_endpoint must be set together with v4_endpoint", |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + for _, tc := range tests { |
| 101 | + t.Run(tc.description, func(t *testing.T) { |
| 102 | + err := tc.source.Validate() |
| 103 | + |
| 104 | + if tc.wantErr != "" { |
| 105 | + if err == nil { |
| 106 | + t.Logf("Expected error '%s', got nothing", tc.wantErr) |
| 107 | + t.Fail() |
| 108 | + } |
| 109 | + assert.EqualError(t, err, tc.wantErr, fmt.Sprintf("Expected '%s', got '%s'", tc.wantErr, err)) |
| 110 | + } |
| 111 | + |
| 112 | + if tc.wantErr == "" && err != nil { |
| 113 | + t.Logf("Got an error when none expected: %s", err) |
| 114 | + t.Fail() |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments