Skip to content

Commit

Permalink
Moving accessible_teams from UserType to MeType.
Browse files Browse the repository at this point in the history
This information is only needed for the current user, so, it should be under `MeType`, not `UserType`.

References: CV2-4938 and CV2-4704.
  • Loading branch information
caiosba committed Aug 14, 2024
1 parent be71f00 commit 0d9b54a
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 93 deletions.
14 changes: 14 additions & 0 deletions app/graph/types/me_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ def team_users_count(status: nil)
team_users(status: status).count
end

field :accessible_teams, TeamType.connection_type, null: true

def accessible_teams
return Team.none unless object == User.current
teams = User.current.is_admin? ? Team.all : User.current.teams.where('team_users.status' => 'member')
teams.order('name ASC')
end

field :accessible_teams_count, GraphQL::Types::Int, null: true

def accessible_teams_count
accessible_teams.count
end

field :annotations, AnnotationType.connection_type, null: true do
argument :type, GraphQL::Types::String, required: false
end
Expand Down
5 changes: 0 additions & 5 deletions app/graph/types/user_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ def profile_image
super_admin? ? "#{CheckConfig.get('checkdesk_base_url')}/images/user.png" : object.profile_image
end

field :accessible_teams, PublicTeamType.connection_type, null: true
def accessible_teams
User.current.is_admin? ? Team.all : User.current.teams
end

private

def super_admin?
Expand Down
43 changes: 22 additions & 21 deletions lib/relay.idl
Original file line number Diff line number Diff line change
Expand Up @@ -8899,6 +8899,28 @@ Me type
"""
type Me implements Node {
accepted_terms: Boolean
accessible_teams(
"""
Returns the elements in the list that come after the specified cursor.
"""
after: String

"""
Returns the elements in the list that come before the specified cursor.
"""
before: String

"""
Returns the first _n_ elements from the list.
"""
first: Int

"""
Returns the last _n_ elements from the list.
"""
last: Int
): TeamConnection
accessible_teams_count: Int
annotations(
"""
Returns the elements in the list that come after the specified cursor.
Expand Down Expand Up @@ -16258,27 +16280,6 @@ type UpdateUserPayload {
User type
"""
type User implements Node {
accessible_teams(
"""
Returns the elements in the list that come after the specified cursor.
"""
after: String

"""
Returns the elements in the list that come before the specified cursor.
"""
before: String

"""
Returns the first _n_ elements from the list.
"""
first: Int

"""
Returns the last _n_ elements from the list.
"""
last: Int
): PublicTeamConnection
created_at: String
dbid: Int
email: String
Expand Down
136 changes: 75 additions & 61 deletions public/relay.json
Original file line number Diff line number Diff line change
Expand Up @@ -48439,6 +48439,81 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "accessible_teams",
"description": null,
"args": [
{
"name": "after",
"description": "Returns the elements in the list that come after the specified cursor.",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "before",
"description": "Returns the elements in the list that come before the specified cursor.",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "first",
"description": "Returns the first _n_ elements from the list.",
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "last",
"description": "Returns the last _n_ elements from the list.",
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
}
],
"type": {
"kind": "OBJECT",
"name": "TeamConnection",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "accessible_teams_count",
"description": null,
"args": [

],
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "annotations",
"description": null,
Expand Down Expand Up @@ -89789,67 +89864,6 @@
"name": "User",
"description": "User type",
"fields": [
{
"name": "accessible_teams",
"description": null,
"args": [
{
"name": "after",
"description": "Returns the elements in the list that come after the specified cursor.",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "before",
"description": "Returns the elements in the list that come before the specified cursor.",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "first",
"description": "Returns the first _n_ elements from the list.",
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "last",
"description": "Returns the last _n_ elements from the list.",
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
}
],
"type": {
"kind": "OBJECT",
"name": "PublicTeamConnection",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "created_at",
"description": null,
Expand Down
16 changes: 10 additions & 6 deletions test/controllers/graphql_controller_11_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def teardown
assert_not_nil data['project_media']['id']
end

test "admin users should be able to see all workspaces" do
test "admin users should be able to see all workspaces as accessible teams" do
Team.destroy_all

user = create_user
Expand All @@ -128,16 +128,18 @@ def teardown
create_team_user user: admin, team: team2

authenticate_with_user(admin)
query = "query { user(id: #{admin.id}) { accessible_teams { edges { node { dbid } } } } }"
query = "query { me { accessible_teams_count, accessible_teams { edges { node { dbid } } } } }"
post :create, params: { query: query }
assert_response :success
data = JSON.parse(response.body)['data']['user']['accessible_teams']['edges']
response = JSON.parse(@response.body)['data']['me']
data = response['accessible_teams']['edges']
assert_equal 2, data.size
assert_equal team1.id, data[0]['node']['dbid']
assert_equal team2.id, data[1]['node']['dbid']
assert_equal 2, response['accessible_teams_count']
end

test "non-admin users should only be able to see workspaces they belong to" do
test "non-admin users should only be able to see workspaces they belong to as accessible teams" do
Team.destroy_all
user = create_user
team1 = create_team
Expand All @@ -148,11 +150,13 @@ def teardown
create_team_user user: user2, team: team2

authenticate_with_user(user)
query = "query { user(id: #{user.id}) { accessible_teams { edges { node { dbid } } } } }"
query = "query { me { accessible_teams_count, accessible_teams { edges { node { dbid } } } } }"
post :create, params: { query: query }
assert_response :success
data = JSON.parse(response.body)['data']['user']['accessible_teams']['edges']
response = JSON.parse(@response.body)['data']['me']
data = response['accessible_teams']['edges']
assert_equal 1, data.size
assert_equal team1.id, data[0]['node']['dbid']
assert_equal 1, response['accessible_teams_count']
end
end

0 comments on commit 0d9b54a

Please sign in to comment.