File: test_helper.rb

package info (click to toggle)
ruby-simple-oauth 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 372 kB
  • sloc: ruby: 1,722; makefile: 4; sh: 4
file content (114 lines) | stat: -rw-r--r-- 3,910 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)

require "minitest/autorun"
require "simple_oauth"

# Define a no-op cover method for regular test runs (mutant-minitest defines this when running mutations)
Minitest::Test.define_singleton_method(:cover) { |*| nil } unless Minitest::Test.respond_to?(:cover)

module TestHelpers
  PRIVATE_KEY_PATH = File.expand_path("fixtures/rsa-private-key", __dir__)

  def rsa_private_key
    @rsa_private_key ||= File.read(PRIVATE_KEY_PATH)
  end

  # Factory method to build a Header with common defaults
  def build_header(method = :get, url = RFC5849::PHOTOS_URL, params = {}, **options)
    defaults = {
      consumer_key: RFC5849::CONSUMER_KEY,
      consumer_secret: RFC5849::CONSUMER_SECRET
    }
    SimpleOAuth::Header.new(method, url, params, defaults.merge(options))
  end

  # Factory method to build a Header with fixed nonce/timestamp for deterministic tests
  def build_header_with_fixed_credentials(method = :get, url = RFC5849::PHOTOS_URL, params = {}, **options)
    defaults = {
      consumer_key: RFC5849::CONSUMER_KEY,
      consumer_secret: RFC5849::CONSUMER_SECRET,
      nonce: "chapoH",
      timestamp: "137131202"
    }
    SimpleOAuth::Header.new(method, url, params, defaults.merge(options))
  end

  # RFC 5849 Example Constants
  # See https://www.rfc-editor.org/rfc/rfc5849 for complete examples
  module RFC5849
    # Section 1.2 - Printer/Photos example endpoints
    PHOTOS_HOST = "photos.example.net".freeze
    PHOTOS_BASE_URL = "https://#{PHOTOS_HOST}".freeze
    PHOTOS_URL = "#{PHOTOS_BASE_URL}/photos".freeze
    PRINTER_HOST = "printer.example.com".freeze
    PRINTER_CALLBACK = "http://#{PRINTER_HOST}/ready".freeze

    # Section 1.2 - Client credentials (printer application)
    CONSUMER_KEY = "dpf43f3p2l4k3l03".freeze
    CONSUMER_SECRET = "kd94hf93k423kf44".freeze

    # Section 1.2 - Temporary credentials
    TEMP_TOKEN = "hh5s93j4hdidpola".freeze
    TEMP_TOKEN_SECRET = "hdhd0244k9j7ao03".freeze

    # Section 1.2 - Token credentials
    TOKEN = "nnch734d00sl2jdk".freeze
    TOKEN_SECRET = "pfkkdhi9sl3r4s00".freeze

    # Section 1.2 - Verifier
    VERIFIER = "hfdp7dh39dks9884".freeze

    # Section 3.1 / 3.4.1 - Signature example
    module SignatureExample
      HOST = "example.com".freeze
      BASE_URL = "http://#{HOST}".freeze
      CONSUMER_KEY = "9djdj82h48djs9d2".freeze
      CONSUMER_SECRET = "j49sk3j29djd".freeze
      TOKEN = "kkk9d7dh3k39sjv7".freeze
      TOKEN_SECRET = "dh893hdasih9".freeze
      TIMESTAMP = "137131201".freeze
      NONCE = "7d8f3e4a".freeze
    end

    # Section 3.5.1 - Authorization header example
    module HeaderExample
      CONSUMER_KEY = "0685bd9184jfhq22".freeze
      CONSUMER_SECRET = "kd94hf93k423kf44".freeze
      TOKEN = "ad180jjd733klru7".freeze
      TOKEN_SECRET = "pfkkdhi9sl3r4s00".freeze
      TIMESTAMP = "137131200".freeze
      NONCE = "4572616e48616d6d65724c61686176".freeze

      # Complete options hash for tests
      OPTIONS = {
        consumer_key: CONSUMER_KEY,
        consumer_secret: CONSUMER_SECRET,
        token: TOKEN,
        token_secret: TOKEN_SECRET,
        nonce: NONCE,
        timestamp: TIMESTAMP
      }.freeze
    end

    # Complete options hash for printer/photos example (Section 1.2)
    PHOTOS_OPTIONS = {
      consumer_key: CONSUMER_KEY,
      consumer_secret: CONSUMER_SECRET,
      nonce: "wIjqoS",
      timestamp: "137131200",
      callback: PRINTER_CALLBACK
    }.freeze

    # Section 2.1 - PLAINTEXT example
    module PlaintextExample
      HOST = "server.example.com".freeze
      BASE_URL = "http://#{HOST}".freeze
      CONSUMER_KEY = "jd83jd92dhsh93js".freeze
      CONSUMER_SECRET = "ja893SD9".freeze
      CALLBACK = "http://client.example.net/cb?x=1".freeze
      TOKEN = "hdk48Djdsa".freeze
      TOKEN_SECRET = "xyz4992k83j47x0b".freeze
      VERIFIER = "473f82d3".freeze
    end
  end
end