forked from HighEncryption/SyncPro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleDriveTests.cs
More file actions
212 lines (162 loc) · 8.51 KB
/
Copy pathGoogleDriveTests.cs
File metadata and controls
212 lines (162 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
namespace SyncPro.UnitTests
{
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using SyncPro.Adapters.GoogleDrive;
using SyncPro.Adapters.GoogleDrive.DataModel;
using SyncPro.OAuth;
using SyncPro.Runtime;
[TestClass]
public class GoogleDriveTests : AdapterTestsBase<GoogleDriveAdapter>
{
private static TokenResponse classCurrentToken;
[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
if (!GlobalTestSettings.RunNetworkTests)
{
return;
}
string tokenFilePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
"SyncProTesting",
"GoogleDriveTestingToken.json");
if (!File.Exists(tokenFilePath))
{
throw new FileNotFoundException("Token file was not present at path " + tokenFilePath);
}
string tokenContent = File.ReadAllText(tokenFilePath);
var token = JsonConvert.DeserializeObject<TokenResponse>(tokenContent);
if (!token.IsEncrypted)
{
// The token file is NOT encrypted. Immediately encrypt and save the file back to disk
// for security before using it.
token.Protect();
tokenContent = JsonConvert.SerializeObject(token, Formatting.Indented);
File.WriteAllText(tokenFilePath, tokenContent);
}
// The token file on disk is encrypted. Decrypt the values for in-memory use.
token.Unprotect();
using (GoogleDriveClient client = new GoogleDriveClient(token))
{
client.TokenRefreshed += (sender, args) =>
{
//token = new TokenResponseEx(args.NewToken);
//token.Protect();
//tokenContent = JsonConvert.SerializeObject(token, Formatting.Indented);
//File.WriteAllText(tokenFilePath, tokenContent);
//token.Unprotect();
// The token was refreshed, so save a protected copy of the token to the token file.
token = args.NewToken;
token.SaveProtectedToken(tokenFilePath);
};
client.GetUserInformation().Wait();
}
//testContext.Properties["CurrentToken"] = token;
GoogleDriveTests.classCurrentToken = token;
}
[TestMethod]
public void BasicSyncLocalToGoogleDrive()
{
if (!GlobalTestSettings.RunNetworkTests)
{
Assert.Inconclusive(GlobalTestSettings.NetworkTestsDisabledMessage);
}
TokenResponse currentToken = this.GetCurrentToken();
using (GoogleDriveClient client = new GoogleDriveClient(currentToken))
{
var res12 = client.GetItemById("0B781VIRHEt3xeDM2UkVZcDFaUTQ").Result;
//var info = client.GetUserInformation().Result;
var res = client.GetChildItems(new GoogleDriveAdapterItem(new Item() {Id = "0B781VIRHEt3xNGtvMUppbnpPRVk" }, null, null)).Result;
}
// Assert.Inconclusive("TODO");
//string testRootPath = Path.Combine(this.TestContext.TestLogsDir, this.TestContext.TestName);
//Directory.CreateDirectory(testRootPath);
//string syncSourcePath = Path.Combine(testRootPath, "Source");
//Directory.CreateDirectory(syncSourcePath);
//// Create temp files/folders
//List<string> syncFileList = new List<string>
// {
// TestHelper.CreateDirectory(syncSourcePath, "dir1"),
// TestHelper.CreateFile(syncSourcePath, "dir1\\file1.txt"),
// TestHelper.CreateFile(syncSourcePath, "dir1\\file2.txt"),
// TestHelper.CreateFile(syncSourcePath, "dir1\\file3.txt"),
// TestHelper.CreateDirectory(syncSourcePath, "dir2"),
// TestHelper.CreateFile(syncSourcePath, "dir2\\file1.txt"),
// TestHelper.CreateFile(syncSourcePath, "dir2\\file2.txt"),
// TestHelper.CreateFile(syncSourcePath, "dir2\\file3.txt")
// };
//TokenResponse currentToken = this.GetCurrentToken();
//Guid remoteTestFolderName = Guid.NewGuid();
//Item remoteTestFolder = CreateOneDriveTestDirectory(currentToken, remoteTestFolderName.ToString("D")).Result;
//SyncRelationship newRelationship = this.SetupRelationship(testRootPath, syncSourcePath, remoteTestFolder);
//ManualResetEvent evt = new ManualResetEvent(false);
//SyncJob run1 = new SyncJob(newRelationship);
//run1.SyncFinished += (sender, args) => { evt.Set(); };
//run1.Start();
//evt.WaitOne();
//Assert.IsTrue(run1.HasFinished);
//Assert.AreEqual(syncFileList.Count, run1.AnalyzeResult.EntryResults.Count);
//OneDriveAdapter oneDriveAdapter =
// newRelationship.Adapters.First(a => !a.Configuration.IsOriginator) as OneDriveAdapter;
//OneDriveClient client = new OneDriveClient(this.GetCurrentToken());
//foreach (string syncFile in syncFileList.Where(f => f.EndsWith(".txt")))
//{
// string localPath = Path.Combine(syncSourcePath, syncFile);
// using (var sha1 = new SHA1Managed())
// {
// byte[] content = File.ReadAllBytes(localPath);
// byte[] localFileHash = sha1.ComputeHash(content);
// var entryResult = run1.AnalyzeResult.EntryResults.FirstOrDefault(
// r => r.Entry.GetRelativePath(newRelationship, "\\") == syncFile);
// Assert.IsNotNull(entryResult);
// byte[] databaseHash = entryResult.Entry.Sha1Hash;
// Assert.AreEqual(
// TestHelper.HashToHex(localFileHash),
// TestHelper.HashToHex(databaseHash),
// "Local file hash does not match database hash.");
// Pre.Assert(oneDriveAdapter != null, "oneDriveAdapter != null");
// var adapterEntry =
// entryResult.Entry.AdapterEntries.First(e => e.AdapterId == oneDriveAdapter.Configuration.Id);
// string itemId = OneDriveAdapter.UniqueIdToItemId(adapterEntry.AdapterEntryId);
// var item = client.GetItemByItemIdAsync(itemId);
// var oneDriveHash = "0x" + item.Result.File.Hashes.Sha1Hash;
// Assert.AreEqual(
// TestHelper.HashToHex(localFileHash),
// oneDriveHash,
// "Local file hash does not match OneDrive hash.");
// }
//}
}
protected override GoogleDriveAdapter CreateSourceAdapter(SyncRelationship newRelationship, string testMethodName)
{
TokenResponse currentToken = this.GetCurrentToken();
GoogleDriveAdapter sourceAdapter = new GoogleDriveAdapter(newRelationship)
{
CurrentToken = currentToken,
};
if (testMethodName == "BasicSyncDownloadOnly")
{
sourceAdapter.TargetItemId = "0B781VIRHEt3xeGpJN1FxNGVpb28";
}
sourceAdapter.Configuration.IsOriginator = true;
sourceAdapter.InitializeClient().Wait();
return sourceAdapter;
}
protected override GoogleDriveAdapter CreateDestinationAdapter(SyncRelationship newRelationship, string testMethodName)
{
throw new NotImplementedException();
}
private TokenResponse GetCurrentToken()
{
//TokenResponse currentToken = this.TestContext.Properties["CurrentToken"] as TokenResponse;
if (classCurrentToken == null)
{
Assert.Inconclusive("Token not initialized.");
}
return classCurrentToken;
}
}
}