Repository navigation
fix(club): 修复未来学年任期异常 #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bc1ac95
fix(api): 明确成员任期生命周期约束
FEnjoyit cf61f8e
chore(api): 从 openapi.yaml 自动生成 API 代码
FEnjoyit d6c955c
fix(club): 阻止异常成员任期覆盖
FEnjoyit 985d142
fix(frontend): 修正成员任期与换届名册交互
FEnjoyit 359d3a3
docs(pr): 补充成员任期修复截图
FEnjoyit 4eaf4d8
test(api): 放宽错误码契约格式断言
FEnjoyit b689bdf
fix(club): 串行化成员任期写入
FEnjoyit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| using System.Net; | ||
| using System.Net.Http.Headers; | ||
| using System.Net.Http.Json; | ||
| using System.Text.Json; | ||
| using ClubHub.Api.Data; | ||
| using ClubHub.Api.Data.Entities; | ||
| using ClubHub.Api.Services; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace ClubHub.Api.Tests; | ||
|
|
||
| public sealed class ClubMemberTermEndpointTests | ||
| { | ||
| [Fact] | ||
| public async Task GetMembers_DefaultQueryIncludesFutureActiveTerm() | ||
| { | ||
| await using var factory = new ClubHubWebApplicationFactory(); | ||
| var dates = FutureTermDates(2); | ||
| using var client = await SeedClubAsync(factory, dates.Start, dates.End, "未来任期"); | ||
|
|
||
| using var response = await client.GetAsync("/api/v1/clubs/830002/members"); | ||
|
|
||
| Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
| using var document = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); | ||
| var member = Assert.Single(document.RootElement.EnumerateArray()); | ||
| Assert.Equal("active", member.GetProperty("memberStatus").GetString()); | ||
| Assert.Equal("未来任期", member.GetProperty("termName").GetString()); | ||
| Assert.False(member.GetProperty("isCurrent").GetBoolean()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CreateMemberTerm_DuplicateTermReturnsConflictWithoutEndingExistingTerm() | ||
| { | ||
| await using var factory = new ClubHubWebApplicationFactory(); | ||
| var dates = FutureTermDates(2); | ||
| using var client = await SeedClubAsync(factory, dates.Start, dates.End, "2030-2031学年"); | ||
|
|
||
| using var response = await CreateTermAsync( | ||
| client, | ||
| "2030-2031学年", | ||
| dates.Start, | ||
| dates.End); | ||
|
|
||
| Assert.Equal(HttpStatusCode.Conflict, response.StatusCode); | ||
| using var body = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync()); | ||
| Assert.Equal("CONFLICT", body.RootElement.GetProperty("code").GetString()); | ||
| Assert.Contains("同名任期", body.RootElement.GetProperty("message").GetString()); | ||
| await AssertExistingTermUnchangedAsync(factory, dates.Start, dates.End); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CreateMemberTerm_EarlierThanExistingActiveTermReturnsConflict() | ||
| { | ||
| await using var factory = new ClubHubWebApplicationFactory(); | ||
| var existingDates = FutureTermDates(3); | ||
| var requestedDates = FutureTermDates(2); | ||
| using var client = await SeedClubAsync( | ||
| factory, | ||
| existingDates.Start, | ||
| existingDates.End, | ||
| "较晚任期"); | ||
|
|
||
| using var response = await CreateTermAsync( | ||
| client, | ||
| "较早任期", | ||
| requestedDates.Start, | ||
| requestedDates.End); | ||
|
|
||
| Assert.Equal(HttpStatusCode.Conflict, response.StatusCode); | ||
| await AssertExistingTermUnchangedAsync(factory, existingDates.Start, existingDates.End); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CreateMemberTerm_SameStartWithDifferentNameReturnsConflict() | ||
| { | ||
| await using var factory = new ClubHubWebApplicationFactory(); | ||
| var dates = FutureTermDates(2); | ||
| using var client = await SeedClubAsync(factory, dates.Start, dates.End, "原任期"); | ||
|
|
||
| using var response = await CreateTermAsync( | ||
| client, | ||
| "同日起始的新任期", | ||
| dates.Start, | ||
| dates.End); | ||
|
|
||
| Assert.Equal(HttpStatusCode.Conflict, response.StatusCode); | ||
| await AssertExistingTermUnchangedAsync(factory, dates.Start, dates.End); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task CreateMemberTerm_LaterTermClosesEarlierOverlappingTerm() | ||
| { | ||
| await using var factory = new ClubHubWebApplicationFactory(); | ||
| var existingStart = new DateTime(DateTime.UtcNow.Year, 7, 1); | ||
| var requestedDates = FutureTermDates(2); | ||
| var existingEnd = requestedDates.End; | ||
| using var client = await SeedClubAsync( | ||
| factory, | ||
| existingStart, | ||
| existingEnd, | ||
| "原任期"); | ||
|
|
||
| using var response = await CreateTermAsync( | ||
| client, | ||
| "新任期", | ||
| requestedDates.Start, | ||
| requestedDates.End); | ||
|
|
||
| Assert.Equal(HttpStatusCode.Created, response.StatusCode); | ||
| await using var scope = factory.Services.CreateAsyncScope(); | ||
| var db = scope.ServiceProvider.GetRequiredService<ClubHubDbContext>(); | ||
| var terms = await db.ClubMembers | ||
| .Where(member => member.ClubId == 830002 && member.UserId == 830003) | ||
| .OrderBy(member => member.TermStart) | ||
| .ToListAsync(); | ||
| Assert.Equal(2, terms.Count); | ||
| Assert.Equal("ended", terms[0].MemberStatus); | ||
| Assert.Equal(requestedDates.Start.AddDays(-1), terms[0].TermEnd); | ||
| Assert.Equal("active", terms[1].MemberStatus); | ||
| Assert.Equal(requestedDates.Start, terms[1].TermStart); | ||
| Assert.Equal(requestedDates.End, terms[1].TermEnd); | ||
| } | ||
|
|
||
| private static async Task<HttpClient> SeedClubAsync( | ||
| ClubHubWebApplicationFactory factory, | ||
| DateTime existingStart, | ||
| DateTime existingEnd, | ||
| string existingTermName) | ||
| { | ||
| await using var scope = factory.Services.CreateAsyncScope(); | ||
| var db = scope.ServiceProvider.GetRequiredService<ClubHubDbContext>(); | ||
| var now = DateTime.UtcNow; | ||
| var principal = new User | ||
| { | ||
| UserId = 830001, | ||
| Username = "member-term-principal", | ||
| PasswordHash = "unused", | ||
| RealName = "任期测试负责人", | ||
| AccountStatus = "normal", | ||
| CreatedAt = now | ||
| }; | ||
| db.Users.AddRange( | ||
| principal, | ||
| new User | ||
| { | ||
| UserId = 830003, | ||
| Username = "member-term-target", | ||
| PasswordHash = "unused", | ||
| RealName = "任期测试成员", | ||
| AccountStatus = "normal", | ||
| CreatedAt = now | ||
| }); | ||
| db.Clubs.Add(new Club | ||
| { | ||
| ClubId = 830002, | ||
| ClubName = "任期回归测试社团", | ||
| PresidentUserId = principal.UserId, | ||
| AuditStatus = "approved", | ||
| ClubStatus = "active", | ||
| CreatedAt = now | ||
| }); | ||
| db.ClubMembers.Add(new ClubMember | ||
| { | ||
| MemberId = 830004, | ||
| ClubId = 830002, | ||
| UserId = 830003, | ||
| PositionName = "成员", | ||
| TermName = existingTermName, | ||
| TermStart = existingStart, | ||
| TermEnd = existingEnd, | ||
| MemberStatus = "active", | ||
| JoinAt = now, | ||
| ContributionScore = 0 | ||
| }); | ||
| await db.SaveChangesAsync(); | ||
|
|
||
| var token = scope.ServiceProvider | ||
| .GetRequiredService<AuthTokenService>() | ||
| .CreateToken(principal); | ||
| var client = factory.CreateClient(); | ||
| client.DefaultRequestHeaders.Authorization = | ||
| new AuthenticationHeaderValue("Bearer", token); | ||
| return client; | ||
| } | ||
|
|
||
| private static async Task<HttpResponseMessage> CreateTermAsync( | ||
| HttpClient client, | ||
| string termName, | ||
| DateTime termStart, | ||
| DateTime termEnd) | ||
| { | ||
| return await client.PostAsJsonAsync( | ||
| "/api/v1/clubs/830002/members/terms", | ||
| new | ||
| { | ||
| userId = 830003, | ||
| positionName = "成员", | ||
| termName, | ||
| termStart, | ||
| termEnd, | ||
| memberStatus = "active", | ||
| contributionScore = 0, | ||
| closeCurrentTerm = true | ||
| }); | ||
| } | ||
|
|
||
| private static async Task AssertExistingTermUnchangedAsync( | ||
| ClubHubWebApplicationFactory factory, | ||
| DateTime expectedStart, | ||
| DateTime expectedEnd) | ||
| { | ||
| await using var scope = factory.Services.CreateAsyncScope(); | ||
| var db = scope.ServiceProvider.GetRequiredService<ClubHubDbContext>(); | ||
| var term = Assert.Single(await db.ClubMembers | ||
| .Where(member => member.ClubId == 830002 && member.UserId == 830003) | ||
| .ToListAsync()); | ||
| Assert.Equal("active", term.MemberStatus); | ||
| Assert.Equal(expectedStart, term.TermStart); | ||
| Assert.Equal(expectedEnd, term.TermEnd); | ||
| } | ||
|
|
||
| private static (DateTime Start, DateTime End) FutureTermDates(int yearsAhead) | ||
| { | ||
| var startYear = DateTime.UtcNow.Year + yearsAhead; | ||
| return (new DateTime(startYear, 7, 1), new DateTime(startYear + 1, 6, 30)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.