Class: TestOAuthRequest

Inherits:
Test::Unit::TestCase
  • Object
show all
Defined in:
lib/lti2_commons/test/test_oauth_request.rb

Constant Summary collapse

ARGV =
['', "--name", "test_duplicate_nonce"]

Instance Method Summary collapse

Instance Method Details

#create_stale_request(minutes_stale) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 32

def create_stale_request(minutes_stale)
  now = Time::now
  stale_time = now + (minutes_stale*60.0)
  # puts "Now: #{now}  Stale_time: #{stale_time}]"
  params = @params.dup
  params['oauth_timestamp'] = stale_time.to_i
  request = Lti2Commons::Signer.create_signed_request @launch_url, @http_method, @consumer_key, @consumer_secret, params
  # dump_oauth_parameters request, "stale create"
  request
end

#dump_oauth_parameters(request, label = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 43

def dump_oauth_parameters(request, label=nil)
  puts ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
  if label
    puts "*** #{label}"
  end
  puts "non_oauth_parameters: #{request.non_oauth_parameters.inspect}"
  puts "oauth_header: #{request.oauth_header}"
  puts "oauth_parameters: #{request.oauth_parameters.inspect}"
  puts "signature_base_string: #{request.signature_base_string}"
  puts "signed? #{request.signed?}"
  puts "signed_uri: #{request.signed_uri}"
  puts "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
end

#setupObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 18

def setup
  @launch_url = 'http://localhost:3000/tenants/3'
  # @launch_url = 'http://vst-bc.com/tenants/3/books'
  @http_method = 'post'
  @consumer_key = '12345'
  @consumer_secret = 'secret'
  @params = {'oauth_consumer_key'=>'12345', 'oauth_signature_method'=> "HMAC-SHA1", 'user_id'=>'jt'}
  @oauth_request = OAuth::OAuthProxy::OAuthRequest.new \
    "method" => @http_method,
    "uri" => @launch_url, 
    "parameters" => @params
  @nonce_cache = Lti2Commons::Cache.new :ttl => 300
end

#test_break_signatureObject



101
102
103
104
105
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 101

def test_break_signature
  request = Lti2Commons::Signer.create_signed_request @launch_url, @http_method, @consumer_key, @consumer_secret, @params
  request.parameters['oauth_signature'] = "asdf"
  assert_equal false, (request.verify_signature? @consumer_secret, @nonce_cache)
end

#test_create_signed_request_getObject



75
76
77
78
79
80
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 75

def test_create_signed_request_get
  request = Lti2Commons::Signer.create_signed_request @launch_url, 'get', @consumer_key, @consumer_secret, @params
  assert_not_nil request
  signature_base_string = request.signature_base_string
  assert_equal "GET&http%3A%2F%2Flocalhost%3A3000%2Ftenants%2F3&basiclti_submit%3DPress%2520t", signature_base_string[0..76]
end

#test_create_signed_request_putObject



82
83
84
85
86
87
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 82

def test_create_signed_request_put
  request = Lti2Commons::Signer.create_signed_request @launch_url, 'put', @consumer_key, @consumer_secret, @params
  assert_not_nil request
  signature_base_string = request.signature_base_string
  assert_equal "PUT&http%3A%2F%2Flocalhost%3A3000%2Ftenants%2F3&basiclti_submit%3DPress%2520t", signature_base_string[0..76]
end

#test_create_signed_request_simple_postObject



68
69
70
71
72
73
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 68

def test_create_signed_request_simple_post
  request = Lti2Commons::Signer.create_signed_request @launch_url, @http_method, @consumer_key, @consumer_secret, @params
  assert_not_nil request
  signature_base_string = request.signature_base_string
  assert_equal "POST&http%3A%2F%2Flocalhost%3A3000%2Ftenants%2F3&basiclti_submit%3DPress%2520t", signature_base_string[0..77]
end

#test_duplicate_nonceObject



132
133
134
135
136
137
138
139
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 132

def test_duplicate_nonce
  params = @params.dup
  params['oauth_nonce'] = "1234"
  request = Lti2Commons::Signer.create_signed_request @launch_url, @http_method, @consumer_key, @consumer_secret, params
  assert_equal true, (request.verify_signature? @consumer_secret, @nonce_cache)
  request = Lti2Commons::Signer.create_signed_request @launch_url, @http_method, @consumer_key, @consumer_secret, params
  assert_equal false, (request.verify_signature? @consumer_secret, @nonce_cache)
end

#test_exception_on_signature_errorObject



107
108
109
110
111
112
113
114
115
116
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 107

def test_exception_on_signature_error
  request = Lti2Commons::Signer.create_signed_request @launch_url, @http_method, @consumer_key, @consumer_secret, @params
  request.parameters['oauth_signature'] = "asdf"
  begin
    request.verify_signature? @consumer_secret, @nonce_cache, false
    fail "Exception expected here"
  rescue
    # expected
  end
end

#test_lti_message_bodyObject



96
97
98
99
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 96

def test_lti_message_body
  assert_not_nil MessageSupport.create_lti_message_body @launch_url, @params
  puts MessageSupport.create_lti_message_body @launch_url, @params
end

#test_lti_message_body_from_signed_requestObject



89
90
91
92
93
94
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 89

def test_lti_message_body_from_signed_request
  request = Signer.create_signed_request @launch_url, 'put', @consumer_key, @consumer_secret, @params
  assert_not_nil request
  assert_not_nil MessageSupport.create_lti_message_body_from_signed_request request
  puts MessageSupport.create_lti_message_body_from_signed_request request
end

#test_oauth_requestObject



57
58
59
60
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 57

def test_oauth_request
  request = Signer.create_signed_request @launch_url, @http_method, @consumer_key, @consumer_secret, @params
  dump_oauth_parameters(request, 'postsigning')
end

#test_timeout_errorObject



125
126
127
128
129
130
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 125

def test_timeout_error
  request = create_stale_request 10
  assert_equal false, (request.verify_signature? @consumer_secret, @nonce_cache)
  request = create_stale_request(-10)
  assert_equal false, (request.verify_signature? @consumer_secret, @nonce_cache)
end

#test_timeout_within_marginObject



118
119
120
121
122
123
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 118

def test_timeout_within_margin
  request = create_stale_request 2
  assert_equal true, (request.verify_signature? @consumer_secret, @nonce_cache)
  request = create_stale_request(-2)
  assert_equal true, (request.verify_signature? @consumer_secret, @nonce_cache)
end

#test_valid_requestObject



62
63
64
65
66
# File 'lib/lti2_commons/test/test_oauth_request.rb', line 62

def test_valid_request
  request = Signer.create_signed_request @launch_url, @http_method, @consumer_key, @consumer_secret, @params
  assert_not_nil request
  assert_equal true, request.verify_signature?(@consumer_secret, @nonce_cache)
end