<commit_before>require 'webmock/rspec'
require 'json'
require 'uri'
require 'support/key_helper'

module MockAPI
  METHOD_STATUS_DEFAULT = { get: 200, put: 200, patch: 200, post: 201, delete: 204 }
  include KeyHelper

  class RequestStub
    def initialize(http_method, route)
      @http_method = http_method
      @url = URI.join('https://example.com/', route)
      @status = METHOD_STATUS_DEFAULT[http_method]
      @request_headers = { 'Accept'=>'application/vnd.duse.1+json' }
      @response_headers = {}
      @body = nil
    end

    def body(body)
      @body = body
      @body = @body.to_json if @body.is_a?(Hash) || @body.is_a?(Array)
      self
    end

    def activate
      WebMock.stub_request(@http_method, @url).
        with(headers: @request_headers).
        to_return(status: @status, body: @body, headers: @response_headers)
    end
  end

  [:get, :put, :patch, :post, :delete].each do |http_method|
    define_method "stub_#{http_method.to_s}" do |route|
      RequestStub.new(http_method, route)
    end
  end

  def stub_get_users
    payload = [{
      'id' => 1,
      'username' => 'server',
      'email' => 'server@localhost',
      'url' => 'https://example.com/users/1'
    }, {
      'id' => 2,
      'username' => 'flower-pot',
      'email' => '<EMAIL>',
      'url' => 'https://example.com/users/2'
    }, {
      'id' => 3,
      'username' => 'adracus',
      'email' => '<EMAIL>',
      'url' => 'https://example.com/users/3'
    }]
    stub_get('/users').body(payload).activate
  end

  def stub_user_me_get
    stub_user_get2('me')
  end

  def stub_user_get2(id = 2)
    payload = {
      'id' => 2,
      'username' => 'flower-pot',
      'email' => '<EMAIL>',
      'public_key' => user_public_key.to_s,
      'url' => 'https://example.com/users/2'
    }

    stub_get("/users/#{id}").body(payload).activate
  end

  def stub_user_get1(id = 1)
    payload = {
      'id' => 1,
      'username' => 'server',
      'email' => 'server@localhost',
      'public_key' => server_public_key.to_s,
      'url' => 'https://example.com/users/1'
    }

    stub_get("/users/#{id}").body(payload).activate
  end

  def stub_server_user_get
    stub_user_get1('server')
  end

  def stub_get_other_user
    payload = {
      'id' => 3,
      'username' => 'adracus',
      'email' => '<EMAIL>',
      'public_key' => other_public_key.to_s,
      'url' => 'https://example.com/users/3'
    }

    stub_get('/users/3').body(payload).activate
  end

  def stub_get_secrets
    payload = [{
      'id' => 1,
      'title' => 'test',
      'url' => 'http://example.com/secrets/1'
    }]
    stub_get('/secrets').body(payload).activate
  end

  def stub_secret_get_broken_signature
    payload = {
      'id' => 2,
      'title' => 'test',
      'cipher_text' => "0aqOigsWK04MhJ5EHgM49Q==\n",
      'shares' => [
        { 'last_edited_by_id' => 2, 'signature' => "blabla", 'content' => "NQSRqhjkHyUyT00YDgQ6WayC0EwtdytQ/iZAUdc6mnXKNQc74hae3VIJqjUf\nNkq1Qus/dS7a5DMsKa+2F74p1UF3YbswNnZ7fc2VH+iz6hIdYbgCu5bmAuVF\nXFpXk7Nw9n4Cpvjg2x5jxBQ0S1BnYY8J+EBluzichoqub4EVl5yxTWipqiaZ\nKwR1gEEKi3h5w2uljOib62gFdxUnXLtMjDlIXJjN6Kl9idCiz3upBj+4KZFx\n4qBWpvU/6xQRgbbOwfHNzTDl9RSlWSYTXvJ1/7iXYfoSEOMGJSmlGSm/P3Y/\nVPoChK0lfJYanNW4Gu4qQf9d09E5d6NQcGZdN3PNIQ==\n"  },
        { 'last_edited_by_id' => 2, 'signature' => "blabla", 'content' => "<KEY>" }
      ],
      'users' => [{
        'id' => 1,
        'username' => 'server',
        'email' => 'server@localhost',
        'public_key' => server_public_key.to_s,
        'url' => 'https://example.com/users/1'
      }, {
        'id' => 2,
        'username' => 'flower-pot',
        'email' => '<EMAIL>',
        'public_key' => user_public_key.to_s,
        'url' => 'https://example.com/users/2'
      }],
      'url' => 'http://example.com/secrets/1'
    }

    stub_get('/secrets/2').body(payload).activate
  end

  def stub_secret_get
    payload = {
      'id' => 1,
      'title' => 'test',
      'cipher_text' => "0aqOigsWK04MhJ5EHgM49Q==\n",
      'shares' => [
        { 'last_edited_by_id' => 2, 'signature' => "dPMQbO9UzYFS4hY0U5iLjjVFTeGvDJemuEYVog6zClbLMbZG8pSaQ2MzNI1I\n0UsZJNXlyHP6NR/kTp0mpCmndrnA+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kJVgH1\nBLO6SC36b/lVsGfcvUwmO0vefGrSy2XykXVJdYPMf/LQsOukOs1rLVacg4B6\nf0KeKvLVf/HVYcKiY4L2rFIPvuWC+bUWxRThpiABSu1ZCH9qgcR24PPRnwYw\nsTT15OBHmBzhsP+b3NZI9Yh3OIzWLvDSjQmRuldryuUstPsgfWKd/xawPU4D\nJ8EUFGNltFqb+R4n2eGpNSVQCl/+o9gfMG6dq5CE5HRHwpKgXaybdy753lrF\nHWXS4g6apO4lpJS7kXdhHX7OTbA79GHj/ait9fuZLA==\n" }
      ],
      'users' => [{
        'id' => 1,
        'username' => 'server',
        'email' => 'server@localhost',
        'public_key' => server_public_key.to_s,
        'url' => 'https://example.com/users/1'
      }, {
        'id' => 2,
        'username' => 'flower-pot',
        'email' => '<EMAIL>',
        'public_key' => user_public_key.to_s,
        'url' => 'https://example.com/users/2'
      }],
      'url' => 'http://example.com/secrets/1'
    }

    stub_get('/secrets/1').body(payload).activate
  end

  def stub_secret_delete
    stub_delete('/secrets/1').activate
  end

  def stub_create_secret
    payload = {
      'id' => 1,
      'title' => 'test',
      'cipher_text' => "0aqOigsWK04MhJ5EHgM49Q==\n",
      'shares' => [
        { 'last_edited_by_id' => 2, 'signature' => "<KEY>", 'content' => "<KEY>"  },
        { 'last_edited_by_id' => 2, 'signature' => "<KEY>", 'content' => "gQUlhs+uPz93UN/Cpcd7x8ochDXWbfiC6IrP7ijCfxaa9kvsVnQddYkJVgH1\nBLO6SC36b/lVsGfcvUwmO0vefGrSy2XykXVJdYPMf/LQsOukOs1rLVacg4B6\nf0KeKvLVf/HVYcKiY4L2rFIPvuWC+bUWxRThpiABSu1ZCH9qgcR24PPRnwYw\nsTT15OBHmBzhsP+b3NZI9Yh3OIzWLvDSjQmRuldryuUstPsgfWKd/xawPU4D\nJ8EUFGNltFqb+R4n2eGpNSVQCl/+o9gfMG6dq5CE5HRHwpKgXaybdy753lrF\nHWXS4g6apO4lpJS7kXdhHX7OTbA79GHj/ait9fuZLA==\n" }
      ],
      'users' => [{
        'id' => 1,
        'username' => 'server',
        'email' => 'server@localhost',
        'public_key' => server_public_key.to_s,
        'url' => 'https://example.com/users/1'
      }, {
        'id' => 2,
        'username' => 'flower-pot',
        'email' => '<EMAIL>',
        'public_key' => user_public_key.to_s,
        'url' => 'https://example.com/users/2'
      }],
      'url' => 'http://example.com/secrets/1'
    }.to_json

    stub_post('/secrets').body(payload).activate
  end
end
<commit_msg>avoid duplication in mock api<commit_after>require 'webmock/rspec'
require 'json'
require 'uri'
require 'support/key_helper'

module MockAPI
  METHOD_STATUS_DEFAULT = { get: 200, put: 200, patch: 200, post: 201, delete: 204 }
  include KeyHelper

  class RequestStub
    def initialize(http_method, route)
      @http_method = http_method
      @url = URI.join('https://example.com/', route)
      @status = METHOD_STATUS_DEFAULT[http_method]
      @request_headers = { 'Accept'=>'application/vnd.duse.1+json' }
      @response_headers = {}
      @body = nil
    end

    def body(body)
      @body = body
      @body = @body.to_json if @body.is_a?(Hash) || @body.is_a?(Array)
      self
    end

    def activate
      WebMock.stub_request(@http_method, @url).
        with(headers: @request_headers).
        to_return(status: @status, body: @body, headers: @response_headers)
    end
  end

  [:get, :put, :patch, :post, :delete].each do |http_method|
    define_method "stub_#{http_method.to_s}" do |route|
      RequestStub.new(http_method, route)
    end
  end

  def secret_hash
    {
      'id' => 1,
      'title' => 'test',
      'cipher_text' => "0aqOigsWK04MhJ5EHgM49Q==\n",
      'shares' => [
        { 'last_edited_by_id' => 2, 'signature' => "<KEY>", 'content' => "<KEY>"  },
        { 'last_edited_by_id' => 2, 'signature' => "<KEY>", 'content' => "<KEY>" }
      ],
      'users' => [{
        'id' => 1,
        'username' => 'server',
        'email' => 'server@localhost',
        'public_key' => server_public_key.to_s,
        'url' => 'https://example.com/users/1'
      }, {
        'id' => 2,
        'username' => 'flower-pot',
        'email' => '<EMAIL>',
        'public_key' => user_public_key.to_s,
        'url' => 'https://example.com/users/2'
      }],
      'url' => 'http://example.com/secrets/1'
    }
  end

  def stub_get_users
    payload = [{
      'id' => 1,
      'username' => 'server',
      'email' => 'server@localhost',
      'url' => 'https://example.com/users/1'
    }, {
      'id' => 2,
      'username' => 'flower-pot',
      'email' => '<EMAIL>',
      'url' => 'https://example.com/users/2'
    }, {
      'id' => 3,
      'username' => 'adracus',
      'email' => '<EMAIL>',
      'url' => 'https://example.com/users/3'
    }]
    stub_get('/users').body(payload).activate
  end

  def stub_user_me_get
    stub_user_get2('me')
  end

  def stub_user_get2(id = 2)
    payload = {
      'id' => 2,
      'username' => 'flower-pot',
      'email' => '<EMAIL>',
      'public_key' => user_public_key.to_s,
      'url' => 'https://example.com/users/2'
    }

    stub_get("/users/#{id}").body(payload).activate
  end

  def stub_user_get1(id = 1)
    payload = {
      'id' => 1,
      'username' => 'server',
      'email' => 'server@localhost',
      'public_key' => server_public_key.to_s,
      'url' => 'https://example.com/users/1'
    }

    stub_get("/users/#{id}").body(payload).activate
  end

  def stub_server_user_get
    stub_user_get1('server')
  end

  def stub_get_other_user
    payload = {
      'id' => 3,
      'username' => 'adracus',
      'email' => '<EMAIL>',
      'public_key' => other_public_key.to_s,
      'url' => 'https://example.com/users/3'
    }

    stub_get('/users/3').body(payload).activate
  end

  def stub_get_secrets
    payload = [{
      'id' => 1,
      'title' => 'test',
      'url' => 'http://example.com/secrets/1'
    }]
    stub_get('/secrets').body(payload).activate
  end

  def stub_secret_get_broken_signature
    payload = secret_hash
    payload['shares'][0]['signature'] = 'blabla'
    payload['shares'][1]['signature'] = 'blabla'

    stub_get('/secrets/2').body(payload).activate
  end

  def stub_secret_get
    stub_get('/secrets/1').body(secret_hash).activate
  end

  def stub_secret_delete
    stub_delete('/secrets/1').activate
  end

  def stub_create_secret
    stub_post('/secrets').body(secret_hash).activate
  end
end

{'fraction_non_alphanumeric': 0.16325094555369865, 'fraction_numerical': 0.03958131761808426, 'mean_word_length': 3.128540305010893, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 173, 'https://': 22, 'lorem ipsum': 0, 'www.': 0, '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': '1172254', 'n_tokens_mistral': 5247, 'n_tokens_neox': 4965, 'n_words': 571}