From 2b78229d97751ca31a5a7a187936fb4c6660da15 Mon Sep 17 00:00:00 2001 From: Meghan Chua <17057809+malchua@users.noreply.github.com> Date: Thu, 30 May 2024 10:55:32 -0500 Subject: [PATCH 1/7] Add isPuid query --- app/graphql/types/query_type.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 28c797a456..084fd5cbe0 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -38,8 +38,19 @@ class QueryType < Types::BaseObject field :project_sample, Types::SampleType, null: true, resolver: Resolvers::ProjectSampleResolver, description: 'Find a sample within a project.' + field :is_puid, Boolean, null: false, description: 'Check if id is in puid format' do + argument :id, GraphQL::Types::ID, 'ID to compare to puid format' + end + def current_user context[:current_user] end + + def is_puid(id:) + Irida::PersistentUniqueId.valid_puid?(id, Sample) || + Irida::PersistentUniqueId.valid_puid?(id, Attachment) || + Irida::PersistentUniqueId.valid_puid?(id, Group) || + Irida::PersistentUniqueId.valid_puid?(id, Namespaces::ProjectNamespace) + end end end From 2b8293aeb439dddfa1e375f6dc091062de543fb2 Mon Sep 17 00:00:00 2001 From: Meghan Chua <17057809+malchua@users.noreply.github.com> Date: Mon, 3 Jun 2024 09:33:55 -0500 Subject: [PATCH 2/7] Change regex to match whole term only --- lib/irida/persistent_unique_id.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/irida/persistent_unique_id.rb b/lib/irida/persistent_unique_id.rb index e97923b9a9..f541e1b41e 100644 --- a/lib/irida/persistent_unique_id.rb +++ b/lib/irida/persistent_unique_id.rb @@ -36,7 +36,7 @@ def time_to_base32(time) end def valid_puid?(puid, object_class = nil) - re = /#{app_prefix}_#{object_class.model_prefix}_([A-Z]|[2-7]){10}/ + re = /\A#{app_prefix}_#{object_class.model_prefix}_([A-Z]|[2-7]){10}\z/ re.match?(puid) end end From 8f5f687d64359ec885c96f1dd99c14ed3157c922 Mon Sep 17 00:00:00 2001 From: Meghan Chua <17057809+malchua@users.noreply.github.com> Date: Mon, 3 Jun 2024 09:49:17 -0500 Subject: [PATCH 3/7] Move resolve method to its own resolver file --- app/graphql/resolvers/is_puid_resolver.rb | 31 +++++++++++++++++++++++ app/graphql/types/query_type.rb | 12 ++------- 2 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 app/graphql/resolvers/is_puid_resolver.rb diff --git a/app/graphql/resolvers/is_puid_resolver.rb b/app/graphql/resolvers/is_puid_resolver.rb new file mode 100644 index 0000000000..c7e16d8c16 --- /dev/null +++ b/app/graphql/resolvers/is_puid_resolver.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module Resolvers + # Project Resolver + class IsPuidResolver < BaseResolver + argument :id, GraphQL::Types::ID, + required: true, + description: 'ID to compare to puid format' + + type Boolean, null: false + + def resolve(id:) + id_sections = id.split('_') + model_prefix = id_sections[1] + return false unless id_sections.length == 3 + + case model_prefix + when 'SAM' + Irida::PersistentUniqueId.valid_puid?(id, Sample) + when 'ATT' + Irida::PersistentUniqueId.valid_puid?(id, Attachment) + when 'GRP' + Irida::PersistentUniqueId.valid_puid?(id, Group) + when 'PRJ' + Irida::PersistentUniqueId.valid_puid?(id, Namespaces::ProjectNamespace) + else + false + end + end + end +end diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 084fd5cbe0..ce67df70a9 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -38,19 +38,11 @@ class QueryType < Types::BaseObject field :project_sample, Types::SampleType, null: true, resolver: Resolvers::ProjectSampleResolver, description: 'Find a sample within a project.' - field :is_puid, Boolean, null: false, description: 'Check if id is in puid format' do - argument :id, GraphQL::Types::ID, 'ID to compare to puid format' - end + field :is_puid, Boolean, null: false, resolver: Resolvers::IsPuidResolver, + description: 'Check if id is in puid format' def current_user context[:current_user] end - - def is_puid(id:) - Irida::PersistentUniqueId.valid_puid?(id, Sample) || - Irida::PersistentUniqueId.valid_puid?(id, Attachment) || - Irida::PersistentUniqueId.valid_puid?(id, Group) || - Irida::PersistentUniqueId.valid_puid?(id, Namespaces::ProjectNamespace) - end end end From 9b6df094e7413531293a89e847e10cddf8479387 Mon Sep 17 00:00:00 2001 From: Meghan Chua <17057809+malchua@users.noreply.github.com> Date: Mon, 3 Jun 2024 09:50:51 -0500 Subject: [PATCH 4/7] Add tests for isPuid query --- test/graphql/is_puid_query_test.rb | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 test/graphql/is_puid_query_test.rb diff --git a/test/graphql/is_puid_query_test.rb b/test/graphql/is_puid_query_test.rb new file mode 100644 index 0000000000..0befa85276 --- /dev/null +++ b/test/graphql/is_puid_query_test.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +require 'test_helper' + +class PuidQueryTest < ActiveSupport::TestCase + PUID_QUERY_TRUE = <<~GRAPHQL + query { + isPuid(id: "INXT_SAM_23F24BP6A7") + } + GRAPHQL + + PUID_QUERY_FALSE = <<~GRAPHQL + query { + isPuid(id: "INXT_SAM_23D56SE3M8") + } + GRAPHQL + + def setup + @user = users(:john_doe) + end + + test 'is puid query should return true' do + user = users(:john_doe) + + result = IridaSchema.execute(PUID_QUERY_TRUE, context: { current_user: @user }, variables: {}) + + assert_nil result['errors'], 'should work and have no errors.' + + data = result['data'] + + assert_not_empty data, 'puid type should work' + assert data['isPuid'] + end + + test 'is puid query should return false' do + user = users(:john_doe) + + result = IridaSchema.execute(PUID_QUERY_FALSE, context: { current_user: @user }, variables: {}) + + assert_nil result['errors'], 'should work and have no errors.' + + data = result['data'] + + assert_not_empty data, 'puid type should work' + assert_not data['isPuid'] + end +end From cb4c85024aad0ba33e8140993b5ca25e4e2c990a Mon Sep 17 00:00:00 2001 From: Meghan Chua <17057809+malchua@users.noreply.github.com> Date: Tue, 4 Jun 2024 14:48:14 -0500 Subject: [PATCH 5/7] Fix rubocop offenses --- test/graphql/is_puid_query_test.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/graphql/is_puid_query_test.rb b/test/graphql/is_puid_query_test.rb index 0befa85276..462ad43da3 100644 --- a/test/graphql/is_puid_query_test.rb +++ b/test/graphql/is_puid_query_test.rb @@ -20,8 +20,6 @@ def setup end test 'is puid query should return true' do - user = users(:john_doe) - result = IridaSchema.execute(PUID_QUERY_TRUE, context: { current_user: @user }, variables: {}) assert_nil result['errors'], 'should work and have no errors.' @@ -33,8 +31,6 @@ def setup end test 'is puid query should return false' do - user = users(:john_doe) - result = IridaSchema.execute(PUID_QUERY_FALSE, context: { current_user: @user }, variables: {}) assert_nil result['errors'], 'should work and have no errors.' From a9732441fb0c22120787d91017d70b6ef1afb8a9 Mon Sep 17 00:00:00 2001 From: Meghan Chua <17057809+malchua@users.noreply.github.com> Date: Tue, 4 Jun 2024 14:49:02 -0500 Subject: [PATCH 6/7] Update schema.graphql --- app/graphql/schema.graphql | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/graphql/schema.graphql b/app/graphql/schema.graphql index 7bd1901167..8838248be6 100644 --- a/app/graphql/schema.graphql +++ b/app/graphql/schema.graphql @@ -714,6 +714,16 @@ type Query { last: Int ): GroupConnection! + """ + Boolean + """ + isPuid( + """ + ID to compare to puid format + """ + id: ID! + ): Boolean! + """ Find a namespace. """ From 2f28c23492beb1e5a99c0b7cf5d2878da51bd28a Mon Sep 17 00:00:00 2001 From: Meghan Chua <17057809+malchua@users.noreply.github.com> Date: Thu, 13 Jun 2024 14:04:29 -0500 Subject: [PATCH 7/7] Fix comment in resolver file --- app/graphql/resolvers/is_puid_resolver.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/graphql/resolvers/is_puid_resolver.rb b/app/graphql/resolvers/is_puid_resolver.rb index c7e16d8c16..af57bd4098 100644 --- a/app/graphql/resolvers/is_puid_resolver.rb +++ b/app/graphql/resolvers/is_puid_resolver.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Resolvers - # Project Resolver + # IsPuid Resolver class IsPuidResolver < BaseResolver argument :id, GraphQL::Types::ID, required: true,