Files
Academic-Affairs-System/tests/Jiaowu.Api.Tests/StudentDisciplinesControllerTests.cs
T

72 lines
5.0 KiB
C#

using Jiaowu.Api.Controllers;
using Jiaowu.Api.Domain.Academic;
using Jiaowu.Api.Domain.Identity;
using Jiaowu.Api.Infrastructure.Auth;
using Jiaowu.Api.Infrastructure.Persistence;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
namespace Jiaowu.Api.Tests;
public sealed class StudentDisciplinesControllerTests
{
[Fact]
public async Task Development_migration_accepts_a_new_database_and_is_repeatable()
{
await using var connection = new SqliteConnection("Data Source=:memory:");
await connection.OpenAsync();
await using var db = new AppDbContext(new DbContextOptionsBuilder<AppDbContext>().UseSqlite(connection).Options);
await db.Database.EnsureCreatedAsync();
var migrator = new DevelopmentSqliteMigrator(db, NullLogger<DevelopmentSqliteMigrator>.Instance);
await migrator.MigrateAsync();
await migrator.MigrateAsync();
Assert.Empty(await db.StudentDisciplineEvents.ToListAsync());
}
[Fact]
public async Task School_student_affairs_can_review_but_other_college_cannot_read()
{
await using var connection = new SqliteConnection("Data Source=:memory:");
await connection.OpenAsync();
await using var db = new AppDbContext(new DbContextOptionsBuilder<AppDbContext>().UseSqlite(connection).Options);
await db.Database.EnsureCreatedAsync();
var college = new College { Code = "C", Name = "学院" };
var major = new Major { Code = "M", Name = "专业", CollegeId = college.Id, DegreeType = "学士" };
var cls = new AdministrativeClass { Code = "A", Name = "班级", MajorId = major.Id, Grade = 2026 };
var student = new Student { StudentNumber = "2026001", Name = "学生", AdministrativeClassId = cls.Id, EnrollmentYear = 2026, EnrollmentDate = new DateOnly(2026, 9, 1), Status = StudentStatus.Active };
db.Add(college); await db.SaveChangesAsync(); db.Add(major); await db.SaveChangesAsync(); db.Add(cls); await db.SaveChangesAsync(); db.Add(student); await db.SaveChangesAsync();
var item = new StudentDiscipline { StudentId = student.Id, DisciplineType = "警告", Facts = "事实", Basis = "依据", DecisionNumber = "1", DecisionAuthority = "学校", DecisionDate = new DateOnly(2026, 1, 1), EffectiveDate = new DateOnly(2026, 1, 1), State = StudentDisciplineState.CollegeApproved, CreatedByUserId = Guid.NewGuid() };
db.Add(item); await db.SaveChangesAsync();
var school = Controller(db, SystemRoles.StudentAffairsAdmin, DataScope.Self, null);
Assert.IsType<NoContentResult>(await school.Transition(item.Id, new(StudentDisciplineState.Effective, "同意"), default));
Assert.Equal(StudentDisciplineState.Effective, item.State);
Assert.Single(await db.StudentDisciplineEvents.ToListAsync());
var otherCollege = Controller(db, SystemRoles.CollegeAdmin, DataScope.College, Guid.NewGuid());
Assert.IsType<NotFoundResult>(await otherCollege.Detail(item.Id, default));
var counselor = Controller(db, SystemRoles.Counselor, DataScope.Class, null);
Assert.IsType<NotFoundResult>(await counselor.Detail(item.Id, default));
Assert.IsType<ConflictObjectResult>(await school.Transition(item.Id, new(StudentDisciplineState.CollegeApproved, null), default));
var export = Assert.IsType<FileContentResult>(await otherCollege.Export(null, null, null, null, null, default));
using var stream = new MemoryStream(export.FileContents);
using var workbook = new ClosedXML.Excel.XLWorkbook(stream);
Assert.Equal(1, workbook.Worksheet(1).LastRowUsed()!.RowNumber());
var custom = Assert.IsType<FileContentResult>(await school.Export(null, null, null, null, null, default, "1,0", "xlsx"));
using var selected = new ClosedXML.Excel.XLWorkbook(new MemoryStream(custom.FileContents));
Assert.Equal("姓名", selected.Worksheet(1).Cell(1, 1).GetString());
Assert.Equal(student.Name, selected.Worksheet(1).Cell(2, 1).GetString());
var restricted = Assert.IsType<FileContentResult>(await otherCollege.Export(null, null, null, null, null, default, "1,0", "txt"));
Assert.DoesNotContain(student.StudentNumber, System.Text.Encoding.UTF8.GetString(restricted.FileContents));
Assert.IsType<NoContentResult>(await school.SetTypes(["警告", "严重警告"], default));
Assert.IsType<NoContentResult>(await school.SetTypes(["严重警告"], default));
Assert.False((await db.StudentDisciplineTypes.SingleAsync(x => x.Name == "警告")).IsEnabled);
Assert.Equal("警告", item.DisciplineType);
}
private static StudentDisciplinesController Controller(AppDbContext db, string role, DataScope scope, Guid? college) =>
new(db, new TestScope(new CurrentUserScope(Guid.NewGuid(), null, college, scope, new HashSet<string> { role })), null!, null!, NullLogger<StudentDisciplinesController>.Instance);
private sealed record TestScope(CurrentUserScope Current) : ICurrentUserDataScope;
}