Skip to content

Commit 0128f59

Browse files
Add some credentials e2e tests
Signed-off-by: Gabriel Adrian Samfira <[email protected]>
1 parent 39a5e14 commit 0128f59

File tree

7 files changed

+303
-4
lines changed

7 files changed

+303
-4
lines changed

database/sql/github.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ func (s *sqlDatabase) CreateGithubCredentials(ctx context.Context, param params.
279279
return errors.Wrap(err, "fetching github endpoint")
280280
}
281281

282-
if err := tx.Where("name = ?", param.Name).First(&creds).Error; err == nil {
283-
return errors.New("github credentials already exists")
282+
if err := tx.Where("name = ? and user_id = ?", param.Name, userID).First(&creds).Error; err == nil {
283+
return errors.Wrap(runnerErrors.ErrDuplicateEntity, "github credentials already exists")
284284
}
285285

286286
var data []byte
@@ -449,15 +449,15 @@ func (s *sqlDatabase) UpdateGithubCredentials(ctx context.Context, id uint, para
449449
}
450450

451451
if param.App != nil {
452-
return errors.New("cannot update app credentials for PAT")
452+
return errors.Wrap(runnerErrors.ErrBadRequest, "cannot update app credentials for PAT")
453453
}
454454
case params.GithubAuthTypeApp:
455455
if param.App != nil {
456456
data, err = s.marshalAndSeal(param.App)
457457
}
458458

459459
if param.PAT != nil {
460-
return errors.New("cannot update PAT credentials for app")
460+
return errors.Wrap(runnerErrors.ErrBadRequest, "cannot update PAT credentials for app")
461461
}
462462
}
463463

test/integration/e2e/client_utils.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ func getGithubCredential(apiCli *client.GarmAPI, apiAuthToken runtime.ClientAuth
7777
return &getCredentialsResponse.Payload, nil
7878
}
7979

80+
func updateGithubCredentials(apiCli *client.GarmAPI, apiAuthToken runtime.ClientAuthInfoWriter, credentialsID int64, credentialsParams params.UpdateGithubCredentialsParams) (*params.GithubCredentials, error) {
81+
updateCredentialsResponse, err := apiCli.Credentials.UpdateCredentials(
82+
clientCredentials.NewUpdateCredentialsParams().WithID(credentialsID).WithBody(credentialsParams),
83+
apiAuthToken)
84+
if err != nil {
85+
return nil, err
86+
}
87+
return &updateCredentialsResponse.Payload, nil
88+
}
89+
8090
func createGithubEndpoint(apiCli *client.GarmAPI, apiAuthToken runtime.ClientAuthInfoWriter, endpointParams params.CreateGithubEndpointParams) (*params.GithubEndpoint, error) {
8191
createEndpointResponse, err := apiCli.Endpoints.CreateGithubEndpoint(
8292
clientEndpoints.NewCreateGithubEndpointParams().WithBody(endpointParams),
@@ -113,6 +123,16 @@ func deleteGithubEndpoint(apiCli *client.GarmAPI, apiAuthToken runtime.ClientAut
113123
apiAuthToken)
114124
}
115125

126+
func updateGithubEndpoint(apiCli *client.GarmAPI, apiAuthToken runtime.ClientAuthInfoWriter, endpointName string, endpointParams params.UpdateGithubEndpointParams) (*params.GithubEndpoint, error) {
127+
updateEndpointResponse, err := apiCli.Endpoints.UpdateGithubEndpoint(
128+
clientEndpoints.NewUpdateGithubEndpointParams().WithName(endpointName).WithBody(endpointParams),
129+
apiAuthToken)
130+
if err != nil {
131+
return nil, err
132+
}
133+
return &updateEndpointResponse.Payload, nil
134+
}
135+
116136
// listProviders lists all the providers configured in GARM.
117137
func listProviders(apiCli *client.GarmAPI, apiAuthToken runtime.ClientAuthInfoWriter) (params.Providers, error) {
118138
listProvidersResponse, err := apiCli.Providers.ListProviders(

test/integration/e2e/credentials.go

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,185 @@ func createDummyCredentials(name, endpointName string) *params.GithubCredentials
3434
}
3535
return CreateGithubCredentials(createCredsParams)
3636
}
37+
38+
func TestGithubCredentialsErrorOnDuplicateCredentialsName() {
39+
name := "dummy"
40+
endpointName := "github.com"
41+
creds := createDummyCredentials(name, endpointName)
42+
defer DeleteGithubCredential(int64(creds.ID))
43+
44+
createCredsParams := params.CreateGithubCredentialsParams{
45+
Name: name,
46+
Endpoint: endpointName,
47+
Description: "GARM test credentials",
48+
AuthType: params.GithubAuthTypePAT,
49+
PAT: params.GithubPAT{
50+
OAuth2Token: "dummy",
51+
},
52+
}
53+
if _, err := createGithubCredentials(cli, authToken, createCredsParams); err == nil {
54+
panic("expected error when creating credentials with duplicate name")
55+
}
56+
}
57+
58+
func TestGithubCredentialsFailsToDeleteWhenInUse() {
59+
name := "dummy"
60+
endpointName := "github.com"
61+
creds := createDummyCredentials(name, endpointName)
62+
63+
repo := CreateRepo("dummy-owner", "dummy-repo", creds.Name, "superSecret@123BlaBla")
64+
defer func() {
65+
deleteRepo(cli, authToken, repo.ID)
66+
deleteGithubCredentials(cli, authToken, int64(creds.ID))
67+
}()
68+
69+
if err := deleteGithubCredentials(cli, authToken, int64(creds.ID)); err == nil {
70+
panic("expected error when deleting credentials in use")
71+
}
72+
}
73+
74+
func TestGithubCredentialsFailsOnInvalidAuthType() {
75+
name := "dummy"
76+
endpointName := "github.com"
77+
78+
createCredsParams := params.CreateGithubCredentialsParams{
79+
Name: name,
80+
Endpoint: endpointName,
81+
Description: "GARM test credentials",
82+
AuthType: params.GithubAuthType("invalid"),
83+
PAT: params.GithubPAT{
84+
OAuth2Token: "dummy",
85+
},
86+
}
87+
_, err := createGithubCredentials(cli, authToken, createCredsParams)
88+
if err == nil {
89+
panic("expected error when creating credentials with invalid auth type")
90+
}
91+
expectAPIStatusCode(err, 400)
92+
}
93+
94+
func TestGithubCredentialsFailsWhenAuthTypeParamsAreIncorrect() {
95+
name := "dummy"
96+
endpointName := "github.com"
97+
98+
createCredsParams := params.CreateGithubCredentialsParams{
99+
Name: name,
100+
Endpoint: endpointName,
101+
Description: "GARM test credentials",
102+
AuthType: params.GithubAuthTypePAT,
103+
App: params.GithubApp{
104+
AppID: 123,
105+
InstallationID: 456,
106+
PrivateKeyBytes: getTestFileContents("certs/srv-key.pem"),
107+
},
108+
}
109+
_, err := createGithubCredentials(cli, authToken, createCredsParams)
110+
if err == nil {
111+
panic("expected error when creating credentials with invalid auth type params")
112+
}
113+
expectAPIStatusCode(err, 400)
114+
}
115+
116+
func TestGithubCredentialsFailsWhenAuthTypeParamsAreMissing() {
117+
name := "dummy"
118+
endpointName := "github.com"
119+
120+
createCredsParams := params.CreateGithubCredentialsParams{
121+
Name: name,
122+
Endpoint: endpointName,
123+
Description: "GARM test credentials",
124+
AuthType: params.GithubAuthTypeApp,
125+
}
126+
_, err := createGithubCredentials(cli, authToken, createCredsParams)
127+
if err == nil {
128+
panic("expected error when creating credentials with missing auth type params")
129+
}
130+
expectAPIStatusCode(err, 400)
131+
}
132+
133+
func TestGithubCredentialsUpdateFailsWhenBothPATAndAppAreSupplied() {
134+
name := "dummy"
135+
endpointName := "github.com"
136+
creds := createDummyCredentials(name, endpointName)
137+
defer DeleteGithubCredential(int64(creds.ID))
138+
139+
updateCredsParams := params.UpdateGithubCredentialsParams{
140+
PAT: &params.GithubPAT{
141+
OAuth2Token: "dummy",
142+
},
143+
App: &params.GithubApp{
144+
AppID: 123,
145+
InstallationID: 456,
146+
PrivateKeyBytes: getTestFileContents("certs/srv-key.pem"),
147+
},
148+
}
149+
_, err := updateGithubCredentials(cli, authToken, int64(creds.ID), updateCredsParams)
150+
if err == nil {
151+
panic("expected error when updating credentials with both PAT and App")
152+
}
153+
expectAPIStatusCode(err, 400)
154+
}
155+
156+
func TestGithubCredentialsFailWhenAppKeyIsInvalid() {
157+
name := "dummy"
158+
endpointName := "github.com"
159+
160+
createCredsParams := params.CreateGithubCredentialsParams{
161+
Name: name,
162+
Endpoint: endpointName,
163+
Description: "GARM test credentials",
164+
AuthType: params.GithubAuthTypeApp,
165+
App: params.GithubApp{
166+
AppID: 123,
167+
InstallationID: 456,
168+
PrivateKeyBytes: []byte("invalid"),
169+
},
170+
}
171+
_, err := createGithubCredentials(cli, authToken, createCredsParams)
172+
if err == nil {
173+
panic("expected error when creating credentials with invalid app key")
174+
}
175+
expectAPIStatusCode(err, 400)
176+
}
177+
178+
func TestGithubCredentialsFailWhenEndpointDoesntExist() {
179+
name := "dummy"
180+
endpointName := "nonexistent"
181+
182+
createCredsParams := params.CreateGithubCredentialsParams{
183+
Name: name,
184+
Endpoint: endpointName,
185+
Description: "GARM test credentials",
186+
AuthType: params.GithubAuthTypePAT,
187+
PAT: params.GithubPAT{
188+
OAuth2Token: "dummy",
189+
},
190+
}
191+
_, err := createGithubCredentials(cli, authToken, createCredsParams)
192+
if err == nil {
193+
panic("expected error when creating credentials with invalid endpoint")
194+
}
195+
expectAPIStatusCode(err, 404)
196+
}
197+
198+
func TestGithubCredentialsFailsOnDuplicateName() {
199+
name := "dummy"
200+
endpointName := "github.com"
201+
creds := createDummyCredentials(name, endpointName)
202+
defer DeleteGithubCredential(int64(creds.ID))
203+
204+
createCredsParams := params.CreateGithubCredentialsParams{
205+
Name: name,
206+
Endpoint: endpointName,
207+
Description: "GARM test credentials",
208+
AuthType: params.GithubAuthTypePAT,
209+
PAT: params.GithubPAT{
210+
OAuth2Token: "dummy",
211+
},
212+
}
213+
_, err := createGithubCredentials(cli, authToken, createCredsParams)
214+
if err == nil {
215+
panic("expected error when creating credentials with duplicate name")
216+
}
217+
expectAPIStatusCode(err, 409)
218+
}

test/integration/e2e/e2e.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ func DeleteGithubEndpoint(name string) {
7777
}
7878
}
7979

80+
func UpdateGithubEndpoint(name string, updateParams params.UpdateGithubEndpointParams) *params.GithubEndpoint {
81+
slog.Info("Update GitHub endpoint")
82+
updated, err := updateGithubEndpoint(cli, authToken, name, updateParams)
83+
if err != nil {
84+
panic(err)
85+
}
86+
return updated
87+
}
88+
8089
func ListProviders() params.Providers {
8190
slog.Info("List providers")
8291
providers, err := listProviders(cli, authToken)

test/integration/e2e/endpoints.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,64 @@ func TestGithubEndpointFailsOnDuplicateName() {
177177
panic("expected error when creating endpoint with duplicate name")
178178
}
179179
}
180+
181+
func TestGithubEndpointUpdateEndpoint() {
182+
slog.Info("Testing endpoint update")
183+
endpoint := createDummyEndpoint("dummy")
184+
defer DeleteGithubEndpoint(endpoint.Name)
185+
186+
newDescription := "Updated description"
187+
newBaseURL := "https://ghes2.example.com"
188+
newAPIBaseURL := "https://api.ghes2.example.com/"
189+
newUploadBaseURL := "https://uploads.ghes2.example.com/"
190+
newCABundle := getTestFileContents("certs/srv-pub.pem")
191+
192+
updateParams := params.UpdateGithubEndpointParams{
193+
Description: &newDescription,
194+
BaseURL: &newBaseURL,
195+
APIBaseURL: &newAPIBaseURL,
196+
UploadBaseURL: &newUploadBaseURL,
197+
CACertBundle: newCABundle,
198+
}
199+
200+
updated, err := updateGithubEndpoint(cli, authToken, endpoint.Name, updateParams)
201+
if err != nil {
202+
panic(err)
203+
}
204+
205+
if updated.Name != endpoint.Name {
206+
panic("Endpoint name mismatch")
207+
}
208+
209+
if updated.Description != newDescription {
210+
panic("Endpoint description mismatch")
211+
}
212+
213+
if updated.BaseURL != newBaseURL {
214+
panic("Endpoint base URL mismatch")
215+
}
216+
217+
if updated.APIBaseURL != newAPIBaseURL {
218+
panic("Endpoint API base URL mismatch")
219+
}
220+
221+
if updated.UploadBaseURL != newUploadBaseURL {
222+
panic("Endpoint upload base URL mismatch")
223+
}
224+
225+
if string(updated.CACertBundle) != string(newCABundle) {
226+
panic("Endpoint CA cert bundle mismatch")
227+
}
228+
}
229+
230+
func createDummyEndpoint(name string) *params.GithubEndpoint {
231+
endpointParams := params.CreateGithubEndpointParams{
232+
Name: name,
233+
Description: "Dummy endpoint",
234+
BaseURL: "https://ghes.example.com",
235+
APIBaseURL: "https://api.ghes.example.com/",
236+
UploadBaseURL: "https://uploads.ghes.example.com/",
237+
}
238+
239+
return CreateGithubEndpoint(endpointParams)
240+
}

test/integration/e2e/utils.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package e2e
22

33
import (
44
"encoding/json"
5+
"log"
56
"log/slog"
67
)
78

@@ -13,3 +14,20 @@ func printJSONResponse(resp interface{}) error {
1314
slog.Info(string(b))
1415
return nil
1516
}
17+
18+
type apiCodeGetter interface {
19+
IsCode(code int) bool
20+
}
21+
22+
func expectAPIStatusCode(err error, expectedCode int) {
23+
if err == nil {
24+
panic("expected error")
25+
}
26+
apiErr, ok := err.(apiCodeGetter)
27+
if !ok {
28+
log.Fatalf("expected API error, got %v (%T)", err, err)
29+
}
30+
if !apiErr.IsCode(expectedCode) {
31+
log.Fatalf("expected status code %d", expectedCode)
32+
}
33+
}

test/integration/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ func main() {
8585

8686
// Create test credentials
8787
e2e.EnsureTestCredentials(credentialsName, ghToken, "github.com")
88+
e2e.TestGithubCredentialsErrorOnDuplicateCredentialsName()
89+
e2e.TestGithubCredentialsFailsToDeleteWhenInUse()
90+
e2e.TestGithubCredentialsFailsOnInvalidAuthType()
91+
e2e.TestGithubCredentialsFailsWhenAuthTypeParamsAreIncorrect()
92+
e2e.TestGithubCredentialsFailsWhenAuthTypeParamsAreMissing()
93+
e2e.TestGithubCredentialsUpdateFailsWhenBothPATAndAppAreSupplied()
94+
e2e.TestGithubCredentialsFailWhenAppKeyIsInvalid()
95+
e2e.TestGithubCredentialsFailWhenEndpointDoesntExist()
96+
e2e.TestGithubCredentialsFailsOnDuplicateName()
8897

8998
// //////////////////
9099
// controller info //

0 commit comments

Comments
 (0)