-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix bug with project members query permissions (#1229)
* write permission test to ensure that users can't query project members they don't have access to
- Loading branch information
Showing
7 changed files
with
173 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,138 @@ | ||
using System.Text.Json.Nodes; | ||
using Shouldly; | ||
using Testing.Services; | ||
|
||
namespace Testing.ApiTests; | ||
|
||
[Trait("Category", "Integration")] | ||
public class ProjectPermissionTests : ApiTestBase | ||
{ | ||
private async Task<JsonObject> QueryProject(string projectCode, bool expectGqlError = false) | ||
{ | ||
var json = await ExecuteGql( | ||
$$""" | ||
query { | ||
projectByCode(code: "{{projectCode}}") { | ||
id | ||
name | ||
users { | ||
user { | ||
id | ||
name | ||
} | ||
} | ||
} | ||
} | ||
""", | ||
expectGqlError); | ||
return json; | ||
} | ||
|
||
private async Task AddUserToProject(Guid projectId, string username) | ||
{ | ||
await ExecuteGql( | ||
$$""" | ||
mutation { | ||
addProjectMember(input: { | ||
projectId: "{{projectId}}", | ||
usernameOrEmail: "{{username}}", | ||
role: EDITOR, | ||
canInvite: false | ||
}) { | ||
project { | ||
id | ||
} | ||
errors { | ||
__typename | ||
... on Error { | ||
message | ||
} | ||
} | ||
} | ||
} | ||
"""); | ||
} | ||
|
||
private JsonObject GetProject(JsonObject json) | ||
{ | ||
var project = json["data"]!["projectByCode"]?.AsObject(); | ||
project.ShouldNotBeNull(); | ||
return project; | ||
} | ||
|
||
private void MustHaveMembers(JsonObject project, int? count = null) | ||
{ | ||
var members = project["users"]!.AsArray(); | ||
members.ShouldNotBeNull().ShouldNotBeEmpty(); | ||
if (count is not null) members.Count.ShouldBe(count.Value); | ||
} | ||
|
||
private void MustNotHaveMembers(JsonObject project) | ||
{ | ||
var users = project["users"]!.AsArray(); | ||
users.ShouldBeEmpty(); | ||
} | ||
|
||
private void MustHaveOnlyUserAsMember(JsonObject project, Guid userId) | ||
{ | ||
var users = project["users"]!.AsArray(); | ||
users.ShouldContain(node => node!["user"]!["id"]!.GetValue<Guid>() == userId, | ||
"user list " + users.ToJsonString()); | ||
} | ||
|
||
[Fact] | ||
public async Task MemberCanSeeProjectMembers() | ||
{ | ||
await LoginAs("manager"); | ||
await using var project = await this.RegisterProjectInLexBox(Utils.GetNewProjectConfig()); | ||
//refresh jwt | ||
await LoginAs("manager"); | ||
var json = GetProject(await QueryProject(project.Code)); | ||
MustHaveMembers(json); | ||
} | ||
|
||
[Fact] | ||
public async Task NonMemberCannotSeeProjectMembers() | ||
{ | ||
await LoginAs("manager"); | ||
await using var project = await this.RegisterProjectInLexBox(Utils.GetNewProjectConfig()); | ||
await LoginAs("user"); | ||
var json = GetProject(await QueryProject(project.Code)); | ||
MustNotHaveMembers(json); | ||
} | ||
|
||
[Fact] | ||
public async Task ConfidentialProject_ManagerCanSeeProjectMembers() | ||
{ | ||
await LoginAs("manager"); | ||
await using var project = await this.RegisterProjectInLexBox(Utils.GetNewProjectConfig(isConfidential: true)); | ||
await LoginAs("manager"); | ||
var json = GetProject(await QueryProject(project.Code)); | ||
MustHaveMembers(json); | ||
} | ||
|
||
[Fact] | ||
public async Task ConfidentialProject_NonManagerCannotSeeProjectMembers() | ||
{ | ||
await LoginAs("manager"); | ||
await using var project = await this.RegisterProjectInLexBox(Utils.GetNewProjectConfig(isConfidential: true)); | ||
await LoginAs("manager"); | ||
await AddUserToProject(project.Id, "editor"); | ||
MustHaveMembers(GetProject(await QueryProject(project.Code)), count: 2); | ||
await LoginAs("editor"); | ||
var json = GetProject(await QueryProject(project.Code)); | ||
MustHaveOnlyUserAsMember(json, CurrentUser.Id); | ||
} | ||
|
||
[Fact] | ||
public async Task ConfidentialProject_NonMemberCannotSeeProject() | ||
{ | ||
await LoginAs("manager"); | ||
await using var project = await this.RegisterProjectInLexBox(Utils.GetNewProjectConfig(isConfidential: true)); | ||
await LoginAs("user"); | ||
var json = await QueryProject(project.Code, expectGqlError: true); | ||
var error = json["errors"]!.AsArray().First()?.AsObject(); | ||
error.ShouldNotBeNull(); | ||
error["extensions"]?["code"]?.GetValue<string>().ShouldBe("AUTH_NOT_AUTHORIZED"); | ||
} | ||
} |
This file contains 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 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