Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/SIL.XForge.Scripture/Services/SFProjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,15 @@ await conn.CreateAsync(
await base.AddUserToProjectAsync(conn, projectDoc, userDoc, projectRole, shareKey);
if (SFProjectRole.IsParatextRole(projectRole))
{
int index = projectDoc.Data.ParatextUsers.FindIndex(u => u.Username == userDoc.Data.Name);
UserSecret userSecret = _userSecrets.Query().FirstOrDefault(u => u.Id == userDoc.Id);
if (userSecret == null)
{
// No user secret means we cannot get a Paratext username. Skip adding the user to ParatextUsers
return;
}

string username = _paratextService.GetParatextUsername(userSecret);
int index = projectDoc.Data.ParatextUsers.FindIndex(u => u.Username == username);
if (index > -1)
await projectDoc.SubmitJson0OpAsync(op =>
{
Expand All @@ -1275,7 +1283,7 @@ await projectDoc.SubmitJson0OpAsync(op =>
new ParatextUserProfile
{
SFUserId = userDoc.Id,
Username = userDoc.Data.Name,
Username = username,
Role = projectRole,
OpaqueUserId = _guidService.NewObjectId(),
}
Expand Down
51 changes: 43 additions & 8 deletions test/SIL.XForge.Scripture.Tests/Services/SFProjectServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,29 @@ public async Task AddUserAsync_AddsRoleAndIdToParatextUsers()
);
}

[Test]
public async Task AddUserAsync_DoesNotAddExistingUser()
{
var env = new TestEnvironment();
SFProject project = env.GetProject(Project02);

Assert.That(project.UserRoles.ContainsKey(User05), Is.False, "setup");
Assert.That(project.ParatextUsers.Count, Is.EqualTo(3), "setup");
Assert.That(project.ParatextUsers.Exists(u => u.Username == "User 05"), Is.True, "setup");
env.ParatextService.TryGetProjectRoleAsync(
Arg.Any<UserSecret>(),
Arg.Any<string>(),
Arg.Any<CancellationToken>()
)
.Returns(Task.FromResult(Attempt.Success(SFProjectRole.Translator)));

await env.Service.AddUserAsync(User05, Project02, null);
project = env.GetProject(Project02);

Assert.That(project.UserRoles.ContainsKey(User05), Is.True, "User should have been added to project");
Assert.That(project.ParatextUsers.Count, Is.EqualTo(3), "No new ParatextUser should have been added");
}

[Test]
public async Task AddUserAsync_ShareKeyExists_AddsUserAndRemovesKey()
{
Expand Down Expand Up @@ -4574,6 +4597,13 @@ public async Task SyncUserRoleAsync_UpgradesRole()
private class TestEnvironment
{
public static readonly Uri WebsiteUrl = new Uri("http://localhost/", UriKind.Absolute);
public readonly Dictionary<string, string> UserIdsToParatextUsernames = new Dictionary<string, string>
{
{ User01, "User 01" },
{ User02, "User 02" },
{ User03, "User 03" },
{ User05, "User 05" },
};

public TestEnvironment()
{
Expand Down Expand Up @@ -4648,13 +4678,13 @@ public TestEnvironment()
Id = User05,
Email = "[email protected]",
ParatextId = "pt-user05",
Name = "User 05",
Name = "[email protected]",
Roles = [SystemRole.User],
Sites = new Dictionary<string, Site>
{
{
SiteId,
new Site { Projects = { Project01 } }
new Site { Projects = { Project01, Project02 } }
},
},
},
Expand Down Expand Up @@ -4845,6 +4875,12 @@ public TestEnvironment()
Username = "User 01",
OpaqueUserId = "syncuser01",
},
new ParatextUserProfile
{
Username = "User 05",
Role = SFProjectRole.Translator,
OpaqueUserId = "syncuser05",
},
},
},
new SFProject
Expand Down Expand Up @@ -5380,12 +5416,7 @@ public TestEnvironment()
ParatextService.CanUserAuthenticateToPTRegistryAsync(Arg.Any<UserSecret>()).Returns(Task.FromResult(true));
ParatextService.CanUserAuthenticateToPTArchivesAsync(Arg.Any<string>()).Returns(Task.FromResult(true));
UserSecrets = new MemoryRepository<UserSecret>(
[
new UserSecret { Id = User01 },
new UserSecret { Id = User02 },
new UserSecret { Id = User03 },
new UserSecret { Id = User05 },
]
UserIdsToParatextUsernames.Keys.Select(id => new UserSecret { Id = id })
);
var translateMetrics = new MemoryRepository<TranslateMetrics>();
FileSystemService = Substitute.For<IFileSystemService>();
Expand Down Expand Up @@ -5507,6 +5538,10 @@ public TestEnvironment()
.GetResourcePermissionAsync(Arg.Any<string>(), Arg.Any<string>(), CancellationToken.None)
.Returns(TextInfoPermission.Read);

ParatextService
.GetParatextUsername(Arg.Any<UserSecret>())
.Returns(u => UserIdsToParatextUsernames[((UserSecret)u[0]).Id]);

Service = new SFProjectService(
RealtimeService,
siteOptions,
Expand Down
Loading