Skip to content

Commit c4a89b1

Browse files
committed
Add tests for team endpoints.
1 parent 2de3574 commit c4a89b1

File tree

1 file changed

+88
-28
lines changed

1 file changed

+88
-28
lines changed

Examples/SimpleTest/Program.cs

Lines changed: 88 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ namespace SimpleTest
1010

1111
using Dropbox.Api;
1212
using Dropbox.Api.Files;
13+
using Dropbox.Api.Team;
1314
using System.Threading;
1415

1516
partial class Program
1617
{
1718
// Add an ApiKey (from https://www.dropbox.com/developers/apps) here
1819
// private const string ApiKey = "XXXXXXXXXXXXXXX";
1920

20-
private DropboxClient client;
21-
2221
[STAThread]
2322
static int Main(string[] args)
2423
{
@@ -50,24 +49,18 @@ private async Task<int> Run()
5049
Timeout = TimeSpan.FromMinutes(20)
5150
};
5251

53-
this.client = new DropboxClient(accessToken, userAgent: "SimpleTestApp", httpClient: httpClient);
54-
5552
try
5653
{
57-
await GetCurrentAccount();
58-
59-
var path = "/DotNetApi/Help";
60-
var list = await ListFolder(path);
54+
var client = new DropboxClient(accessToken, userAgent: "SimpleTestApp", httpClient: httpClient);
55+
await RunUserTests(client);
6156

62-
var firstFile = list.Entries.FirstOrDefault(i => i.IsFile);
63-
if (firstFile != null)
64-
{
65-
await Download(path, firstFile.AsFile);
66-
}
57+
// Tests below are for Dropbox Business endpoints. To run these tests, make sure the ApiKey is for
58+
// a Dropbox Business app and you have an admin account to log in.
6759

68-
await Upload(path, "Test.txt", "This is a text file");
69-
70-
await ChunkUpload(path, "Binary");
60+
/*
61+
var client = new DropboxTeamClient(accessToken, userAgent: "SimpleTeamTestApp", httpClient: httpClient);
62+
await RunTeamTests(client);
63+
*/
7164
}
7265
catch (HttpException e)
7366
{
@@ -83,6 +76,50 @@ private async Task<int> Run()
8376
return 0;
8477
}
8578

79+
/// <summary>
80+
/// Run tests for user-level operations.
81+
/// </summary>
82+
/// <param name="client">The Dropbox client.</param>
83+
/// <returns>An asynchronous task.</returns>
84+
private async Task RunUserTests(DropboxClient client)
85+
{
86+
await GetCurrentAccount(client);
87+
88+
var path = "/DotNetApi/Help";
89+
var list = await ListFolder(client, path);
90+
91+
var firstFile = list.Entries.FirstOrDefault(i => i.IsFile);
92+
if (firstFile != null)
93+
{
94+
await Download(client, path, firstFile.AsFile);
95+
}
96+
97+
await Upload(client, path, "Test.txt", "This is a text file");
98+
99+
await ChunkUpload(client, path, "Binary");
100+
}
101+
102+
/// <summary>
103+
/// Run tests for team-level operations.
104+
/// </summary>
105+
/// <param name="client">The Dropbox client.</param>
106+
/// <returns>An asynchronous task.</returns>
107+
private async Task RunTeamTests(DropboxTeamClient client)
108+
{
109+
var members = await client.Team.MembersListAsync();
110+
111+
var member = members.Members.FirstOrDefault();
112+
113+
if (member != null)
114+
{
115+
// A team client can perform action on a team member's behalf. To do this,
116+
// just pass in team member id in to AsMember function which returns a user client.
117+
// This client will operates on this team member's Dropbox.
118+
var userClient = client.AsMember(member.Profile.TeamMemberId);
119+
await RunUserTests(userClient);
120+
}
121+
}
122+
86123
/// <summary>
87124
/// Initializes ssl certificate pinning.
88125
/// </summary>
@@ -177,10 +214,11 @@ private async Task<string> GetAccessToken()
177214
/// This demonstrates calling a simple rpc style api from the Users namespace.
178215
/// </para>
179216
/// </summary>
217+
/// <param name="client">The Dropbox client.</param>
180218
/// <returns>An asynchronous task.</returns>
181-
private async Task GetCurrentAccount()
219+
private async Task GetCurrentAccount(DropboxClient client)
182220
{
183-
var full = await this.client.Users.GetCurrentAccountAsync();
221+
var full = await client.Users.GetCurrentAccountAsync();
184222

185223
Console.WriteLine("Account id : {0}", full.AccountId);
186224
Console.WriteLine("Country : {0}", full.Country);
@@ -211,11 +249,12 @@ private async Task GetCurrentAccount()
211249
/// </summary>
212250
/// <remarks>This is a demonstrates calling an rpc style api in the Files namespace.</remarks>
213251
/// <param name="path">The path to list.</param>
252+
/// <param name="client">The Dropbox client.</param>
214253
/// <returns>The result from the ListFolderAsync call.</returns>
215-
private async Task<ListFolderResult> ListFolder(string path)
254+
private async Task<ListFolderResult> ListFolder(DropboxClient client, string path)
216255
{
217256
Console.WriteLine("--- Files ---");
218-
var list = await this.client.Files.ListFolderAsync(path);
257+
var list = await client.Files.ListFolderAsync(path);
219258

220259
// show folders then files
221260
foreach (var item in list.Entries.Where(i => i.IsFolder))
@@ -243,14 +282,15 @@ private async Task<ListFolderResult> ListFolder(string path)
243282
/// Downloads a file.
244283
/// </summary>
245284
/// <remarks>This demonstrates calling a download style api in the Files namespace.</remarks>
285+
/// <param name="client">The Dropbox client.</param>
246286
/// <param name="folder">The folder path in which the file should be found.</param>
247287
/// <param name="file">The file to download within <paramref name="folder"/>.</param>
248288
/// <returns></returns>
249-
private async Task Download(string folder, FileMetadata file)
289+
private async Task Download(DropboxClient client, string folder, FileMetadata file)
250290
{
251291
Console.WriteLine("Download file...");
252292

253-
using (var response = await this.client.Files.DownloadAsync(folder + "/" + file.Name))
293+
using (var response = await client.Files.DownloadAsync(folder + "/" + file.Name))
254294
{
255295
Console.WriteLine("Downloaded {0} Rev {1}", response.Response.Name, response.Response.Rev);
256296
Console.WriteLine("------------------------------");
@@ -262,17 +302,18 @@ private async Task Download(string folder, FileMetadata file)
262302
/// <summary>
263303
/// Uploads given content to a file in Dropbox.
264304
/// </summary>
305+
/// <param name="client">The Dropbox client.</param>
265306
/// <param name="folder">The folder to upload the file.</param>
266307
/// <param name="fileName">The name of the file.</param>
267308
/// <param name="fileContent">The file content.</param>
268309
/// <returns></returns>
269-
private async Task Upload(string folder, string fileName, string fileContent)
310+
private async Task Upload(DropboxClient client, string folder, string fileName, string fileContent)
270311
{
271312
Console.WriteLine("Upload file...");
272313

273314
using (var stream = new MemoryStream(System.Text.UTF8Encoding.UTF8.GetBytes(fileContent)))
274315
{
275-
var response = await this.client.Files.UploadAsync(folder + "/" + fileName, WriteMode.Overwrite.Instance, body: stream);
316+
var response = await client.Files.UploadAsync(folder + "/" + fileName, WriteMode.Overwrite.Instance, body: stream);
276317

277318
Console.WriteLine("Uploaded Id {0} Rev {1}", response.Id, response.Rev);
278319
}
@@ -282,10 +323,11 @@ private async Task Upload(string folder, string fileName, string fileContent)
282323
/// Uploads a big file in chunk. The is very helpful for uploading large file in slow network condition
283324
/// and also enable capability to track upload progerss.
284325
/// </summary>
326+
/// <param name="client">The Dropbox client.</param>
285327
/// <param name="folder">The folder to upload the file.</param>
286328
/// <param name="fileName">The name of the file.</param>
287329
/// <returns></returns>
288-
private async Task ChunkUpload(string folder, string fileName)
330+
private async Task ChunkUpload(DropboxClient client, string folder, string fileName)
289331
{
290332
Console.WriteLine("Chunk upload file...");
291333
// Chunk size is 128KB.
@@ -311,7 +353,7 @@ private async Task ChunkUpload(string folder, string fileName)
311353
{
312354
if (idx == 0)
313355
{
314-
var result = await this.client.Files.UploadSessionStartAsync(memStream);
356+
var result = await client.Files.UploadSessionStartAsync(memStream);
315357
sessionId = result.SessionId;
316358
}
317359

@@ -321,18 +363,36 @@ private async Task ChunkUpload(string folder, string fileName)
321363

322364
if (idx == numChunks - 1)
323365
{
324-
await this.client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(folder + "/" + fileName), memStream);
366+
await client.Files.UploadSessionFinishAsync(cursor, new CommitInfo(folder + "/" + fileName), memStream);
325367
}
326368

327369
else
328370
{
329-
await this.client.Files.UploadSessionAppendAsync(cursor, memStream);
371+
await client.Files.UploadSessionAppendAsync(cursor, memStream);
330372
}
331373
}
332374
}
333375
}
334376
}
377+
}
378+
379+
/// <summary>
380+
/// List all members in the team.
381+
/// </summary>
382+
/// <param name="client">The Dropbox team client.</param>
383+
/// <returns>The result from the MembersListAsync call.</returns>
384+
private async Task<MembersListResult> ListTeamMembers(DropboxTeamClient client)
385+
{
386+
var members = await client.Team.MembersListAsync();
387+
388+
foreach (var member in members.Members)
389+
{
390+
Console.WriteLine("Member id : {0}", member.Profile.TeamMemberId);
391+
Console.WriteLine("Name : {0}", member.Profile.Name);
392+
Console.WriteLine("Email : {0}", member.Profile.Email);
393+
}
335394

395+
return members;
336396
}
337397
}
338398
}

0 commit comments

Comments
 (0)