Skip to content

Commit

Permalink
[WIP] Ticket CV2-5067: Finishing mutation and adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba committed Aug 16, 2024
1 parent f3ed5cf commit 4bd600e
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 4 deletions.
14 changes: 13 additions & 1 deletion app/graph/mutations/export_mutations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@ class ExportList < Mutations::BaseMutation
field :success, GraphQL::Types::Boolean, null: true

def resolve(query:)
{ success: false }
ability = context[:ability]
team = Team.find_if_can(Team.current.id, ability)
if ability.cannot?(:export_list, team)
{ success: false }
else
search = CheckSearch.new(query, nil, team.id)
if search.number_of_results > CheckConfig.get(:export_csv_maximum_number_of_results, 10000, :integer)
{ success: false }
else
search.export_to_csv
{ success: true }
end
end
end
end
end
1 change: 1 addition & 0 deletions app/lib/check_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

class CheckConfig
def self.get(key, default = nil, type = nil)
key = key.to_s
value = ENV[key]
value ||= CONFIG[key] if CONFIG.has_key?(key)
return default if value.nil?
Expand Down
2 changes: 1 addition & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def admin_perms
can :destroy, Team, :id => @context_team.id
can :create, TeamUser, :team_id => @context_team.id, role: ['admin']
can [:update, :destroy], TeamUser, team_id: @context_team.id
can :duplicate, Team, :id => @context_team.id
can [:duplicate, :export_list], Team, :id => @context_team.id
can :set_privacy, Project, :team_id => @context_team.id
can :read_feed_invitations, Feed, :team_id => @context_team.id
can :destroy, Feed, :team_id => @context_team.id
Expand Down
3 changes: 1 addition & 2 deletions config/config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,13 @@ development: &default
tipline_user_max_messages_per_day: 1500
nlu_global_rate_limit: 100
nlu_user_rate_limit: 30

devise_maximum_attempts: 5
devise_unlock_accounts_after: 1
login_rate_limit: 10
api_rate_limit: 100

session_store_key: '_checkdesk_session_dev'
session_store_domain: 'localhost'
export_csv_maximum_number_of_results: 10000
test:
<<: *default
checkdesk_base_url_private: http://api:3000
Expand Down
4 changes: 4 additions & 0 deletions lib/check_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ def medias_get_search_result(query)
@options['es_id'] ? $repository.find([@options['es_id']]).compact : $repository.search(query: query, collapse: collapse, sort: sort, size: @options['eslimit'], from: @options['esoffset']).results
end

def export_to_csv
# TODO
end

private

def adjust_es_window_size
Expand Down
28 changes: 28 additions & 0 deletions lib/relay.idl
Original file line number Diff line number Diff line change
Expand Up @@ -8357,6 +8357,28 @@ type ExplainerItemEdge {
node: ExplainerItem
}

"""
Autogenerated input type of ExportList
"""
input ExportListInput {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
query: String!
}

"""
Autogenerated return type of ExportList
"""
type ExportListPayload {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
success: Boolean
}

"""
Autogenerated input type of ExtractText
"""
Expand Down Expand Up @@ -9917,6 +9939,12 @@ type MutationType {
"""
input: DuplicateTeamMutationInput!
): DuplicateTeamMutationPayload
exportList(
"""
Parameters for ExportList
"""
input: ExportListInput!
): ExportListPayload
extractText(
"""
Parameters for ExtractText
Expand Down
109 changes: 109 additions & 0 deletions public/relay.json
Original file line number Diff line number Diff line change
Expand Up @@ -45264,6 +45264,86 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "INPUT_OBJECT",
"name": "ExportListInput",
"description": "Autogenerated input type of ExportList",
"fields": null,
"inputFields": [
{
"name": "query",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "clientMutationId",
"description": "A unique identifier for the client performing the mutation.",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
}
],
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "ExportListPayload",
"description": "Autogenerated return type of ExportList",
"fields": [
{
"name": "clientMutationId",
"description": "A unique identifier for the client performing the mutation.",
"args": [

],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "success",
"description": null,
"args": [

],
"type": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [

],
"enumValues": null,
"possibleTypes": null
},
{
"kind": "INPUT_OBJECT",
"name": "ExtractTextInput",
Expand Down Expand Up @@ -53582,6 +53662,35 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "exportList",
"description": null,
"args": [
{
"name": "input",
"description": "Parameters for ExportList",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "INPUT_OBJECT",
"name": "ExportListInput",
"ofType": null
}
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
}
],
"type": {
"kind": "OBJECT",
"name": "ExportListPayload",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "extractText",
"description": null,
Expand Down
38 changes: 38 additions & 0 deletions test/controllers/graphql_controller_11_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,42 @@ def teardown
assert_equal team1.id, data[0]['node']['dbid']
assert_equal 1, response['accessible_teams_count']
end

test "should export list if it's a workspace admin and number of results is not over the limit" do
u = create_user
t = create_team
create_team_user team: t, user: u, role: 'admin'
authenticate_with_user(u)

query = "mutation { exportList(input: { query: \"{}\" }) { success } }"
post :create, params: { query: query, team: t.slug }
assert_response :success
assert JSON.parse(@response.body)['data']['exportList']['success']
end

test "should not export list if it's not a workspace admin" do
u = create_user
t = create_team
create_team_user team: t, user: u, role: 'editor'
authenticate_with_user(u)

query = "mutation { exportList(input: { query: \"{}\" }) { success } }"
post :create, params: { query: query, team: t.slug }
assert_response :success
assert !JSON.parse(@response.body)['data']['exportList']['success']
end

test "should not export list if it's over the limit" do
u = create_user
t = create_team
create_team_user team: t, user: u, role: 'admin'
authenticate_with_user(u)

stub_configs({ 'export_csv_maximum_number_of_results' => -1 }) do
query = "mutation { exportList(input: { query: \"{}\" }) { success } }"
post :create, params: { query: query, team: t.slug }
assert_response :success
assert !JSON.parse(@response.body)['data']['exportList']['success']
end
end
end

0 comments on commit 4bd600e

Please sign in to comment.