File: header_attributes_test.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 (91 lines) | stat: -rw-r--r-- 3,127 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
require "test_helper"

module SimpleOAuth
  # Tests for OAuth attribute handling.
  class HeaderAttributesTest < Minitest::Test
    include TestHelpers

    cover "SimpleOAuth::Header*"

    def test_attributes_prepends_keys_with_oauth
      # RFC 5849 Section 3.5.1 - parameter names prefixed with oauth_
      header = build_header_with_all_attribute_keys
      header.options[:ignore_extra_keys] = true

      assert(header.send(:attributes).keys.all? { |k| k.to_s =~ /^oauth_/ })
    end

    def test_attributes_has_only_symbol_keys
      header = build_header_with_all_attribute_keys
      header.options[:ignore_extra_keys] = true

      assert(header.send(:attributes).keys.all?(Symbol))
    end

    def test_attributes_excludes_invalid_keys
      header = build_header_with_all_attribute_keys
      header.options[:ignore_extra_keys] = true

      refute header.send(:attributes).key?(:oauth_other)
    end

    def test_attributes_preserves_values_for_valid_keys
      header = build_header_with_all_attribute_keys
      header.options[:ignore_extra_keys] = true

      assert(header.send(:attributes).all? { |k, v| k.to_s == "oauth_#{v.downcase}" })
    end

    def test_attributes_has_same_count_as_attribute_keys
      header = build_header_with_all_attribute_keys
      header.options[:ignore_extra_keys] = true

      assert_equal SimpleOAuth::Header::ATTRIBUTE_KEYS.size, header.send(:attributes).size
    end

    def test_attributes_raises_for_extra_keys
      header = build_header_with_all_attribute_keys
      error = assert_raises(SimpleOAuth::InvalidOptionsError) { header.send(:attributes) }
      assert_equal "Unknown option keys: :other", error.message
    end

    def test_attributes_does_not_raise_when_ignore_extra_keys_is_true
      header = build_header_with_all_attribute_keys
      header.options[:ignore_extra_keys] = true

      assert header.send(:attributes)
    end

    def test_attributes_does_not_raise_when_no_extra_keys
      header = SimpleOAuth::Header.new(:get, "https://photos.example.net/photos", {},
        consumer_key: RFC5849::CONSUMER_KEY)

      assert header.send(:attributes)
    end

    def test_attributes_raises_when_ignore_extra_keys_is_explicitly_false
      header = SimpleOAuth::Header.new(:get, "https://photos.example.net/photos", {},
        other: "OTHER")
      header.options[:ignore_extra_keys] = false

      assert_raises(SimpleOAuth::InvalidOptionsError) { header.send(:attributes) }
    end

    def test_attributes_error_message_includes_comma_separator_for_multiple_extra_keys
      header = SimpleOAuth::Header.new(:get, "https://photos.example.net/photos", {},
        extra1: "EXTRA1", extra2: "EXTRA2")

      error = assert_raises(SimpleOAuth::InvalidOptionsError) { header.send(:attributes) }
      assert_includes error.message, ", "
    end

    private

    def build_header_with_all_attribute_keys
      options = {}
      SimpleOAuth::Header::ATTRIBUTE_KEYS.each { |k| options[k] = k.to_s.upcase }
      options[:other] = "OTHER"
      SimpleOAuth::Header.new(:get, "https://photos.example.net/photos", {}, options)
    end
  end
end