From 710ff72b20bb4bb7721cc2c5680edb6df562e629 Mon Sep 17 00:00:00 2001 From: forest93 Date: Fri, 11 Oct 2024 00:26:54 +0800 Subject: [PATCH] Fix breaking UT cases. --- UnitTestProject1/Tests/GeneratorTests1.cs | 6 +++--- UnitTestProject1/Tests/PageTests.cs | 2 +- UnitTestProject1/Tests/SiteTokenTests.cs | 12 ++++++------ UnitTestProject1/Tests/WikiLinkTests.cs | 8 ++++---- WikiClientLibrary/Infrastructures/TokensManager.cs | 6 ++++-- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/UnitTestProject1/Tests/GeneratorTests1.cs b/UnitTestProject1/Tests/GeneratorTests1.cs index c9973d46a..c0c44c73b 100644 --- a/UnitTestProject1/Tests/GeneratorTests1.cs +++ b/UnitTestProject1/Tests/GeneratorTests1.cs @@ -72,9 +72,9 @@ public async Task AllCategoriesGeneratorTest1(string siteName, int batches) } [Theory] - [InlineData(nameof(WpTest2SiteAsync), "Category:Template documentation pages‏‎")] - [InlineData(nameof(WikiaTestSiteAsync), "BlogListingPage‏‎‏‎")] - [InlineData(nameof(TFWikiSiteAsync), "Category:Autobot subgroups‏‎")] + [InlineData(nameof(WpTest2SiteAsync), "Category:Template documentation pages‏")] + [InlineData(nameof(WikiaTestSiteAsync), "Free wiki software‎")] + [InlineData(nameof(TFWikiSiteAsync), "Category:Autobot subgroups‏")] public async Task CategoryMembersGeneratorTest(string siteName, string categoryName) { var site = await WikiSiteFromNameAsync(siteName); diff --git a/UnitTestProject1/Tests/PageTests.cs b/UnitTestProject1/Tests/PageTests.cs index 97f98cc77..6ad8418fb 100644 --- a/UnitTestProject1/Tests/PageTests.cs +++ b/UnitTestProject1/Tests/PageTests.cs @@ -59,7 +59,7 @@ public async Task WpEnPageReadTest1() [Theory] [InlineData(nameof(WpTest2SiteAsync), "Project:sandbox", "Wikipedia:Sandbox", BuiltInNamespaces.Project, 2076)] - [InlineData(nameof(WikiaTestSiteAsync), "Project:sandbox", "Discussion Manual Wiki:Sandbox", BuiltInNamespaces.Project, 637)] + [InlineData(nameof(WikiaTestSiteAsync), "Project:sandbox", "DMan Ⅱ Wiki:Sandbox", BuiltInNamespaces.Project, 149)] [InlineData(nameof(TFWikiSiteAsync), "Help:coming soon", "Help:Coming soon", BuiltInNamespaces.Help, 10122)] public async Task WikiPageReadTest2(string siteName, string fetchTitle, string expectedTitle, int expectedNs, int expectedId) { diff --git a/UnitTestProject1/Tests/SiteTokenTests.cs b/UnitTestProject1/Tests/SiteTokenTests.cs index 69b639af0..526b4e97e 100644 --- a/UnitTestProject1/Tests/SiteTokenTests.cs +++ b/UnitTestProject1/Tests/SiteTokenTests.cs @@ -18,14 +18,14 @@ public SiteTokenTests(ITestOutputHelper output, WikiSiteProvider wikiSiteProvide SiteNeedsLogin(Endpoints.TFWiki); } - [SkippableTheory] - [InlineData(nameof(WpTest2SiteAsync))] - [InlineData(nameof(WikiaTestSiteAsync))] - [InlineData(nameof(TFWikiSiteAsync))] - public async Task TokenTest(string testSiteName) + [Theory] + [InlineData(nameof(WpTest2SiteAsync), new[] { "edit", "move", "patrol" })] + [InlineData(nameof(WikiaTestSiteAsync), new[] { "edit", "move", "patrol" })] + [InlineData(nameof(TFWikiSiteAsync), new[] { "edit", "patrol" })] + public async Task TokenTest(string testSiteName, string[] tokenTypes) { var site = await WikiSiteFromNameAsync(testSiteName); - foreach (var tokenType in new[] { "edit", "move", "patrol" }) + foreach (var tokenType in tokenTypes) { // Fetch twice at a time... var tokenValueTask1 = site.GetTokenAsync(tokenType); diff --git a/UnitTestProject1/Tests/WikiLinkTests.cs b/UnitTestProject1/Tests/WikiLinkTests.cs index ecd305cf9..d1b196e4b 100644 --- a/UnitTestProject1/Tests/WikiLinkTests.cs +++ b/UnitTestProject1/Tests/WikiLinkTests.cs @@ -49,10 +49,10 @@ public async Task WikiLinkTest1() [Fact] public async Task TestMethod2() { - var WikiaTestSite = await WikiaTestSiteAsync; - var link1 = WikiLink.Parse(WikiaTestSite, "__ _project_ _talk_:___sandbox_", BuiltInNamespaces.Category); - var link2 = WikiLink.Parse(WikiaTestSite, "part1:part2:part3", BuiltInNamespaces.Category); - Assert.Equal("Discussion Manual Wiki talk:Sandbox", link1.ToString()); + var wikiaTestSite = await WikiaTestSiteAsync; + var link1 = WikiLink.Parse(wikiaTestSite, "__ _project_ _talk_:___sandbox_", BuiltInNamespaces.Category); + var link2 = WikiLink.Parse(wikiaTestSite, "part1:part2:part3", BuiltInNamespaces.Category); + Assert.Equal("DMan Ⅱ Wiki talk:Sandbox", link1.ToString()); Assert.Equal("Category:Part1:part2:part3", link2.ToString()); } diff --git a/WikiClientLibrary/Infrastructures/TokensManager.cs b/WikiClientLibrary/Infrastructures/TokensManager.cs index f35e277d9..d3e10e6a5 100644 --- a/WikiClientLibrary/Infrastructures/TokensManager.cs +++ b/WikiClientLibrary/Infrastructures/TokensManager.cs @@ -82,7 +82,7 @@ public async Task GetTokenAsync(string tokenType, bool forceRefetch, Can { if (string.IsNullOrEmpty(tokenType)) throw new ArgumentException(Prompts.ExceptionArgumentNullOrEmpty, nameof(tokenType)); - if (tokenType.Contains("|")) + if (tokenType.Contains('|')) throw new ArgumentException(Prompts.ExceptionArgumentContainsPipe, nameof(tokenType)); cancellationToken.ThrowIfCancellationRequested(); tokenType = tokenType.Trim(); @@ -95,6 +95,8 @@ public async Task GetTokenAsync(string tokenType, bool forceRefetch, Can if (site.SiteInfo.Version < v117 && tokenType == "patrol") realTokenType = "edit"; // Use csrf token if possible. + // https://www.mediawiki.org/wiki/MediaWiki_1.37/Deprecation_of_legacy_API_token_parameters + // https://github.com/wikimedia/mediawiki/blob/1.19.10/includes/api/ApiQueryInfo.php if (site.SiteInfo.Version >= v124 && CsrfTokens.Contains(tokenType)) realTokenType = "csrf"; // Collect tokens from cache @@ -156,7 +158,7 @@ string ExtractToken(IDictionary jTokens, string tokenType1) } var token = (string?)jtoken; if (token == null) - throw new ArgumentException($"Invalid token type: {tokenType1}.", nameof(tokenType)); + throw new ArgumentException($"Failed to extract {tokenType1} token from the API response.", nameof(tokenType)); return token; }