From d2f1bbb5c797dd9acd168d18aff40473d2834cfc Mon Sep 17 00:00:00 2001 From: ashmrtn <3891298+ashmrtn@users.noreply.github.com> Date: Mon, 29 Jan 2024 18:41:53 -0800 Subject: [PATCH] Skip ms auth checks for gock client (#5163) Disable sending requests to microsoft servers to get API tokens when using the gock client. This is accomplished by passing in a mock that always returns no error for auth requests. **This PR does not go through and make existing tests using gock unit tests instead of integration tests. That will need to be done separately** --- #### Does this PR need a docs update or release note? - [ ] :white_check_mark: Yes, it's included - [ ] :clock1: Yes, but in a later PR - [x] :no_entry: No #### Type of change - [ ] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [x] :robot: Supportability/Tests - [ ] :computer: CI/Deployment - [ ] :broom: Tech Debt/Cleanup #### Issue(s) * #5124 #### Test Plan - [x] :muscle: Manual - [ ] :zap: Unit test - [ ] :green_heart: E2E --- .../services/m365/api/graph/service_mock.go | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/pkg/services/m365/api/graph/service_mock.go b/src/pkg/services/m365/api/graph/service_mock.go index 96373c198..b90c312f7 100644 --- a/src/pkg/services/m365/api/graph/service_mock.go +++ b/src/pkg/services/m365/api/graph/service_mock.go @@ -1,6 +1,8 @@ package graph import ( + "context" + "github.com/alcionai/clues" "github.com/h2non/gock" abstractions "github.com/microsoft/kiota-abstractions-go" @@ -10,6 +12,23 @@ import ( "github.com/alcionai/corso/src/pkg/count" ) +// authMock implements the +// github.com/microsoft.kiota-abstractions-go/authentication:AuthenticationProvider +// interface. +type authMock struct{} + +// AuthenticateRequest is the function called prior to sending a graph API +// request. It ensures the client has the proper authentication to send the +// request. Returning nil allows us to skip the authentication that would +// normally happen. +func (a authMock) AuthenticateRequest( + context.Context, + *abstractions.RequestInformation, + map[string]any, +) error { + return nil +} + func NewGockService( creds account.M365Config, counter *count.Bus, @@ -35,11 +54,6 @@ func CreateGockAdapter( counter *count.Bus, opts ...Option, ) (abstractions.RequestAdapter, error) { - auth, err := GetAuth(tenant, client, secret) - if err != nil { - return nil, err - } - httpClient, cc := KiotaHTTPClient(counter, opts...) // This makes sure that we are able to intercept any requests via @@ -47,7 +61,9 @@ func CreateGockAdapter( gock.InterceptClient(httpClient) ng, err := msgraphsdkgo.NewGraphRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient( - auth, + // Use our own mock auth instance. This allows us to completely avoid having + // to make requests to microsoft servers during testing. + authMock{}, nil, nil, httpClient)