Skip to content

Commit

Permalink
Adding missing tests and fixing related functionality.
Browse files Browse the repository at this point in the history
Reference: CV2-3765.
  • Loading branch information
caiosba committed Sep 21, 2023
1 parent 7039a67 commit f20cd6b
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ plugins:
config:
languages:
ruby:
mass_threshold: 30
mass_threshold: 36
bundler-audit:
enabled: true
exclude_patterns:
Expand Down
2 changes: 2 additions & 0 deletions app/graph/mutations/destroy_mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def define_behavior(subclass, mutation_target, parents_mapping)
subclass.define_method :resolve do |**inputs|
::GraphqlCrudOperations.destroy(inputs, context, parents_mapping)
end

type_class
end
end
end
Expand Down
4 changes: 1 addition & 3 deletions app/graph/mutations/duplicate_team_mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ def resolve(team_id:, custom_slug: nil, custom_name: nil)
user = User.current
ability = Ability.new(user)
team = GraphqlCrudOperations.load_if_can(Team, id, context)
if ability.cannot?(:duplicate, team)
raise I18n.t("team_clone.user_not_authorized")
end
raise I18n.t("team_clone.user_not_authorized") if ability.cannot?(:duplicate, team)
new_team =
Team.duplicate(
team,
Expand Down
6 changes: 2 additions & 4 deletions app/graph/mutations/graphql_crud_operations.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
class GraphqlCrudOperations
def self.safe_save(obj, attrs, parent_names = [])
if User.current.nil? && ApiKey.current.nil?
raise "This operation must be done by a signed-in user"
end
raise 'This operation must be done by a signed-in user' if User.current.nil? && ApiKey.current.nil?
attrs.each do |key, value|
method = key == "clientMutationId" ? "client_mutation_id=" : "#{key}="
obj.send(method, value) if obj.respond_to?(method)
Expand Down Expand Up @@ -96,7 +94,7 @@ def self.create(type, inputs, ctx, parents_mapping = {})
self.safe_save(obj, attrs, parents_mapping.keys)
end

def self.update_from_single_id(graphql_id, obj, inputs, ctx, parent_names)
def self.update_from_single_id(_graphql_id, obj, inputs, ctx, parent_names)
obj.file = ctx[:file] unless ctx[:file].blank?

attrs = inputs.keys.inject({}) do |memo, key|
Expand Down
2 changes: 1 addition & 1 deletion app/graph/types/default_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def inherited(subclass)
subclass.global_id_field :id
end
end

field :permissions, GraphQL::Types::String, null: true

def permissions
Expand Down
16 changes: 8 additions & 8 deletions app/graph/types/project_media_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,18 @@ def assignments(user_id:, annotation_type:)
end

DynamicAnnotation::AnnotationType.pluck(:annotation_type).each do |type|
field "dynamic_annotations_#{type}".to_sym, DynamicType.connection_type, null: true
field "dynamic_annotations_#{type}".to_sym, DynamicType.connection_type, null: true

define_method("dynamic_annotations_#{type}".to_sym) do |**_inputs|
object.get_annotations(type)
end
define_method("dynamic_annotations_#{type}".to_sym) do |**_inputs|
object.get_annotations(type)
end

field "dynamic_annotation_#{type}".to_sym, DynamicType, null: true
field "dynamic_annotation_#{type}".to_sym, DynamicType, null: true

define_method("dynamic_annotation_#{type}".to_sym) do |**_inputs|
object.get_dynamic_annotation(type)
end
define_method("dynamic_annotation_#{type}".to_sym) do |**_inputs|
object.get_dynamic_annotation(type)
end
end

field :suggested_similar_relationships, RelationshipType.connection_type, null: true

Expand Down
10 changes: 1 addition & 9 deletions app/graph/types/public_team_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ def spam_count
private

def archived_count(team)
team.private &&
(!User.current ||
(!User.current.is_admin &&
TeamUser
.where(team_id: team.id, user_id: User.current.id)
.last
.nil?
)
)
team.private && (!User.current || (!User.current.is_admin && TeamUser.where(team_id: team.id, user_id: User.current.id).last.nil?))
end
end
2 changes: 1 addition & 1 deletion app/graph/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def me
argument :random, GraphQL::Types::String, required: false
end

def team(id: nil, slug: nil, random: nil)
def team(id: nil, slug: nil, _random: nil)
tid = id.to_i
if !slug.blank?
team = Team.where(slug: slug).first
Expand Down
2 changes: 1 addition & 1 deletion app/graph/types/source_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def account_sources
field :medias, ProjectMediaType.connection_type, null: true

def medias
object.media
object.medias
end

field :medias_count, GraphQL::Types::Int, null: true
Expand Down
18 changes: 9 additions & 9 deletions app/helpers/search_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ def get_from_and_to_values(values, tz)
if ['less_than', 'more_than'].include?(condition_type)
period = values.dig('period').to_i
period_date = case values.dig('period_type').downcase
when 'd'
Time.now - period.day
when 'w'
Time.now - period.week
when 'm'
Time.now - period.month
when 'y'
Time.now - period.year
end
when 'd'
Time.now - period.day
when 'w'
Time.now - period.week
when 'm'
Time.now - period.month
when 'y'
Time.now - period.year
end
condition_date = period_date.blank? ? nil : format_time_with_timezone(period_date.to_s, tz)
if condition_type == 'less_than'
from = condition_date
Expand Down
3 changes: 3 additions & 0 deletions app/models/annotations/flag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Flag < ApplicationRecord
include AnnotationBase
end
9 changes: 5 additions & 4 deletions test/controllers/graphql_controller_10_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,22 @@ def setup
assert_equal 'foo', data['tag_texts']['edges'][0]['node']['text']
end

test "should get team team_users" do
test "should get team users and bots" do
t = create_team
u = create_user
u2 = create_user
u3 = create_bot_user team: t
create_team_user team: t, user: u, role: 'admin'
create_team_user team: t, user: u2
authenticate_with_user(u)
query = "query GetById { team(id: \"#{t.id}\") { team_users { edges { node { user { dbid } } } } } }"
query = "query GetById { team(id: \"#{t.id}\") { team_users { edges { node { user { dbid, get_send_email_notifications, get_send_successful_login_notifications, get_send_failed_login_notifications, source { medias(first: 1) { edges { node { id } } } }, annotations(first: 1) { edges { node { id } } }, team_users(first: 1) { edges { node { id } } }, bot { get_description, get_role, get_version, get_source_code_url } } } } } } }"
post :create, params: { query: query, team: t.slug }

assert_response :success
data = JSON.parse(@response.body)['data']['team']['team_users']['edges']
ids = data.collect{ |i| i['node']['user']['dbid'] }
assert_equal 2, data.size
assert_equal [u.id, u2.id], ids.sort
assert_equal 3, data.size
assert_equal [u.id, u2.id, u3.id], ids.sort
end

test "should get team tasks" do
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/graphql_controller_5_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def setup
pm = create_project_media team: t
authenticate_with_user(u)
# verify create
query = 'mutation create { createDynamic(input: { annotation_type: "flag", clientMutationId: "1", annotated_type: "ProjectMedia", annotated_id: "' + pm.id.to_s + '", set_fields: "{\"flags\":{\"adult\":3,\"spoof\":2,\"medical\":1,\"violence\":3,\"racy\":4,\"spam\":0},\"show_cover\":false}" }) { dynamic { dbid } } }'
query = 'mutation create { createDynamic(input: { annotation_type: "flag", clientMutationId: "1", annotated_type: "ProjectMedia", annotated_id: "' + pm.id.to_s + '", set_fields: "{\"flags\":{\"adult\":3,\"spoof\":2,\"medical\":1,\"violence\":3,\"racy\":4,\"spam\":0},\"show_cover\":false}" }) { dynamic { dbid, id } } }'
post :create, params: { query: query, team: t.slug }
assert_response :success
assert JSON.parse(@response.body).dig('errors').blank?
Expand Down
12 changes: 11 additions & 1 deletion test/controllers/graphql_controller_8_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def setup
t = create_team private: true
create_team_user(user: u, team: t)
f = create_feed
query = "query { team(slug: \"#{t.slug}\") { feed(dbid: #{f.id}) { current_feed_team { dbid } } } }"
query = "query { team(slug: \"#{t.slug}\") { feed(dbid: #{f.id}) { current_feed_team { dbid, requests_filters } } } }"

post :create, params: { query: query, team: t.slug }
assert_nil JSON.parse(@response.body).dig('data', 'team', 'feed')
Expand Down Expand Up @@ -537,6 +537,7 @@ def setup
id
trash_count
unconfirmed_count
spam_count
}
search {
id
Expand Down Expand Up @@ -870,4 +871,13 @@ def setup
assert_response :success
assert_nil JSON.parse(@response.body)['data']['dynamic_annotation_field']
end

test "should get team settings fields" do
u = create_user is_admin: true
authenticate_with_user(u)
t = create_team
fields = %w(get_slack_notifications_enabled get_slack_webhook get_embed_whitelist get_report_design_image_template get_status_target_turnaround get_rules get_languages get_language get_report get_data_report_url get_outgoing_urls_utm_code get_shorten_outgoing_urls)
post :create, params: { query: "query Team { team { join_requests(first: 10) { edges { node { id } } }, #{fields.join(', ')} } }", team: t.slug }
assert_response :success
end
end
17 changes: 17 additions & 0 deletions test/controllers/graphql_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ def setup
assert_graphql_create('project_media', { media_id: m.id, project_id: p.id })
end

test "should read project media flag and source" do
u = create_user is_admin: true
authenticate_with_user(u)
pm = create_project_media
create_flag annotated: pm
query = "query GetById { project_media(ids: \"#{pm.id},nil,#{pm.team_id}\") { source { id }, annotation(annotation_type: \"flag\") { project_media { id } }, annotations(annotation_type: \"flag\") { edges { node { ... on Flag { id } } } } } }"
post :create, params: { query: query, team: pm.team.slug }
assert_response :success
end

test "should read project medias" do
authenticate_with_user
p = create_project team: @team
Expand Down Expand Up @@ -906,4 +916,11 @@ def setup
assert_response :success
end

test "should have a base interface for GraphQL types" do
assert_nothing_raised do
class TestType < BaseObject
implements BaseInterface
end
end
end
end
6 changes: 6 additions & 0 deletions test/models/project_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -680,4 +680,10 @@ def setup
end
end
end

test "should be inactive if team is inactive" do
t = create_team inactive: true
p = create_project team: t
assert p.inactive
end
end

0 comments on commit f20cd6b

Please sign in to comment.