//[TestMethod]
        public void TestProxyInitializesCommunicationChannel()
        {
            // Configure the channel mock
            CommunicationChannelMock channelMock = new CommunicationChannelMock();
            channelMock.AddResponse("https://auth.services.mozilla.com/user/1.0/" + TestUserName + "/node/weave",
                RestOperation.Get, TestWeaveNodeAddress);
            channelMock.AddResponse(TestWeaveNodeAddress + "/1.1/" + TestUserName + "/storage/meta/global",
                RestOperation.Get, new GlobalMetaData() { StorageVersion = 5, SyncId = "dfsf" });

            WeaveProxy proxy = new WeaveProxy(channelMock, TestUserName, TestPassword, TestPassphrase);

            Assert.IsTrue(channelMock.IsInitialized);
        }
Example #2
0
        /// <summary>
        /// Method performing the actual work of the operation RefreshWeaveDataAsync.
        /// </summary>
        /// <param name="asyncOperation">An <see cref="AsyncOperation"/> object.</param>
        private void RefreshWeaveDataWorker(AsyncOperation asyncOperation)
        {
            try
            {
                if (string.IsNullOrEmpty(storage.UserName))
                {
                    throw new Exception(AppResources.InvalidUserName);
                }

                if (string.IsNullOrEmpty(storage.Password))
                {
                    throw new Exception(AppResources.InvalidPassword);
                }

                if (string.IsNullOrEmpty(storage.Passphrase))
                {
                    throw new Exception(AppResources.InvalidSyncKey);
                }

                if (!storage.UseDefaultServer && string.IsNullOrEmpty(storage.ServerAddress))
                {
                    throw new Exception(AppResources.InvalidCustomServerAddress);
                }

                if (!NetworkInterface.GetIsNetworkAvailable())
                {
                    throw new Exception(AppResources.NoNetwork);
                }

                RefreshWeaveDataProgressUpdateMethod(AppResources.ConnectingToWeaveServer, asyncOperation);

                WeaveProxy proxy;

                if (storage.UseDefaultServer)
                    proxy = new WeaveProxy(new HttpCommunicationChannel(), storage.UserName, storage.Password, storage.Passphrase);
                else
                    proxy = new WeaveProxy(new HttpCommunicationChannel(), storage.UserName, storage.Password, storage.Passphrase, storage.ServerAddress);

                RefreshWeaveDataProgressUpdateMethod(AppResources.UpdatingProfile, asyncOperation);

                // Download collection information
                Dictionary<string, int> collectionCounts = proxy.GetCollectionCounts();
                Dictionary<string, int> collectionUsage = proxy.GetCollectionUsage();

                // Download clients
                IEnumerable<WeaveBasicObject> encryptedClients = proxy.GetCollection("clients", SortOrder.Index);
                IEnumerable<WeaveClient> weaveClients = proxy.DecryptPayload<WeaveClient>(encryptedClients);

                // Update the storage
                this.storage.Profile = new ProfileInformation(proxy.WeaveNode, proxy.ServerStorageVersion,
                    CombineCollectionInformation(collectionCounts, collectionUsage), ConvertWeaveClients(weaveClients));
                this.storage.PersistProfile();

                if (this.storage.SynchronizeBookmarks)
                {
                    RefreshWeaveDataProgressUpdateMethod(AppResources.UpdatingBookmarks, asyncOperation);

                    // Download bookmarks
                    IEnumerable<WeaveBasicObject> encryptedWeaveBookmarks = proxy.GetCollection("bookmarks", SortOrder.Index);
                    if (encryptedWeaveBookmarks != null)
                    {
                        IEnumerable<WeaveBookmark> weaveBookmarks = proxy.DecryptPayload<WeaveBookmark>(encryptedWeaveBookmarks);

                        Directory rootBookmarksDirectory = BookmarksStructureBuilder.Build(weaveBookmarks);

                        // Update the storage
                        this.storage.Bookmarks = rootBookmarksDirectory;
                        this.storage.PersistBookmarks();
                    }
                }

                if (this.storage.SynchronizeHistory)
                {
                    RefreshWeaveDataProgressUpdateMethod(AppResources.UpdatingHistory, asyncOperation);

                    // Download history
                    IEnumerable<WeaveBasicObject> encryptedWeaveHistory = proxy.GetCollection("history", SortOrder.Index);
                    if (encryptedWeaveHistory != null)
                    {
                        IEnumerable<WeaveHistoryItem> weaveHistory = proxy.DecryptPayload<WeaveHistoryItem>(encryptedWeaveHistory).ToList();

                        Directory rootHistoryDirectory = HistoryStructureBuilder.Build(weaveHistory);
                        this.storage.PersistBookmarks();

                        // Update the storage
                        this.storage.History = rootHistoryDirectory;
                        this.storage.PersistHistory();
                    }
                }

                if (this.storage.SynchronizeTabs)
                {
                    RefreshWeaveDataProgressUpdateMethod(AppResources.UpdatingTabs, asyncOperation);

                    // Download opened tabs
                    IEnumerable<WeaveBasicObject> encryptedWeaveTabs = proxy.GetCollection("tabs", SortOrder.Index);
                    if (encryptedWeaveTabs != null)
                    {
                        IEnumerable<WeaveClientSession> weaveClientSessions = proxy.DecryptPayload<WeaveClientSession>(encryptedWeaveTabs);

                        // Update the storage
                        this.storage.Tabs = OpenTabsStructureBuilder.Build(weaveClientSessions);
                        this.storage.PersistTabs();
                    }
                }

                RefreshWeaveDataCompletionMethod(null, false, asyncOperation);
            }
            catch (Exception ex)
            {
                RefreshWeaveDataCompletionMethod(ex, false, asyncOperation);
            }
        }