|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/cloudbase/garm/params" |
| 5 | +) |
| 6 | + |
| 7 | +const ( |
| 8 | + defaultEndpointName string = "github.com" |
| 9 | + dummyCredentialsName string = "dummy" |
| 10 | +) |
| 11 | + |
| 12 | +func (suite *GarmSuite) TestGithubCredentialsErrorOnDuplicateCredentialsName() { |
| 13 | + t := suite.T() |
| 14 | + t.Log("Testing error on duplicate credentials name") |
| 15 | + creds, err := suite.createDummyCredentials(dummyCredentialsName, defaultEndpointName) |
| 16 | + suite.NoError(err) |
| 17 | + defer suite.DeleteGithubCredential(int64(creds.ID)) |
| 18 | + |
| 19 | + createCredsParams := params.CreateGithubCredentialsParams{ |
| 20 | + Name: dummyCredentialsName, |
| 21 | + Endpoint: defaultEndpointName, |
| 22 | + Description: "GARM test credentials", |
| 23 | + AuthType: params.GithubAuthTypePAT, |
| 24 | + PAT: params.GithubPAT{ |
| 25 | + OAuth2Token: "dummy", |
| 26 | + }, |
| 27 | + } |
| 28 | + _, err = createGithubCredentials(suite.cli, suite.authToken, createCredsParams) |
| 29 | + suite.Error(err, "expected error when creating credentials with duplicate name") |
| 30 | +} |
| 31 | + |
| 32 | +func (suite *GarmSuite) TestGithubCredentialsFailsToDeleteWhenInUse() { |
| 33 | + t := suite.T() |
| 34 | + t.Log("Testing error when deleting credentials in use") |
| 35 | + creds, err := suite.createDummyCredentials(dummyCredentialsName, defaultEndpointName) |
| 36 | + suite.NoError(err) |
| 37 | + |
| 38 | + orgName := "dummy-owner" |
| 39 | + repoName := "dummy-repo" |
| 40 | + createParams := params.CreateRepoParams{ |
| 41 | + Owner: orgName, |
| 42 | + Name: repoName, |
| 43 | + CredentialsName: creds.Name, |
| 44 | + WebhookSecret: "superSecret@123BlaBla", |
| 45 | + } |
| 46 | + |
| 47 | + t.Logf("Create repository with owner_name: %s, repo_name: %s", orgName, repoName) |
| 48 | + repo, err := createRepo(suite.cli, suite.authToken, createParams) |
| 49 | + suite.NoError(err) |
| 50 | + defer func() { |
| 51 | + deleteRepo(suite.cli, suite.authToken, repo.ID) |
| 52 | + deleteGithubCredentials(suite.cli, suite.authToken, int64(creds.ID)) |
| 53 | + }() |
| 54 | + |
| 55 | + err = deleteGithubCredentials(suite.cli, suite.authToken, int64(creds.ID)) |
| 56 | + suite.Error(err, "expected error when deleting credentials in use") |
| 57 | +} |
| 58 | + |
| 59 | +func (suite *GarmSuite) TestGithubCredentialsFailsOnInvalidAuthType() { |
| 60 | + t := suite.T() |
| 61 | + t.Log("Testing error on invalid auth type") |
| 62 | + createCredsParams := params.CreateGithubCredentialsParams{ |
| 63 | + Name: dummyCredentialsName, |
| 64 | + Endpoint: defaultEndpointName, |
| 65 | + Description: "GARM test credentials", |
| 66 | + AuthType: params.GithubAuthType("invalid"), |
| 67 | + PAT: params.GithubPAT{ |
| 68 | + OAuth2Token: "dummy", |
| 69 | + }, |
| 70 | + } |
| 71 | + _, err := createGithubCredentials(suite.cli, suite.authToken, createCredsParams) |
| 72 | + suite.Error(err, "expected error when creating credentials with invalid auth type") |
| 73 | + expectAPIStatusCode(err, 400) |
| 74 | +} |
| 75 | + |
| 76 | +func (suite *GarmSuite) TestGithubCredentialsFailsWhenAuthTypeParamsAreIncorrect() { |
| 77 | + t := suite.T() |
| 78 | + t.Log("Testing error when auth type params are incorrect") |
| 79 | + privateKeyBytes, err := getTestFileContents("certs/srv-key.pem") |
| 80 | + suite.NoError(err) |
| 81 | + createCredsParams := params.CreateGithubCredentialsParams{ |
| 82 | + Name: dummyCredentialsName, |
| 83 | + Endpoint: defaultEndpointName, |
| 84 | + Description: "GARM test credentials", |
| 85 | + AuthType: params.GithubAuthTypePAT, |
| 86 | + App: params.GithubApp{ |
| 87 | + AppID: 123, |
| 88 | + InstallationID: 456, |
| 89 | + PrivateKeyBytes: privateKeyBytes, |
| 90 | + }, |
| 91 | + } |
| 92 | + _, err = createGithubCredentials(suite.cli, suite.authToken, createCredsParams) |
| 93 | + suite.Error(err, "expected error when creating credentials with invalid auth type params") |
| 94 | + |
| 95 | + expectAPIStatusCode(err, 400) |
| 96 | +} |
| 97 | + |
| 98 | +func (suite *GarmSuite) TestGithubCredentialsFailsWhenAuthTypeParamsAreMissing() { |
| 99 | + t := suite.T() |
| 100 | + t.Log("Testing error when auth type params are missing") |
| 101 | + createCredsParams := params.CreateGithubCredentialsParams{ |
| 102 | + Name: dummyCredentialsName, |
| 103 | + Endpoint: defaultEndpointName, |
| 104 | + Description: "GARM test credentials", |
| 105 | + AuthType: params.GithubAuthTypeApp, |
| 106 | + } |
| 107 | + _, err := createGithubCredentials(suite.cli, suite.authToken, createCredsParams) |
| 108 | + suite.Error(err, "expected error when creating credentials with missing auth type params") |
| 109 | + expectAPIStatusCode(err, 400) |
| 110 | +} |
| 111 | + |
| 112 | +func (suite *GarmSuite) TestGithubCredentialsUpdateFailsWhenBothPATAndAppAreSupplied() { |
| 113 | + t := suite.T() |
| 114 | + t.Log("Testing error when both PAT and App are supplied") |
| 115 | + creds, err := suite.createDummyCredentials(dummyCredentialsName, defaultEndpointName) |
| 116 | + suite.NoError(err) |
| 117 | + defer suite.DeleteGithubCredential(int64(creds.ID)) |
| 118 | + |
| 119 | + privateKeyBytes, err := getTestFileContents("certs/srv-key.pem") |
| 120 | + suite.NoError(err) |
| 121 | + updateCredsParams := params.UpdateGithubCredentialsParams{ |
| 122 | + PAT: ¶ms.GithubPAT{ |
| 123 | + OAuth2Token: "dummy", |
| 124 | + }, |
| 125 | + App: ¶ms.GithubApp{ |
| 126 | + AppID: 123, |
| 127 | + InstallationID: 456, |
| 128 | + PrivateKeyBytes: privateKeyBytes, |
| 129 | + }, |
| 130 | + } |
| 131 | + _, err = updateGithubCredentials(suite.cli, suite.authToken, int64(creds.ID), updateCredsParams) |
| 132 | + suite.Error(err, "expected error when updating credentials with both PAT and App") |
| 133 | + expectAPIStatusCode(err, 400) |
| 134 | +} |
| 135 | + |
| 136 | +func (suite *GarmSuite) TestGithubCredentialsFailWhenAppKeyIsInvalid() { |
| 137 | + t := suite.T() |
| 138 | + t.Log("Testing error when app key is invalid") |
| 139 | + createCredsParams := params.CreateGithubCredentialsParams{ |
| 140 | + Name: dummyCredentialsName, |
| 141 | + Endpoint: defaultEndpointName, |
| 142 | + Description: "GARM test credentials", |
| 143 | + AuthType: params.GithubAuthTypeApp, |
| 144 | + App: params.GithubApp{ |
| 145 | + AppID: 123, |
| 146 | + InstallationID: 456, |
| 147 | + PrivateKeyBytes: []byte("invalid"), |
| 148 | + }, |
| 149 | + } |
| 150 | + _, err := createGithubCredentials(suite.cli, suite.authToken, createCredsParams) |
| 151 | + suite.Error(err, "expected error when creating credentials with invalid app key") |
| 152 | + expectAPIStatusCode(err, 400) |
| 153 | +} |
| 154 | + |
| 155 | +func (suite *GarmSuite) TestGithubCredentialsFailWhenEndpointDoesntExist() { |
| 156 | + t := suite.T() |
| 157 | + t.Log("Testing error when endpoint doesn't exist") |
| 158 | + createCredsParams := params.CreateGithubCredentialsParams{ |
| 159 | + Name: dummyCredentialsName, |
| 160 | + Endpoint: "iDontExist.example.com", |
| 161 | + Description: "GARM test credentials", |
| 162 | + AuthType: params.GithubAuthTypePAT, |
| 163 | + PAT: params.GithubPAT{ |
| 164 | + OAuth2Token: "dummy", |
| 165 | + }, |
| 166 | + } |
| 167 | + _, err := createGithubCredentials(suite.cli, suite.authToken, createCredsParams) |
| 168 | + suite.Error(err, "expected error when creating credentials with invalid endpoint") |
| 169 | + expectAPIStatusCode(err, 404) |
| 170 | +} |
| 171 | + |
| 172 | +func (suite *GarmSuite) TestGithubCredentialsFailsOnDuplicateName() { |
| 173 | + t := suite.T() |
| 174 | + t.Log("Testing error on duplicate credentials name") |
| 175 | + creds, err := suite.createDummyCredentials(dummyCredentialsName, defaultEndpointName) |
| 176 | + suite.NoError(err) |
| 177 | + defer suite.DeleteGithubCredential(int64(creds.ID)) |
| 178 | + |
| 179 | + createCredsParams := params.CreateGithubCredentialsParams{ |
| 180 | + Name: dummyCredentialsName, |
| 181 | + Endpoint: defaultEndpointName, |
| 182 | + Description: "GARM test credentials", |
| 183 | + AuthType: params.GithubAuthTypePAT, |
| 184 | + PAT: params.GithubPAT{ |
| 185 | + OAuth2Token: "dummy", |
| 186 | + }, |
| 187 | + } |
| 188 | + _, err = createGithubCredentials(suite.cli, suite.authToken, createCredsParams) |
| 189 | + suite.Error(err, "expected error when creating credentials with duplicate name") |
| 190 | + expectAPIStatusCode(err, 409) |
| 191 | +} |
| 192 | + |
| 193 | +func (suite *GarmSuite) createDummyCredentials(name, endpointName string) (*params.GithubCredentials, error) { |
| 194 | + createCredsParams := params.CreateGithubCredentialsParams{ |
| 195 | + Name: name, |
| 196 | + Endpoint: endpointName, |
| 197 | + Description: "GARM test credentials", |
| 198 | + AuthType: params.GithubAuthTypePAT, |
| 199 | + PAT: params.GithubPAT{ |
| 200 | + OAuth2Token: "dummy", |
| 201 | + }, |
| 202 | + } |
| 203 | + return suite.CreateGithubCredentials(createCredsParams) |
| 204 | +} |
| 205 | + |
| 206 | +func (suite *GarmSuite) CreateGithubCredentials(credentialsParams params.CreateGithubCredentialsParams) (*params.GithubCredentials, error) { |
| 207 | + t := suite.T() |
| 208 | + t.Log("Create GitHub credentials") |
| 209 | + credentials, err := createGithubCredentials(suite.cli, suite.authToken, credentialsParams) |
| 210 | + if err != nil { |
| 211 | + return nil, err |
| 212 | + } |
| 213 | + |
| 214 | + return credentials, nil |
| 215 | +} |
| 216 | + |
| 217 | +func (suite *GarmSuite) DeleteGithubCredential(id int64) error { |
| 218 | + t := suite.T() |
| 219 | + t.Log("Delete GitHub credential") |
| 220 | + if err := deleteGithubCredentials(suite.cli, suite.authToken, id); err != nil { |
| 221 | + return err |
| 222 | + } |
| 223 | + return nil |
| 224 | +} |
0 commit comments