Verified Commit 576fa15e authored by BoxBoxJason's avatar BoxBoxJason Committed by GitLab
Browse files

feat: support renew application secret api endpoint

Changelog: Improvements
parent 86a9417e
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -39,6 +39,12 @@ type (
		// GitLab API docs:
		// https://docs.gitlab.com/api/applications/#delete-an-application
		DeleteApplication(application int64, options ...RequestOptionFunc) (*Response, error)

		// RenewApplicationSecret renews the secret for a specific application.
		//
		// GitLab API docs:
		// https://docs.gitlab.com/api/applications/#renew-an-application-secret
		RenewApplicationSecret(application int64, options ...RequestOptionFunc) (*Application, *Response, error)
	}

	// ApplicationsService handles communication with administrables applications
@@ -106,3 +112,15 @@ func (s *ApplicationsService) DeleteApplication(application int64, options ...Re
	)
	return resp, err
}

// RenewApplicationSecret renews the secret for a specific application.
//
// GitLab API docs:
// https://docs.gitlab.com/api/applications/#renew-an-application-secret
func (s *ApplicationsService) RenewApplicationSecret(application int64, options ...RequestOptionFunc) (*Application, *Response, error) {
	return do[*Application](s.client,
		withMethod(http.MethodPost),
		withPath("applications/%d/renew-secret", application),
		withRequestOpts(options...),
	)
}
+38 −0
Original line number Diff line number Diff line
@@ -102,3 +102,41 @@ func TestDeleteApplication(t *testing.T) {

	assert.Equal(t, http.StatusAccepted, resp.StatusCode)
}

func TestRenewApplicationSecret(t *testing.T) {
	t.Parallel()
	mux, client := setup(t)

	// GIVEN a server that returns an application with a renewed secret
	mux.HandleFunc("/api/v4/applications/1/renew-secret",
		func(w http.ResponseWriter, r *http.Request) {
			testMethod(t, r, http.MethodPost)
			fmt.Fprint(w, `
{
    "id":1,
    "application_id":"abc123",
    "application_name":"testApplication",
    "secret":"newSecret",
    "callback_url":"http://example.com",
    "confidential":true,
    "scopes":["api"]
}`)
		},
	)

	// WHEN renewing the secret for an application
	app, _, err := client.Applications.RenewApplicationSecret(1)
	require.NoError(t, err)

	// THEN the returned application contains the new secret
	want := &Application{
		ID:              1,
		ApplicationID:   "abc123",
		ApplicationName: "testApplication",
		Secret:          "newSecret",
		CallbackURL:     "http://example.com",
		Confidential:    true,
		Scopes:          []string{"api"},
	}
	assert.Equal(t, want, app)
}
+45 −0
Original line number Diff line number Diff line
@@ -172,3 +172,48 @@ func (c *MockApplicationsServiceInterfaceListApplicationsCall) DoAndReturn(f fun
	c.Call = c.Call.DoAndReturn(f)
	return c
}

// RenewApplicationSecret mocks base method.
func (m *MockApplicationsServiceInterface) RenewApplicationSecret(application int64, options ...gitlab.RequestOptionFunc) (*gitlab.Application, *gitlab.Response, error) {
	m.ctrl.T.Helper()
	varargs := []any{application}
	for _, a := range options {
		varargs = append(varargs, a)
	}
	ret := m.ctrl.Call(m, "RenewApplicationSecret", varargs...)
	ret0, _ := ret[0].(*gitlab.Application)
	ret1, _ := ret[1].(*gitlab.Response)
	ret2, _ := ret[2].(error)
	return ret0, ret1, ret2
}

// RenewApplicationSecret indicates an expected call of RenewApplicationSecret.
func (mr *MockApplicationsServiceInterfaceMockRecorder) RenewApplicationSecret(application any, options ...any) *MockApplicationsServiceInterfaceRenewApplicationSecretCall {
	mr.mock.ctrl.T.Helper()
	varargs := append([]any{application}, options...)
	call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RenewApplicationSecret", reflect.TypeOf((*MockApplicationsServiceInterface)(nil).RenewApplicationSecret), varargs...)
	return &MockApplicationsServiceInterfaceRenewApplicationSecretCall{Call: call}
}

// MockApplicationsServiceInterfaceRenewApplicationSecretCall wrap *gomock.Call
type MockApplicationsServiceInterfaceRenewApplicationSecretCall struct {
	*gomock.Call
}

// Return rewrite *gomock.Call.Return
func (c *MockApplicationsServiceInterfaceRenewApplicationSecretCall) Return(arg0 *gitlab.Application, arg1 *gitlab.Response, arg2 error) *MockApplicationsServiceInterfaceRenewApplicationSecretCall {
	c.Call = c.Call.Return(arg0, arg1, arg2)
	return c
}

// Do rewrite *gomock.Call.Do
func (c *MockApplicationsServiceInterfaceRenewApplicationSecretCall) Do(f func(int64, ...gitlab.RequestOptionFunc) (*gitlab.Application, *gitlab.Response, error)) *MockApplicationsServiceInterfaceRenewApplicationSecretCall {
	c.Call = c.Call.Do(f)
	return c
}

// DoAndReturn rewrite *gomock.Call.DoAndReturn
func (c *MockApplicationsServiceInterfaceRenewApplicationSecretCall) DoAndReturn(f func(int64, ...gitlab.RequestOptionFunc) (*gitlab.Application, *gitlab.Response, error)) *MockApplicationsServiceInterfaceRenewApplicationSecretCall {
	c.Call = c.Call.DoAndReturn(f)
	return c
}