private async Task <WikiSite> CreateWikiSiteAsync(WikiClientBase wikiClient, string url)
        {
            var options = new SiteOptions(url)
            {
                AccountAssertion = AccountAssertionBehavior.AssertAll
            };
            var site = await WikiSite.CreateAsync(wikiClient, options);

            site.Logger = new TestOutputLogger(Output);
            if (sitesNeedsLogin.Contains(url))
            {
                await CredentialManager.LoginAsync(site);
            }
            return(site);
        }
Beispiel #2
0
        public async Task LoginWikiaTest_1()
        {
            var site = await CreateIsolatedWikiSiteAsync(Utility.EntryPointWikiaTest);

            await CredentialManager.LoginAsync(site);

            Assert.True(site.AccountInfo.IsUser);
            Assert.False(site.AccountInfo.IsAnonymous);
            Output.WriteLine($"{site.AccountInfo.Name} has logged into {site}");
            await site.LogoutAsync();

            Assert.False(site.AccountInfo.IsUser);
            Assert.True(site.AccountInfo.IsAnonymous);
            Output.WriteLine($"{site.AccountInfo.Name} has logged out.");
        }
Beispiel #3
0
        public async Task LoginPrivateWikiTest()
        {
            if (string.IsNullOrEmpty(CredentialManager.PrivateWikiTestsEntryPointUrl))
            {
                Utility.Inconclusive("The test needs CredentialManager.PrivateWikiTestsEntryPointUrl to be set.");
            }
            var client = CreateWikiClient();
            // Load cookies, if any. Here we just create a client from scratch.
            var site = await WikiSite.CreateAsync(client,
                                                  new SiteOptions(CredentialManager.PrivateWikiTestsEntryPointUrl)
            {
                ExplicitInfoRefresh = true
            });

            bool needsLogin;

            try
            {
                // It's better to get user (rather than site) info here.
                await site.RefreshAccountInfoAsync();

                // If the attempt is succcessful, it means we should have logged in.
                // After all, it's a private wiki, where anonymous users shouldn't have
                // access to reading the wiki.
                needsLogin = !site.AccountInfo.IsUser;
                // If needsLogin evaluates to true here... Well, you'd better
                // check if your private wiki is private enough.
                // Nonetheless, the code still works XD
            }
            catch (UnauthorizedOperationException)
            {
                // Cannot read user info. We must haven't logged in.
                needsLogin = true;
            }
            if (needsLogin)
            {
                // Login if needed.
                await CredentialManager.LoginAsync(site);

                Debug.Assert(site.AccountInfo.IsUser);
            }
            // Login succeeded. We should initialize site information.
            await site.RefreshSiteInfoAsync();

            // Now we can do something.
            ShallowTrace(site);
            await site.LogoutAsync();
        }
Beispiel #4
0
        public async Task AccountAssertionTest2()
        {
            // This method will militate the Site instance…
            var site = await CreateIsolatedWikiSiteAsync(EntryPointWikipediaTest2);

            site.AccountAssertionFailureHandler = new MyAccountAssertionFailureHandler(async s =>
            {
                await CredentialManager.LoginAsync(site);
                return(true);
            });
            Assert.False(site.AccountInfo.IsUser, "You should have not logged in… Wierd.");
            // Make believe that we're bots…
            typeof(AccountInfo).GetRuntimeProperty("Groups").SetValue(site.AccountInfo, new[] { "*", "user", "bot" });
            Assert.True(site.AccountInfo.IsUser, "Cannot militate user information.");
            // Send a request…
            var message = await site.GetMessageAsync("edit");

            Output.WriteLine("Message(edit) = " + message);
        }
Beispiel #5
0
        public async Task InterlacingLoginLogoutTest(string endpointUrl)
        {
            // The two sites belong to different WikiClient instances.
            var site1 = await CreateIsolatedWikiSiteAsync(endpointUrl);

            var site2 = await CreateIsolatedWikiSiteAsync(endpointUrl);

            await CredentialManager.LoginAsync(site1);

            await CredentialManager.LoginAsync(site2);

            await site2.LogoutAsync();

            await site1.RefreshAccountInfoAsync();

            // This is a known issue of MediaWiki.
            // MediaWiki Phabricator Task T51890: Logging out on a different device logs me out everywhere else
            Assert.False(site1.AccountInfo.IsUser,
                         "T51890 seems have been resolved. If this test continue to fail, please re-open the issue: https://github.com/CXuesong/WikiClientLibrary/issues/11 .");
        }