Skip to content
This repository was archived by the owner on Oct 12, 2024. It is now read-only.

Commit 4582071

Browse files
authored
Merge pull request #31 from sanjuktaghosh7/drive-v3-teamDrive-ut
Drive-v3 TeamDriveSnippet UnitTest
2 parents cd6db2b + b29cae7 commit 4582071

File tree

6 files changed

+198
-1
lines changed

6 files changed

+198
-1
lines changed

drive/snippets/drive_v3/DriveV3Snippets.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Microsoft Visual Studio Solution File, Format Version 12.00
33
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DriveV3Snippets", "DriveV3Snippets\DriveV3Snippets.csproj", "{CADC79A2-203D-43AB-8322-4CEA3C25946E}"
44
EndProject
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DriveV3SnippetsTest", "DriveV3SnippetsTest\DriveV3SnippetsTest.csproj", "{DBA71DCB-2607-40D8-852F-B269A7A672B6}"
6+
EndProject
57
Global
68
GlobalSection(SolutionConfigurationPlatforms) = preSolution
79
Debug|Any CPU = Debug|Any CPU
@@ -12,5 +14,9 @@ Global
1214
{CADC79A2-203D-43AB-8322-4CEA3C25946E}.Debug|Any CPU.Build.0 = Debug|Any CPU
1315
{CADC79A2-203D-43AB-8322-4CEA3C25946E}.Release|Any CPU.ActiveCfg = Release|Any CPU
1416
{CADC79A2-203D-43AB-8322-4CEA3C25946E}.Release|Any CPU.Build.0 = Release|Any CPU
17+
{DBA71DCB-2607-40D8-852F-B269A7A672B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
18+
{DBA71DCB-2607-40D8-852F-B269A7A672B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
19+
{DBA71DCB-2607-40D8-852F-B269A7A672B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
20+
{DBA71DCB-2607-40D8-852F-B269A7A672B6}.Release|Any CPU.Build.0 = Release|Any CPU
1521
EndGlobalSection
1622
EndGlobal

drive/snippets/drive_v3/DriveV3Snippets/DriveV3Snippets.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
54
<TargetFramework>net6.0</TargetFramework>
65
<ImplicitUsings>enable</ImplicitUsings>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<OutputType>Library</OutputType>
8+
<IsPackable>false</IsPackable>
79
</PropertyGroup>
810

911
<ItemGroup>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using Google.Apis.Auth.OAuth2;
2+
using Google.Apis.Drive.v3;
3+
using Google.Apis.Services;
4+
using NUnit.Framework;
5+
6+
namespace DriveV3SnippetsTest
7+
{
8+
public abstract class BaseTest
9+
{
10+
protected DriveService service;
11+
12+
public DriveService BuildService()
13+
{
14+
/* Load pre-authorized user credentials from the environment.
15+
TODO(developer) - See https://developers.google.com/identity for
16+
guides on implementing OAuth2 for your application. */
17+
GoogleCredential credential = GoogleCredential.GetApplicationDefault();
18+
var scopes = new[]
19+
{
20+
DriveService.Scope.Drive,
21+
DriveService.Scope.DriveAppdata
22+
};
23+
credential = credential.CreateScoped(scopes);
24+
25+
// Create Drive API service.
26+
var service = new DriveService(new BaseClientService.Initializer
27+
{
28+
HttpClientInitializer = credential,
29+
ApplicationName = "Drive API Snippets"
30+
});
31+
32+
return service;
33+
}
34+
35+
[SetUp]
36+
public void Setup()
37+
{
38+
service = BuildService();
39+
}
40+
41+
protected void DeleteFileOnCleanup(string id)
42+
{
43+
service.Files.Delete(id).Execute();
44+
}
45+
46+
protected string CreateTestDocument(string filePath)
47+
{
48+
var fileMetadata = new Google.Apis.Drive.v3.Data.File();
49+
fileMetadata.Name = "Test Document";
50+
fileMetadata.MimeType = "application/vnd.google-apps.document";
51+
using (var stream = new FileStream(filePath,
52+
FileMode.Open))
53+
{
54+
var request = service.Files.Create(
55+
fileMetadata, stream, "text/plain");
56+
request.Fields = "id, mimeType";
57+
request.Upload();
58+
var file = request.ResponseBody;
59+
if (file != null)
60+
{
61+
return file.Id;
62+
}
63+
else
64+
{
65+
return null;
66+
}
67+
}
68+
}
69+
70+
protected string CreateTestBlob(string filePath)
71+
{
72+
var fileMetadata = new Google.Apis.Drive.v3.Data.File();
73+
fileMetadata.Name = "photo.jpg";
74+
using (var stream = new FileStream(filePath, FileMode.Open))
75+
{
76+
var request = service.Files.Create(
77+
fileMetadata, stream, "image/jpeg");
78+
request.Fields = "id";
79+
request.Upload();
80+
var file = request.ResponseBody;
81+
return file.Id;
82+
}
83+
}
84+
}
85+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using DriveV3Snippets;
16+
using NUnit.Framework;
17+
18+
namespace DriveV3SnippetsTest
19+
{
20+
// Unit testcase for drive v3 create team drive snippet
21+
[TestFixture]
22+
public class CreateTeamDriveTest : BaseTest
23+
{
24+
[Test]
25+
public void TestCreateTeamDrive()
26+
{
27+
var id = CreateTeamDrive.DriveCreateTeamDrive();
28+
Assert.IsNotNull(id);
29+
service.Teamdrives.Delete(id).Execute();
30+
}
31+
}
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<OutputType>Library</OutputType>
6+
<IsPackable>false</IsPackable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<TestProjectType>UnitTest</TestProjectType>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Google.Apis.Drive.v3" Version="1.57.0.2663" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
14+
<PackageReference Include="NUnit" Version="3.13.2" />
15+
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
16+
<PackageReference Include="coverlet.collector" Version="3.1.0" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\DriveV3Snippets\DriveV3Snippets.csproj" />
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using DriveV3Snippets;
16+
using NUnit.Framework;
17+
18+
namespace DriveV3SnippetsTest
19+
{
20+
// Unit testcase for drive v3 recover team drive snippet
21+
[TestFixture]
22+
public class RecoverTeamDriveTest : BaseTest
23+
{
24+
[Test]
25+
public void TestRecoverTeamDrives()
26+
{
27+
var id = CreateOrphanedTeamDrive();
28+
var results = RecoverTeamDrives.DriveRecoverTeamDrives("[email protected]");
29+
Assert.AreNotEqual(0, results.Count);
30+
service.Teamdrives.Delete(id).Execute();
31+
}
32+
33+
private string CreateOrphanedTeamDrive()
34+
{
35+
var teamDriveId = CreateTeamDrive.DriveCreateTeamDrive();
36+
var listRequest = service.Permissions.List(teamDriveId);
37+
listRequest.SupportsTeamDrives = true;
38+
var response = listRequest.Execute();
39+
40+
foreach (var permission in response.Permissions)
41+
{
42+
var deleteRequest = service.Permissions.Delete(teamDriveId, permission.Id);
43+
deleteRequest.SupportsTeamDrives = true;
44+
deleteRequest.Execute();
45+
}
46+
return teamDriveId;
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)