<commit_before>module TentValidator

  class SubscriptionValidator < TentValidator::Spec
    require 'tent-validator/validators/support/post_generators'
    include Support::PostGenerators

    require 'tent-validator/validators/support/relationship_importer'
    include Support::RelationshipImporter

    describe "Create Subscription" do
      context "when no existing relationship" do

        set(:user, TentD::Model::User.generate)
        set(:subscription_type, %(https://tent.io/types/status/v0#))
        set(:subscription_type_base, %(https://tent.io/types/status/v0))
        set(:subscription_post_type, TentClient::TentType.new("https://tent.io/types/subscription/v0##{get(:subscription_type_base)}").to_s)

        ##
        # Setup asyc request expectation for relationship#initial post
        expect_async_request(
          :method => "PUT",
          :url => %r{\A#{Regexp.escape(get(:user).entity)}},
          :path => %r{\A/posts/#{Regexp.escape(URI.encode_www_form_component(TentValidator.remote_entity_uri))}/[^/]+\Z}
        ) do
          expect_schema(:post)
          expect_headers(
            'Content-Type' => %r{\brel=['"]#{Regexp.escape("https://tent.io/rels/notification")}['"]}
          )
          expect_headers(
            'Content-Type' => %r{\A#{Regexp.escape(TentD::API::POST_CONTENT_TYPE % %(https://tent.io/types/relationship/v0#initial))}}
          )
        end.expect_response(:status => 200, :schema => :data) do
          expect_schema(:post, '/post')
          expect_headers(
            'Content-Type' => TentD::API::POST_CONTENT_TYPE % %(https://tent.io/types/relationship/v0#initial)
          )
        end

        ##
        # Setup asyc request expectation for subscription
        expect_async_request(
          :method => "PUT",
          :url => %r{\A#{Regexp.escape(get(:user).entity)}},
          :path => %r{\A/posts/#{Regexp.escape(URI.encode_www_form_component(TentValidator.remote_entity_uri))}/[^/]+\Z}
        ) do
          expect_schema(:post)
          expect_headers(
            'Content-Type' => %r{\brel=['"]#{Regexp.escape("https://tent.io/rels/notification")}['"]}
          )
          expect_headers(
            'Content-Type' => %r{\A#{Regexp.escape(TentD::API::POST_CONTENT_TYPE % get(:subscription_post_type))}}
          )
        end.expect_response(:status => 200, :schema => :data) do
          expect_schema(:post, '/post')
          expect_headers(
            'Content-Type' => TentD::API::POST_CONTENT_TYPE % get(:subscription_post_type)
          )
        end

        ##
        # Create subscription on remote server (should trigger relationship init and send us the subscription)
        expect_response(:status => 200, :schema => :data) do
          data = {
            :type => get(:subscription_post_type),
            :mentions => [{ 'entity' => get(:user).entity }],
            :content => {
              :type => get(:subscription_type)
            },
            :permissions => {
              :entities => [get(:user).entity]
            }
          }
          clients(:app_auth).post.create(data)
        end

      end
    end

    describe "Import Subscription"  do
      setup do
        set(:type, "https://example.org/types/status-#{TentD::Utils.timestamp}/v#{rand(1000)}#")
        set(:notification_types, [get(:type)])
        set(:read_types, get(:notification_types))

        user = TentD::Model::User.generate
        set(:user, user)
        set(:entity, user.entity)
        set(:meta_post, user.meta_post.as_json)
      end<commit_msg>Update create subscription validation to create the same subscription for multiple entities<commit_after>module TentValidator

  class SubscriptionValidator < TentValidator::Spec
    require 'tent-validator/validators/support/post_generators'
    include Support::PostGenerators

    require 'tent-validator/validators/support/relationship_importer'
    include Support::RelationshipImporter

    describe "Create Subscription" do
      context "when no existing relationship" do

        shared_example :create_relationship_and_subscription do
          ##
          # Setup asyc request expectation for relationship#initial post
          expect_async_request(
            :method => "PUT",
            :url => %r{\A#{Regexp.escape(get(:user).entity)}},
            :path => %r{\A/posts/#{Regexp.escape(URI.encode_www_form_component(TentValidator.remote_entity_uri))}/[^/]+\Z}
          ) do
            expect_schema(:post)
            expect_headers(
              'Content-Type' => %r{\brel=['"]#{Regexp.escape("https://tent.io/rels/notification")}['"]}
            )
            expect_headers(
              'Content-Type' => %r{\A#{Regexp.escape(TentD::API::POST_CONTENT_TYPE % %(https://tent.io/types/relationship/v0#initial))}}
            )
          end.expect_response(:status => 200, :schema => :data) do
            expect_schema(:post, '/post')
            expect_headers(
              'Content-Type' => TentD::API::POST_CONTENT_TYPE % %(https://tent.io/types/relationship/v0#initial)
            )
          end

          ##
          # Setup asyc request expectation for subscription
          expect_async_request(
            :method => "PUT",
            :url => %r{\A#{Regexp.escape(get(:user).entity)}},
            :path => %r{\A/posts/#{Regexp.escape(URI.encode_www_form_component(TentValidator.remote_entity_uri))}/[^/]+\Z}
          ) do
            expect_schema(:post)
            expect_headers(
              'Content-Type' => %r{\brel=['"]#{Regexp.escape("https://tent.io/rels/notification")}['"]}
            )
            expect_headers(
              'Content-Type' => %r{\A#{Regexp.escape(TentD::API::POST_CONTENT_TYPE % get(:subscription_post_type))}}
            )
          end.expect_response(:status => 200, :schema => :data) do
            expect_schema(:post, '/post')
            expect_headers(
              'Content-Type' => TentD::API::POST_CONTENT_TYPE % get(:subscription_post_type)
            )
          end

          ##
          # Create subscription on remote server (should trigger relationship init and send us the subscription)
          expect_response(:status => 200, :schema => :data) do
            data = {
              :type => get(:subscription_post_type),
              :mentions => [{ 'entity' => get(:user).entity }],
              :content => {
                :type => get(:subscription_type)
              },
              :permissions => {
                :entities => [get(:user).entity]
              }
            }
            clients(:app_auth).post.create(data)
          end
        end

        set(:subscription_type, %(https://tent.io/types/status/v0#))
        set(:subscription_type_base, %(https://tent.io/types/status/v0))
        set(:subscription_post_type, TentClient::TentType.new("https://tent.io/types/subscription/v0##{get(:subscription_type_base)}").to_s)

        context "entity A" do
          set(:user, TentD::Model::User.generate)
          behaves_as(:create_relationship_and_subscription)
        end

        context "entity B" do
          set(:user, TentD::Model::User.generate)
          behaves_as(:create_relationship_and_subscription)
        end
      end
    end

    describe "Import Subscription"  do
      setup do
        set(:type, "https://example.org/types/status-#{TentD::Utils.timestamp}/v#{rand(1000)}#")
        set(:notification_types, [get(:type)])
        set(:read_types, get(:notification_types))

        user = TentD::Model::User.generate
        set(:user, user)
        set(:entity, user.entity)
        set(:meta_post, user.meta_post.as_json)
      end
{'fraction_non_alphanumeric': 0.1452265900483218, 'fraction_numerical': 0.004701580253362936, 'mean_word_length': 2.3825088339222615, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 16, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3867957', 'n_tokens_mistral': 2271, 'n_tokens_neox': 2251, 'n_words': 340}