Skip to content
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

Moving accessible_teams from UserType to MeType. #1991

Merged
merged 3 commits into from
Aug 14, 2024
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
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
4 changes: 2 additions & 2 deletions app/models/concerns/project_media_creators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,14 @@ def create_claim_description_and_fact_check
claim_description: cd,
report_status: (fact_check['publish_report'] ? 'published' : 'unpublished'),
rating: self.set_status,
tags: self.set_tags,
tags: self.set_tags.to_a.map(&:strip),
skip_check_ability: true
})
end
fc
end

def create_tags
self.set_tags.each { |tag| Tag.create!(annotated: self, tag: tag, skip_check_ability: true) } if self.set_tags.is_a?(Array)
self.set_tags.each { |tag| Tag.create!(annotated: self, tag: tag.strip, skip_check_ability: true) } if self.set_tags.is_a?(Array)
end
end
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
8 changes: 5 additions & 3 deletions test/controllers/graphql_controller_12_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ def teardown
end

test "should treat ' tag' and 'tag' as the same tag, and not try to create a new tag" do
Sidekiq::Testing.inline!
t = create_team
a = ApiKey.create!
b = create_bot_user api_key_id: a.id
Expand All @@ -622,7 +623,7 @@ def teardown
createProjectMedia(input: {
project_id: ' + p.id.to_s + ',
media_type: "Blank",
channel: {main: 1},
channel: { main: 1 },
set_tags: ["science"],
set_status: "verified",
set_claim_description: "Claim #1.",
Expand All @@ -647,13 +648,14 @@ def teardown
post :create, params: { query: query1, team: t.slug }
assert_response :success
assert_equal 'science', JSON.parse(@response.body)['data']['createProjectMedia']['project_media']['tags']['edges'][0]['node']['tag_text']
sleep 1

query2 = ' mutation create {
createProjectMedia(input: {
project_id: ' + p.id.to_s + ',
media_type: "Blank",
channel: {main: 1},
set_tags: [" science"],
channel: { main: 1 },
set_tags: ["science "],
set_status: "verified",
set_claim_description: "Claim #2.",
set_fact_check: {
Expand Down
Loading