Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Skip ms auth checks for gock client (#5163)
Browse files Browse the repository at this point in the history
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?

- [ ] ✅ Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x] ⛔ No

#### Type of change

- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [x] 🤖 Supportability/Tests
- [ ] 💻 CI/Deployment
- [ ] 🧹 Tech Debt/Cleanup

#### Issue(s)

* #5124

#### Test Plan

- [x] 💪 Manual
- [ ] ⚡ Unit test
- [ ] 💚 E2E
  • Loading branch information
ashmrtn authored Jan 30, 2024
1 parent 45b021d commit d2f1bbb
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/pkg/services/m365/api/graph/service_mock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package graph

import (
"context"

"github.com/alcionai/clues"
"github.com/h2non/gock"
abstractions "github.com/microsoft/kiota-abstractions-go"
Expand All @@ -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,
Expand All @@ -35,19 +54,16 @@ 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
// gock. Only necessary for testing.
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)

Expand Down

0 comments on commit d2f1bbb

Please sign in to comment.