Add a helper function that returns site ID and site URL in a Site struct (#2778)

This PR adds a helper function that returns a newly introduced `Site` struct that has a site ID and site webURL for a given m365 tenant.

---

#### Does this PR need a docs update or release note?
- [X]  No

#### Type of change

<!--- Please check the type of change your PR introduces: --->
- [X] 🌻 Feature

#### Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
* #2777 

#### Test Plan

<!-- How will this be tested prior to merging.-->
- [X]  Unit test

```
--- PASS: TestM365IntegrationSuite (2.49s)
    --- PASS: TestM365IntegrationSuite/TestSites (1.82s)
        --- PASS: TestM365IntegrationSuite/TestSites/site (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#01 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#02 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#03 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#04 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#05 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#06 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#07 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#08 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#09 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#10 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#11 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#12 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#13 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#14 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#15 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#16 (0.00s)
        --- PASS: TestM365IntegrationSuite/TestSites/site#17 (0.00s)
```
This commit is contained in:
Aarti Jivrajani 2023-03-13 18:00:11 -07:00 committed by GitHub
parent ed6f1af3ba
commit 120eb0d256
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 5 deletions

View File

@ -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: <site collection hostname>.<site collection unique id>.<site unique id>
// 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 {

View File

@ -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)
})
}
}