au_pair 1.0.2 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -27,6 +27,9 @@ To set up tokens, create a configuration file in config/initializers/au_pair.rb
27
27
 
28
28
  end
29
29
 
30
+ (Note that for security reasons, you probably want to read in the tokens from an environment variable as opposed to storing
31
+ them in your source code.)
32
+
30
33
  Then in your application controller, or in individual controllers if you want to limit authentication to certain actions:
31
34
 
32
35
  class ApplicationController < ActionController::Base
@@ -1,6 +1,6 @@
1
1
  class AuPair::ApiConstraint
2
2
 
3
- attr_accessor :numeric_version
3
+ attr_accessor :numeric_version, :path_part
4
4
 
5
5
  def initialize(path_part)
6
6
  @path_part = path_part.downcase
@@ -11,14 +11,12 @@ class AuPair::ApiConstraint
11
11
  path_matches?(request) || header_matches?(request) || param_matches?(request)
12
12
  end
13
13
 
14
- private
15
-
16
14
  def path_matches?(request)
17
- ! (request.path =~ /\/#{@path_part}\//).nil?
15
+ request.path =~ /\/#{@path_part}\//i
18
16
  end
19
17
 
20
18
  def header_matches?(request)
21
- ! (request.headers['x-api-version'] =~ /#{@numeric_version}/).nil?
19
+ request.headers['x-api-version'] =~ /^[^\d]*#{numeric_version}$/
22
20
  end
23
21
 
24
22
  def param_matches?(request)
@@ -1,3 +1,3 @@
1
1
  module AuPair
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.4"
3
3
  end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe AuPair::ApiConstraint do
4
+
5
+ class Request
6
+ attr_accessor :path, :headers, :request_parameters, :query_parameters
7
+ end
8
+
9
+ let(:constraint) { AuPair::ApiConstraint.new("") }
10
+ let(:request) { Request.new }
11
+
12
+ before do
13
+ constraint.stub(:request) { request }
14
+ request.headers = {}
15
+ request.request_parameters = {}
16
+ request.query_parameters = {}
17
+ end
18
+
19
+ context 'path matching' do
20
+
21
+ it 'matches with mixed text and numbers' do
22
+ request.path = "/foo/version_1/bar"
23
+ constraint.path_part = "version_1"
24
+ expect(constraint.matches?(request)).to be_true
25
+ end
26
+
27
+ end
28
+
29
+ context 'param matching' do
30
+
31
+ it 'matches with numbers' do
32
+ request.request_parameters['api_version'] = "1"
33
+ constraint.numeric_version = 1
34
+ expect(constraint.matches?(request)).to be_true
35
+ end
36
+
37
+ end
38
+
39
+ context 'header matching' do
40
+
41
+ it 'matches with mixed text and numbers' do
42
+ request.headers['x-api-version'] = "version_1"
43
+ constraint.numeric_version = "1"
44
+ expect(constraint.matches?(request)).to be_true
45
+ end
46
+
47
+ it 'matches with numbers' do
48
+ request.headers['x-api-version'] = "11"
49
+ constraint.numeric_version = 11
50
+ expect(constraint.matches?(request)).to be_true
51
+ end
52
+
53
+ it 'with no false positives' do
54
+ request.headers['x-api-version'] = "1"
55
+ constraint.numeric_version = 21
56
+ expect(constraint.matches?(request)).to be_false
57
+ end
58
+
59
+ end
60
+
61
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: au_pair
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -80,6 +80,7 @@ files:
80
80
  - lib/au_pair/authentication_token.rb
81
81
  - lib/au_pair/version.rb
82
82
  - spec/.rspec
83
+ - spec/api_constraint_spec.rb
83
84
  - spec/authentication_token_spec.rb
84
85
  - spec/configuration_spec.rb
85
86
  - spec/spec_helper.rb
@@ -98,7 +99,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
99
  version: '0'
99
100
  segments:
100
101
  - 0
101
- hash: -3736725964287678646
102
+ hash: 1991329679706175588
102
103
  required_rubygems_version: !ruby/object:Gem::Requirement
103
104
  none: false
104
105
  requirements:
@@ -107,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
108
  version: '0'
108
109
  segments:
109
110
  - 0
110
- hash: -3736725964287678646
111
+ hash: 1991329679706175588
111
112
  requirements: []
112
113
  rubyforge_project:
113
114
  rubygems_version: 1.8.24
@@ -116,6 +117,7 @@ specification_version: 3
116
117
  summary: API versioning and token authentication
117
118
  test_files:
118
119
  - spec/.rspec
120
+ - spec/api_constraint_spec.rb
119
121
  - spec/authentication_token_spec.rb
120
122
  - spec/configuration_spec.rb
121
123
  - spec/spec_helper.rb