add drive info to site get by id (#4197)

extends the site get-by-id call to include drive info. Useful for detecting whether the site is owned by
a user, or a group or team.

---

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

- [x]  No

#### Type of change

- [x] 🌻 Feature

#### Test Plan

- [x]  Unit test
- [x] 💚 E2E
This commit is contained in:
Keepers 2023-09-07 16:13:46 -06:00 committed by GitHub
parent bf02f57527
commit f94f5bbc76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 16 deletions

View File

@ -10,11 +10,11 @@ import (
"github.com/alcionai/clues"
msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/microsoftgraph/msgraph-sdk-go/sites"
"github.com/pkg/errors"
"github.com/alcionai/corso/src/internal/common/ptr"
"github.com/alcionai/corso/src/internal/m365/graph"
"github.com/alcionai/corso/src/internal/m365/graph/betasdk/sites"
"github.com/alcionai/corso/src/pkg/fault"
)
@ -36,11 +36,17 @@ type Sites struct {
// ---------------------------------------------------------------------------
func (c Sites) GetRoot(ctx context.Context) (models.Siteable, error) {
options := &sites.SiteItemRequestBuilderGetRequestConfiguration{
QueryParameters: &sites.SiteItemRequestBuilderGetQueryParameters{
Expand: []string{"drive"},
},
}
resp, err := c.Stable.
Client().
Sites().
BySiteIdString("root").
Get(ctx, nil)
Get(ctx, options)
if err != nil {
return nil, graph.Wrap(ctx, err, "getting root site")
}
@ -103,7 +109,7 @@ const uuidRE = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9
// deadbeef-0000-0000-0000-000000000000,beefdead-0000-0000-0000-000000000000
var siteIDRE = regexp.MustCompile(`(.+,)?` + uuidRE + "," + uuidRE)
const webURLGetTemplate = "https://graph.microsoft.com/v1.0/sites/%s:/%s"
const sitesWebURLGetTemplate = "https://graph.microsoft.com/v1.0/sites/%s:/%s?$expand=drive"
// GetByID looks up the site matching the given identifier. The identifier can be either a
// canonical site id or a webURL. Assumes the webURL is complete and well formed;
@ -117,11 +123,17 @@ func (c Sites) GetByID(ctx context.Context, identifier string) (models.Siteable,
ctx = clues.Add(ctx, "given_site_id", identifier)
if siteIDRE.MatchString(identifier) {
options := &sites.SiteItemRequestBuilderGetRequestConfiguration{
QueryParameters: &sites.SiteItemRequestBuilderGetQueryParameters{
Expand: []string{"drive"},
},
}
resp, err = c.Stable.
Client().
Sites().
BySiteIdString(identifier).
Get(ctx, nil)
Get(ctx, options)
if err != nil {
err := graph.Wrap(ctx, err, "getting site by id")
@ -153,7 +165,7 @@ func (c Sites) GetByID(ctx context.Context, identifier string) (models.Siteable,
// don't construct a path with double leading slashes
path := strings.TrimPrefix(u.Path, "/")
rawURL := fmt.Sprintf(webURLGetTemplate, u.Host, path)
rawURL := fmt.Sprintf(sitesWebURLGetTemplate, u.Host, path)
resp, err = sites.
NewItemSitesSiteItemRequestBuilder(rawURL, c.Stable.Adapter()).

View File

@ -158,62 +158,70 @@ func (suite *SitesIntgSuite) TestSites_GetByID() {
table := []struct {
name string
id string
expectErr func(*testing.T, error)
expectErr func(*testing.T, error) bool
}{
{
name: "3 part id",
id: siteID,
expectErr: func(t *testing.T, err error) {
expectErr: func(t *testing.T, err error) bool {
assert.NoError(t, err, clues.ToCore(err))
return false
},
},
{
name: "2 part id",
id: uuids,
expectErr: func(t *testing.T, err error) {
expectErr: func(t *testing.T, err error) bool {
assert.NoError(t, err, clues.ToCore(err))
return false
},
},
{
name: "malformed id",
id: uuid.NewString(),
expectErr: func(t *testing.T, err error) {
expectErr: func(t *testing.T, err error) bool {
assert.Error(t, err, clues.ToCore(err))
return true
},
},
{
name: "random id",
id: uuid.NewString() + "," + uuid.NewString(),
expectErr: func(t *testing.T, err error) {
expectErr: func(t *testing.T, err error) bool {
assert.ErrorIs(t, err, graph.ErrResourceOwnerNotFound, clues.ToCore(err))
return true
},
},
{
name: "url",
id: siteURL,
expectErr: func(t *testing.T, err error) {
expectErr: func(t *testing.T, err error) bool {
assert.NoError(t, err, clues.ToCore(err))
return false
},
},
{
name: "malformed url",
id: "barunihlda",
expectErr: func(t *testing.T, err error) {
expectErr: func(t *testing.T, err error) bool {
assert.Error(t, err, clues.ToCore(err))
return true
},
},
{
name: "well formed url, invalid hostname",
id: "https://test/sites/testing",
expectErr: func(t *testing.T, err error) {
expectErr: func(t *testing.T, err error) bool {
assert.Error(t, err, clues.ToCore(err))
return true
},
},
{
name: "well formed url, no sites match",
id: modifiedSiteURL,
expectErr: func(t *testing.T, err error) {
expectErr: func(t *testing.T, err error) bool {
assert.ErrorIs(t, err, graph.ErrResourceOwnerNotFound, clues.ToCore(err))
return true
},
},
}
@ -224,8 +232,16 @@ func (suite *SitesIntgSuite) TestSites_GetByID() {
ctx, flush := tester.NewContext(t)
defer flush()
_, err := sitesAPI.GetByID(ctx, test.id)
test.expectErr(t, err)
site, err := sitesAPI.GetByID(ctx, test.id)
expectedErr := test.expectErr(t, err)
if expectedErr {
return
}
require.NotEmpty(t, ptr.Val(site.GetId()), "must have an id")
require.NotNil(t, site.GetDrive(), "must have drive info")
require.NotNil(t, site.GetDrive().GetOwner(), "must have drive owner info")
})
}
}
@ -240,4 +256,6 @@ func (suite *SitesIntgSuite) TestGetRoot() {
require.NoError(t, err)
require.NotNil(t, result, "must find the root site")
require.NotEmpty(t, ptr.Val(result.GetId()), "must have an id")
require.NotNil(t, result.GetDrive(), "must have drive info")
require.NotNil(t, result.GetDrive().GetOwner(), "must have drive owner info")
}