diff --git a/src/pkg/services/m365/m365.go b/src/pkg/services/m365/m365.go index 948650ef6..b53fd047c 100644 --- a/src/pkg/services/m365/m365.go +++ b/src/pkg/services/m365/m365.go @@ -86,6 +86,34 @@ func UserPNs(ctx context.Context, acct account.Account, errs *fault.Bus) ([]stri return ret, nil } +type Site struct { + // WebURL that displays the item in the browser + WebURL string + + // ID is of the format: .. + // for example: contoso.sharepoint.com,abcdeab3-0ccc-4ce1-80ae-b32912c9468d,xyzud296-9f7c-44e1-af81-3c06d0d43007 + ID string +} + +// Sites returns a list of Sites in a specified M365 tenant +func Sites(ctx context.Context, acct account.Account, errs *fault.Bus) ([]*Site, error) { + gc, err := connector.NewGraphConnector(ctx, graph.HTTPClient(graph.NoTimeout()), acct, connector.Sites, errs) + if err != nil { + return nil, errors.Wrap(err, "initializing M365 graph connection") + } + + // gc.Sites is a map with keys: SiteURL, values: ID + ret := make([]*Site, 0, len(gc.Sites)) + for k, v := range gc.Sites { + ret = append(ret, &Site{ + WebURL: k, + ID: v, + }) + } + + return ret, nil +} + // SiteURLs returns a list of SharePoint site WebURLs in the specified M365 tenant func SiteURLs(ctx context.Context, acct account.Account, errs *fault.Bus) ([]string, error) { gc, err := connector.NewGraphConnector(ctx, graph.HTTPClient(graph.NoTimeout()), acct, connector.Sites, errs) @@ -96,7 +124,7 @@ func SiteURLs(ctx context.Context, acct account.Account, errs *fault.Bus) ([]str return gc.GetSiteWebURLs(), nil } -// SiteURLs returns a list of SharePoint sites IDs in the specified M365 tenant +// SiteIDs returns a list of SharePoint sites IDs in the specified M365 tenant func SiteIDs(ctx context.Context, acct account.Account, errs *fault.Bus) ([]string, error) { gc, err := connector.NewGraphConnector(ctx, graph.HTTPClient(graph.NoTimeout()), acct, connector.Sites, errs) if err != nil { diff --git a/src/pkg/services/m365/m365_test.go b/src/pkg/services/m365/m365_test.go index ecd360637..172589902 100644 --- a/src/pkg/services/m365/m365_test.go +++ b/src/pkg/services/m365/m365_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "github.com/alcionai/corso/src/internal/tester" @@ -34,9 +33,8 @@ func (suite *M365IntegrationSuite) TestUsers() { ) users, err := Users(ctx, acct, fault.New(true)) - require.NoError(t, err) - require.NotNil(t, users) - require.Greater(t, len(users), 0) + assert.NoError(t, err) + assert.NotEmpty(t, users) for _, u := range users { suite.Run("user_"+u.ID, func() { @@ -48,3 +46,25 @@ func (suite *M365IntegrationSuite) TestUsers() { }) } } + +func (suite *M365IntegrationSuite) TestSites() { + ctx, flush := tester.NewContext() + defer flush() + + var ( + t = suite.T() + acct = tester.NewM365Account(suite.T()) + ) + + sites, err := Sites(ctx, acct, fault.New(true)) + assert.NoError(t, err) + assert.NotEmpty(t, sites) + + for _, s := range sites { + suite.Run("site", func() { + t := suite.T() + assert.NotEmpty(t, s.WebURL) + assert.NotEmpty(t, s.ID) + }) + } +}