remove exchangeService for graph.Service (#2002)

## Description

the exchangeService struct is a vestigial prop
from before we had centralized around the
graph.Service, and is no longer needed.  These
changes will cascade into the migration of
graph client usage into an api package for mocking.

## Does this PR need a docs update or release note?

- [x]  No 

## Type of change

- [x] 🐹 Trivial/Minor

## Issue(s)

* #1967

## Test Plan

- [x]  Unit test
- [x] 💚 E2E
This commit is contained in:
Keepers 2023-01-03 12:49:09 -07:00 committed by GitHub
parent e4e1b4d4d8
commit b15ce2807b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 70 deletions

View File

@ -21,7 +21,8 @@ import (
type ExchangeServiceSuite struct { type ExchangeServiceSuite struct {
suite.Suite suite.Suite
es *exchangeService credentials account.M365Config
gs graph.Servicer
} }
func TestExchangeServiceSuite(t *testing.T) { func TestExchangeServiceSuite(t *testing.T) {
@ -43,12 +44,15 @@ func (suite *ExchangeServiceSuite) SetupSuite() {
a := tester.NewM365Account(t) a := tester.NewM365Account(t)
require.NoError(t, err) require.NoError(t, err)
m365, err := a.M365Config() m365, err := a.M365Config()
require.NoError(t, err) require.NoError(t, err)
service, err := createService(m365) service, err := createService(m365)
require.NoError(t, err) require.NoError(t, err)
suite.es = service suite.credentials = m365
suite.gs = service
} }
// TestCreateService verifies that services are created // TestCreateService verifies that services are created
@ -56,8 +60,8 @@ func (suite *ExchangeServiceSuite) SetupSuite() {
// incorrect tenant or password information will NOT generate // incorrect tenant or password information will NOT generate
// an error. // an error.
func (suite *ExchangeServiceSuite) TestCreateService() { func (suite *ExchangeServiceSuite) TestCreateService() {
creds := suite.es.credentials creds := suite.credentials
invalidCredentials := suite.es.credentials invalidCredentials := suite.credentials
invalidCredentials.AzureClientSecret = "" invalidCredentials.AzureClientSecret = ""
tests := []struct { tests := []struct {
@ -235,7 +239,7 @@ func (suite *ExchangeServiceSuite) TestGraphQueryFunctions() {
for _, test := range tests { for _, test := range tests {
suite.T().Run(test.name, func(t *testing.T) { suite.T().Run(test.name, func(t *testing.T) {
response, err := test.function(ctx, suite.es, userID) response, err := test.function(ctx, suite.gs, userID)
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, response) assert.NotNil(t, response)
}) })
@ -259,20 +263,20 @@ func (suite *ExchangeServiceSuite) TestRestoreContact() {
folderName = "TestRestoreContact: " + common.FormatSimpleDateTime(now) folderName = "TestRestoreContact: " + common.FormatSimpleDateTime(now)
) )
aFolder, err := CreateContactFolder(ctx, suite.es, userID, folderName) aFolder, err := CreateContactFolder(ctx, suite.gs, userID, folderName)
require.NoError(t, err) require.NoError(t, err)
folderID := *aFolder.GetId() folderID := *aFolder.GetId()
defer func() { defer func() {
// Remove the folder containing contact prior to exiting test // Remove the folder containing contact prior to exiting test
err = DeleteContactFolder(ctx, suite.es, userID, folderID) err = DeleteContactFolder(ctx, suite.gs, userID, folderID)
assert.NoError(t, err) assert.NoError(t, err)
}() }()
info, err := RestoreExchangeContact(ctx, info, err := RestoreExchangeContact(ctx,
mockconnector.GetMockContactBytes("Corso TestContact"), mockconnector.GetMockContactBytes("Corso TestContact"),
suite.es, suite.gs,
control.Copy, control.Copy,
folderID, folderID,
userID) userID)
@ -292,20 +296,20 @@ func (suite *ExchangeServiceSuite) TestRestoreEvent() {
name = "TestRestoreEvent: " + common.FormatSimpleDateTime(time.Now()) name = "TestRestoreEvent: " + common.FormatSimpleDateTime(time.Now())
) )
calendar, err := CreateCalendar(ctx, suite.es, userID, name) calendar, err := CreateCalendar(ctx, suite.gs, userID, name)
require.NoError(t, err) require.NoError(t, err)
calendarID := *calendar.GetId() calendarID := *calendar.GetId()
defer func() { defer func() {
// Removes calendar containing events created during the test // Removes calendar containing events created during the test
err = DeleteCalendar(ctx, suite.es, userID, calendarID) err = DeleteCalendar(ctx, suite.gs, userID, calendarID)
assert.NoError(t, err) assert.NoError(t, err)
}() }()
info, err := RestoreExchangeEvent(ctx, info, err := RestoreExchangeEvent(ctx,
mockconnector.GetMockEventWithAttendeesBytes(name), mockconnector.GetMockEventWithAttendeesBytes(name),
suite.es, suite.gs,
control.Copy, control.Copy,
calendarID, calendarID,
userID) userID)
@ -315,7 +319,12 @@ func (suite *ExchangeServiceSuite) TestRestoreEvent() {
// TestRestoreExchangeObject verifies path.Category usage for restored objects // TestRestoreExchangeObject verifies path.Category usage for restored objects
func (suite *ExchangeServiceSuite) TestRestoreExchangeObject() { func (suite *ExchangeServiceSuite) TestRestoreExchangeObject() {
service := loadService(suite.T()) a := tester.NewM365Account(suite.T())
m365, err := a.M365Config()
require.NoError(suite.T(), err)
service, err := createService(m365)
require.NoError(suite.T(), err)
userID := tester.M365UserID(suite.T()) userID := tester.M365UserID(suite.T())
now := time.Now() now := time.Now()
@ -333,7 +342,7 @@ func (suite *ExchangeServiceSuite) TestRestoreExchangeObject() {
cleanupFunc: DeleteMailFolder, cleanupFunc: DeleteMailFolder,
destination: func(t *testing.T, ctx context.Context) string { destination: func(t *testing.T, ctx context.Context) string {
folderName := "TestRestoreMailObject: " + common.FormatSimpleDateTime(now) folderName := "TestRestoreMailObject: " + common.FormatSimpleDateTime(now)
folder, err := CreateMailFolder(ctx, suite.es, userID, folderName) folder, err := CreateMailFolder(ctx, suite.gs, userID, folderName)
require.NoError(t, err) require.NoError(t, err)
return *folder.GetId() return *folder.GetId()
@ -346,7 +355,7 @@ func (suite *ExchangeServiceSuite) TestRestoreExchangeObject() {
cleanupFunc: DeleteMailFolder, cleanupFunc: DeleteMailFolder,
destination: func(t *testing.T, ctx context.Context) string { destination: func(t *testing.T, ctx context.Context) string {
folderName := "TestRestoreMailwithAttachment: " + common.FormatSimpleDateTime(now) folderName := "TestRestoreMailwithAttachment: " + common.FormatSimpleDateTime(now)
folder, err := CreateMailFolder(ctx, suite.es, userID, folderName) folder, err := CreateMailFolder(ctx, suite.gs, userID, folderName)
require.NoError(t, err) require.NoError(t, err)
return *folder.GetId() return *folder.GetId()
@ -359,7 +368,7 @@ func (suite *ExchangeServiceSuite) TestRestoreExchangeObject() {
cleanupFunc: DeleteMailFolder, cleanupFunc: DeleteMailFolder,
destination: func(t *testing.T, ctx context.Context) string { destination: func(t *testing.T, ctx context.Context) string {
folderName := "TestRestoreMailwithLargeAttachment: " + common.FormatSimpleDateTime(now) folderName := "TestRestoreMailwithLargeAttachment: " + common.FormatSimpleDateTime(now)
folder, err := CreateMailFolder(ctx, suite.es, userID, folderName) folder, err := CreateMailFolder(ctx, suite.gs, userID, folderName)
require.NoError(t, err) require.NoError(t, err)
return *folder.GetId() return *folder.GetId()
@ -372,7 +381,7 @@ func (suite *ExchangeServiceSuite) TestRestoreExchangeObject() {
cleanupFunc: DeleteMailFolder, cleanupFunc: DeleteMailFolder,
destination: func(t *testing.T, ctx context.Context) string { destination: func(t *testing.T, ctx context.Context) string {
folderName := "TestRestoreMailwithAttachments: " + common.FormatSimpleDateTime(now) folderName := "TestRestoreMailwithAttachments: " + common.FormatSimpleDateTime(now)
folder, err := CreateMailFolder(ctx, suite.es, userID, folderName) folder, err := CreateMailFolder(ctx, suite.gs, userID, folderName)
require.NoError(t, err) require.NoError(t, err)
return *folder.GetId() return *folder.GetId()
@ -385,7 +394,7 @@ func (suite *ExchangeServiceSuite) TestRestoreExchangeObject() {
cleanupFunc: DeleteMailFolder, cleanupFunc: DeleteMailFolder,
destination: func(t *testing.T, ctx context.Context) string { destination: func(t *testing.T, ctx context.Context) string {
folderName := "TestRestoreMailwithReferenceAttachment: " + common.FormatSimpleDateTime(now) folderName := "TestRestoreMailwithReferenceAttachment: " + common.FormatSimpleDateTime(now)
folder, err := CreateMailFolder(ctx, suite.es, userID, folderName) folder, err := CreateMailFolder(ctx, suite.gs, userID, folderName)
require.NoError(t, err) require.NoError(t, err)
return *folder.GetId() return *folder.GetId()
@ -399,7 +408,7 @@ func (suite *ExchangeServiceSuite) TestRestoreExchangeObject() {
cleanupFunc: DeleteContactFolder, cleanupFunc: DeleteContactFolder,
destination: func(t *testing.T, ctx context.Context) string { destination: func(t *testing.T, ctx context.Context) string {
folderName := "TestRestoreContactObject: " + common.FormatSimpleDateTime(now) folderName := "TestRestoreContactObject: " + common.FormatSimpleDateTime(now)
folder, err := CreateContactFolder(ctx, suite.es, userID, folderName) folder, err := CreateContactFolder(ctx, suite.gs, userID, folderName)
require.NoError(t, err) require.NoError(t, err)
return *folder.GetId() return *folder.GetId()
@ -412,7 +421,7 @@ func (suite *ExchangeServiceSuite) TestRestoreExchangeObject() {
cleanupFunc: DeleteCalendar, cleanupFunc: DeleteCalendar,
destination: func(t *testing.T, ctx context.Context) string { destination: func(t *testing.T, ctx context.Context) string {
calendarName := "TestRestoreEventObject: " + common.FormatSimpleDateTime(now) calendarName := "TestRestoreEventObject: " + common.FormatSimpleDateTime(now)
calendar, err := CreateCalendar(ctx, suite.es, userID, calendarName) calendar, err := CreateCalendar(ctx, suite.gs, userID, calendarName)
require.NoError(t, err) require.NoError(t, err)
return *calendar.GetId() return *calendar.GetId()
@ -425,7 +434,7 @@ func (suite *ExchangeServiceSuite) TestRestoreExchangeObject() {
cleanupFunc: DeleteCalendar, cleanupFunc: DeleteCalendar,
destination: func(t *testing.T, ctx context.Context) string { destination: func(t *testing.T, ctx context.Context) string {
calendarName := "TestRestoreEventObject_" + common.FormatSimpleDateTime(now) calendarName := "TestRestoreEventObject_" + common.FormatSimpleDateTime(now)
calendar, err := CreateCalendar(ctx, suite.es, userID, calendarName) calendar, err := CreateCalendar(ctx, suite.gs, userID, calendarName)
require.NoError(t, err) require.NoError(t, err)
return *calendar.GetId() return *calendar.GetId()
@ -462,9 +471,15 @@ func (suite *ExchangeServiceSuite) TestGetContainerIDFromCache() {
ctx, flush := tester.NewContext() ctx, flush := tester.NewContext()
defer flush() defer flush()
a := tester.NewM365Account(suite.T())
m365, err := a.M365Config()
require.NoError(suite.T(), err)
connector, err := createService(m365)
require.NoError(suite.T(), err)
var ( var (
user = tester.M365UserID(suite.T()) user = tester.M365UserID(suite.T())
connector = loadService(suite.T())
directoryCaches = make(map[path.CategoryType]graph.ContainerResolver) directoryCaches = make(map[path.CategoryType]graph.ContainerResolver)
folderName = tester.DefaultTestRestoreDestination().ContainerName folderName = tester.DefaultTestRestoreDestination().ContainerName
tests = []struct { tests = []struct {
@ -479,7 +494,7 @@ func (suite *ExchangeServiceSuite) TestGetContainerIDFromCache() {
pathFunc1: func(t *testing.T) path.Path { pathFunc1: func(t *testing.T) path.Path {
pth, err := path.Builder{}.Append("Griffindor"). pth, err := path.Builder{}.Append("Griffindor").
Append("Croix").ToDataLayerExchangePathForCategory( Append("Croix").ToDataLayerExchangePathForCategory(
suite.es.credentials.AzureTenantID, suite.credentials.AzureTenantID,
user, user,
path.EmailCategory, path.EmailCategory,
false, false,
@ -491,7 +506,7 @@ func (suite *ExchangeServiceSuite) TestGetContainerIDFromCache() {
pathFunc2: func(t *testing.T) path.Path { pathFunc2: func(t *testing.T) path.Path {
pth, err := path.Builder{}.Append("Griffindor"). pth, err := path.Builder{}.Append("Griffindor").
Append("Felicius").ToDataLayerExchangePathForCategory( Append("Felicius").ToDataLayerExchangePathForCategory(
suite.es.credentials.AzureTenantID, suite.credentials.AzureTenantID,
user, user,
path.EmailCategory, path.EmailCategory,
false, false,
@ -507,7 +522,7 @@ func (suite *ExchangeServiceSuite) TestGetContainerIDFromCache() {
pathFunc1: func(t *testing.T) path.Path { pathFunc1: func(t *testing.T) path.Path {
aPath, err := path.Builder{}.Append("HufflePuff"). aPath, err := path.Builder{}.Append("HufflePuff").
ToDataLayerExchangePathForCategory( ToDataLayerExchangePathForCategory(
suite.es.credentials.AzureTenantID, suite.credentials.AzureTenantID,
user, user,
path.ContactsCategory, path.ContactsCategory,
false, false,
@ -519,7 +534,7 @@ func (suite *ExchangeServiceSuite) TestGetContainerIDFromCache() {
pathFunc2: func(t *testing.T) path.Path { pathFunc2: func(t *testing.T) path.Path {
aPath, err := path.Builder{}.Append("Ravenclaw"). aPath, err := path.Builder{}.Append("Ravenclaw").
ToDataLayerExchangePathForCategory( ToDataLayerExchangePathForCategory(
suite.es.credentials.AzureTenantID, suite.credentials.AzureTenantID,
user, user,
path.ContactsCategory, path.ContactsCategory,
false, false,
@ -535,7 +550,7 @@ func (suite *ExchangeServiceSuite) TestGetContainerIDFromCache() {
pathFunc1: func(t *testing.T) path.Path { pathFunc1: func(t *testing.T) path.Path {
aPath, err := path.Builder{}.Append("Durmstrang"). aPath, err := path.Builder{}.Append("Durmstrang").
ToDataLayerExchangePathForCategory( ToDataLayerExchangePathForCategory(
suite.es.credentials.AzureTenantID, suite.credentials.AzureTenantID,
user, user,
path.EventsCategory, path.EventsCategory,
false, false,
@ -546,7 +561,7 @@ func (suite *ExchangeServiceSuite) TestGetContainerIDFromCache() {
pathFunc2: func(t *testing.T) path.Path { pathFunc2: func(t *testing.T) path.Path {
aPath, err := path.Builder{}.Append("Beauxbatons"). aPath, err := path.Builder{}.Append("Beauxbatons").
ToDataLayerExchangePathForCategory( ToDataLayerExchangePathForCategory(
suite.es.credentials.AzureTenantID, suite.credentials.AzureTenantID,
user, user,
path.EventsCategory, path.EventsCategory,
false, false,

View File

@ -57,17 +57,6 @@ func (suite *ExchangeIteratorSuite) TestDescendable() {
assert.NotNil(t, aDescendable.GetParentFolderId()) assert.NotNil(t, aDescendable.GetParentFolderId())
} }
func loadService(t *testing.T) *exchangeService {
a := tester.NewM365Account(t)
m365, err := a.M365Config()
require.NoError(t, err)
service, err := createService(m365)
require.NoError(t, err)
return service
}
// TestCollectionFunctions verifies ability to gather // TestCollectionFunctions verifies ability to gather
// containers functions are valid for current versioning of msgraph-go-sdk. // containers functions are valid for current versioning of msgraph-go-sdk.
// Tests for mail have been moved to graph_connector_test.go. // Tests for mail have been moved to graph_connector_test.go.
@ -119,24 +108,36 @@ func (suite *ExchangeIteratorSuite) TestCollectionFunctions() {
} }
for _, test := range tests { for _, test := range tests {
suite.T().Run(test.name, func(t *testing.T) { suite.T().Run(test.name, func(t *testing.T) {
service := loadService(t) a := tester.NewM365Account(t)
m365, err := a.M365Config()
require.NoError(t, err)
service, err := createService(m365)
require.NoError(t, err)
response, err := test.queryFunc(ctx, service, userID) response, err := test.queryFunc(ctx, service, userID)
require.NoError(t, err) require.NoError(t, err)
// Iterator Creation // Iterator Creation
pageIterator, err := msgraphgocore.NewPageIterator(response, pageIterator, err := msgraphgocore.NewPageIterator(
&service.adapter, response,
service.Adapter(),
test.transformer) test.transformer)
require.NoError(t, err) require.NoError(t, err)
// Create collection for iterate test // Create collection for iterate test
collections := make(map[string]graph.Container) collections := make(map[string]graph.Container)
var errs error var errs error
errUpdater := func(id string, err error) { errUpdater := func(id string, err error) {
errs = support.WrapAndAppend(id, err, errs) errs = support.WrapAndAppend(id, err, errs)
} }
// callbackFunc iterates through all models.Messageable and fills exchange.Collection.added[] // callbackFunc iterates through all models.Messageable and fills exchange.Collection.added[]
// with corresponding item IDs. New collections are created for each directory // with corresponding item IDs. New collections are created for each directory
callbackFunc := test.iterativeFunction( callbackFunc := test.iterativeFunction(collections, "", errUpdater)
collections, "", errUpdater)
iterateError := pageIterator.Iterate(ctx, callbackFunc) iterateError := pageIterator.Iterate(ctx, callbackFunc)
assert.NoError(t, iterateError) assert.NoError(t, iterateError)
assert.NoError(t, errs) assert.NoError(t, errs)

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
"github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -16,28 +15,10 @@ import (
var ErrFolderNotFound = errors.New("folder not found") var ErrFolderNotFound = errors.New("folder not found")
type exchangeService struct {
client msgraphsdk.GraphServiceClient
adapter msgraphsdk.GraphRequestAdapter
credentials account.M365Config
}
// ------------------------------------------------------------
// Functions to comply with graph.Servicer Interface
// ------------------------------------------------------------
func (es *exchangeService) Client() *msgraphsdk.GraphServiceClient {
return &es.client
}
func (es *exchangeService) Adapter() *msgraphsdk.GraphRequestAdapter {
return &es.adapter
}
// createService internal constructor for exchangeService struct returns an error // createService internal constructor for exchangeService struct returns an error
// iff the params for the entry are incorrect (e.g. len(TenantID) == 0, etc.) // iff the params for the entry are incorrect (e.g. len(TenantID) == 0, etc.)
// NOTE: Incorrect account information will result in errors on subsequent queries. // NOTE: Incorrect account information will result in errors on subsequent queries.
func createService(credentials account.M365Config) (*exchangeService, error) { func createService(credentials account.M365Config) (*graph.Service, error) {
adapter, err := graph.CreateAdapter( adapter, err := graph.CreateAdapter(
credentials.AzureTenantID, credentials.AzureTenantID,
credentials.AzureClientID, credentials.AzureClientID,
@ -47,13 +28,7 @@ func createService(credentials account.M365Config) (*exchangeService, error) {
return nil, errors.Wrap(err, "creating microsoft graph service for exchange") return nil, errors.Wrap(err, "creating microsoft graph service for exchange")
} }
service := exchangeService{ return graph.NewService(adapter), nil
adapter: *adapter,
client: *msgraphsdk.NewGraphServiceClient(adapter),
credentials: credentials,
}
return &service, nil
} }
// CreateMailFolder makes a mail folder iff a folder of the same name does not exist // CreateMailFolder makes a mail folder iff a folder of the same name does not exist