예제 #1
0
        public static void ClassInitialize(TestContext testContext)
        {
            if (!GlobalTestSettings.RunNetworkTests)
            {
                return;
            }

            string tokenFilePath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
                "SyncProTesting",
                "OneDriveTestingToken.json");

            if (!File.Exists(tokenFilePath))
            {
                string  tokenHelperPath = "OneDriveTokenHelper.exe";
                Process p = Process.Start(tokenHelperPath, "/getToken /path \"" + tokenFilePath + "\"");
                Pre.Assert(p != null, "p != null");
                p.WaitForExit();

                if (p.ExitCode != 1)
                {
                    throw new Exception("Failed to get token using OneDriveTokenHelper");
                }

                //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 (OneDriveClient client = new OneDriveClient(token))
            {
                client.TokenRefreshed += (sender, args) =>
                {
                    // The token was refreshed, so save a protected copy of the token to the token file.
                    token = args.NewToken;
                    token.SaveProtectedToken(tokenFilePath);
                };

                client.GetUserProfileAsync().Wait();
            }

            classCurrentToken = token;
        }