From d7b24f37ab77d8f1dc87808b5901559613a2c01f Mon Sep 17 00:00:00 2001 From: Abhishek Pandey Date: Tue, 17 Oct 2023 14:36:35 -0700 Subject: [PATCH] Add tests which were rolled back with 4456 (#4459) Recent PR #4456 rolled back some of newer test additions. This PR reintroduces these tests. --- #### Does this PR need a docs update or release note? - [ ] :white_check_mark: Yes, it's included - [ ] :clock1: Yes, but in a later PR - [x] :no_entry: No #### Type of change - [ ] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [x] :robot: Supportability/Tests - [ ] :computer: CI/Deployment - [ ] :broom: Tech Debt/Cleanup #### Issue(s) * # #### Test Plan - [x] :muscle: Manual - [ ] :zap: Unit test - [ ] :green_heart: E2E --- .../m365/collection/drive/item_test.go | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/src/internal/m365/collection/drive/item_test.go b/src/internal/m365/collection/drive/item_test.go index b63aa1c02d..7fc9e065c2 100644 --- a/src/internal/m365/collection/drive/item_test.go +++ b/src/internal/m365/collection/drive/item_test.go @@ -16,11 +16,13 @@ import ( "github.com/alcionai/corso/src/internal/common/dttm" "github.com/alcionai/corso/src/internal/common/ptr" + "github.com/alcionai/corso/src/internal/common/str" "github.com/alcionai/corso/src/internal/m365/graph" "github.com/alcionai/corso/src/internal/tester" "github.com/alcionai/corso/src/internal/tester/tconfig" "github.com/alcionai/corso/src/pkg/control" "github.com/alcionai/corso/src/pkg/control/testdata" + "github.com/alcionai/corso/src/pkg/selectors" "github.com/alcionai/corso/src/pkg/services/m365/api" ) @@ -61,6 +63,106 @@ func (suite *ItemIntegrationSuite) SetupSuite() { suite.userDriveID = ptr.Val(odDrives[0].GetId()) } +func getOneDriveItem( + ctx context.Context, + t *testing.T, + ac api.Client, + driveID string, +) models.DriveItemable { + pager := ac.Drives().EnumerateDriveItemsDelta( + ctx, + driveID, + "", + api.CallConfig{ + Select: api.DefaultDriveItemProps(), + }) + + // Get a file item + for page, _, done := pager.NextPage(); !done; page, _, done = pager.NextPage() { + for _, item := range page { + if item.GetFile() != nil { + return item + } + } + } + + return nil +} + +// TestItemReader is an integration test that makes a few assumptions +// about the test environment +// 1) It assumes the test user has a drive +// 2) It assumes the drive has a file it can use to test `driveItemReader` +// The test checks these in below +func (suite *ItemIntegrationSuite) TestItemReader_oneDrive() { + t := suite.T() + + ctx, flush := tester.NewContext(t) + defer flush() + + sc := selectors. + NewOneDriveBackup([]string{suite.user}). + AllData()[0] + + driveItem := getOneDriveItem(ctx, t, suite.service.ac, suite.userDriveID) + // Test Requirement 2: Need a file + require.NotEmpty( + t, + driveItem, + "no file item found for user %s drive %s", + suite.user, + suite.userDriveID) + + bh := &userDriveBackupHandler{ + baseUserDriveHandler: baseUserDriveHandler{ + ac: suite.service.ac.Drives(), + }, + userID: suite.user, + scope: sc, + } + + // Read data for the file + itemData, err := downloadItem(ctx, bh, driveItem) + require.NoError(t, err, clues.ToCore(err)) + + size, err := io.Copy(io.Discard, itemData) + require.NoError(t, err, clues.ToCore(err)) + require.NotZero(t, size) +} + +// In prod we consider any errors in isURLExpired as non-fatal and carry on +// with the download. This is a regression test to make sure we keep track +// of any graph changes to the download url scheme, including how graph +// embeds the jwt token. +func (suite *ItemIntegrationSuite) TestIsURLExpired() { + t := suite.T() + + ctx, flush := tester.NewContext(t) + defer flush() + + driveItem := getOneDriveItem(ctx, t, suite.service.ac, suite.userDriveID) + require.NotEmpty( + t, + driveItem, + "no file item found for user %s drive %s", + suite.user, + suite.userDriveID) + + var url string + + for _, key := range downloadURLKeys { + if v, err := str.AnyValueToString(key, driveItem.GetAdditionalData()); err == nil { + url = v + break + } + } + + expired, err := isURLExpired(ctx, url) + require.NoError(t, err, clues.ToCore(err)) + + require.False(t, expired) +} + // TestItemWriter is an integration test for uploading data to OneDrive // It creates a new folder with a new item and writes data to it func (suite *ItemIntegrationSuite) TestItemWriter() {