agile-proxy-jruby 0.1.25-jruby

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +7 -0
  2. data/.bowerrc +3 -0
  3. data/.gitignore +10 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +36 -0
  6. data/.travis.yml +10 -0
  7. data/Gemfile +4 -0
  8. data/Guardfile +20 -0
  9. data/LICENSE +22 -0
  10. data/README.md +131 -0
  11. data/Rakefile +15 -0
  12. data/agile-proxy.gemspec +60 -0
  13. data/assets/index.html +39 -0
  14. data/assets/ui/app/AgileProxyApi.js +31 -0
  15. data/assets/ui/app/app.js +1 -0
  16. data/assets/ui/app/controller/Stubs.js +64 -0
  17. data/assets/ui/app/controller/main.js +12 -0
  18. data/assets/ui/app/directive/AppEnhancedFormElement.js +21 -0
  19. data/assets/ui/app/directive/AppFor.js +16 -0
  20. data/assets/ui/app/directive/AppResponseEditor.js +54 -0
  21. data/assets/ui/app/model/RequestSpec.js +6 -0
  22. data/assets/ui/app/routes.js +11 -0
  23. data/assets/ui/app/service/Dialog.js +49 -0
  24. data/assets/ui/app/service/DomId.js +10 -0
  25. data/assets/ui/app/service/Error.js +7 -0
  26. data/assets/ui/app/service/Stub.js +36 -0
  27. data/assets/ui/app/view/404.html +2 -0
  28. data/assets/ui/app/view/dialog/error.html +10 -0
  29. data/assets/ui/app/view/dialog/yesNo.html +8 -0
  30. data/assets/ui/app/view/responses/editForm.html +78 -0
  31. data/assets/ui/app/view/status.html +1 -0
  32. data/assets/ui/app/view/stubs.html +19 -0
  33. data/assets/ui/app/view/stubs/edit.html +58 -0
  34. data/assets/ui/css/main.css +3 -0
  35. data/bin/agile_proxy +4 -0
  36. data/bower.json +27 -0
  37. data/config.yml +6 -0
  38. data/db.yml +10 -0
  39. data/db/migrations/20140818110800_create_users.rb +9 -0
  40. data/db/migrations/20140818134700_create_applications.rb +10 -0
  41. data/db/migrations/20140818135200_create_request_specs.rb +13 -0
  42. data/db/migrations/20140821115300_create_responses.rb +14 -0
  43. data/db/migrations/20140823082900_add_method_to_request_specs.rb +7 -0
  44. data/db/migrations/20140823083900_rename_request_spec_columns.rb +8 -0
  45. data/db/migrations/20141031072100_add_url_type_to_request_specs.rb +8 -0
  46. data/db/migrations/20141105125600_add_conditions_to_request_specs.rb +7 -0
  47. data/db/migrations/20141106083100_add_username_and_password_to_applications.rb +8 -0
  48. data/db/migrations/20141119143800_add_record_to_applications.rb +7 -0
  49. data/db/migrations/20141119174300_create_recordings.rb +18 -0
  50. data/db/migrations/20150221152500_add_record_requests_to_request_specs.rb +7 -0
  51. data/db/schema.rb +78 -0
  52. data/db/seed.rb +26 -0
  53. data/echo_server.rb +19 -0
  54. data/examples/README.md +1 -0
  55. data/examples/facebook_api.html +59 -0
  56. data/examples/tumblr_api.html +22 -0
  57. data/lib/agile_proxy.rb +8 -0
  58. data/lib/agile_proxy/api/applications.rb +77 -0
  59. data/lib/agile_proxy/api/recordings.rb +52 -0
  60. data/lib/agile_proxy/api/request_spec_recordings.rb +52 -0
  61. data/lib/agile_proxy/api/request_specs.rb +86 -0
  62. data/lib/agile_proxy/api/root.rb +45 -0
  63. data/lib/agile_proxy/cli.rb +116 -0
  64. data/lib/agile_proxy/config.rb +66 -0
  65. data/lib/agile_proxy/handlers/handler.rb +43 -0
  66. data/lib/agile_proxy/handlers/proxy_handler.rb +111 -0
  67. data/lib/agile_proxy/handlers/request_handler.rb +75 -0
  68. data/lib/agile_proxy/handlers/stub_handler.rb +146 -0
  69. data/lib/agile_proxy/mitm.crt +22 -0
  70. data/lib/agile_proxy/mitm.key +27 -0
  71. data/lib/agile_proxy/model/application.rb +20 -0
  72. data/lib/agile_proxy/model/recording.rb +17 -0
  73. data/lib/agile_proxy/model/request_spec.rb +48 -0
  74. data/lib/agile_proxy/model/response.rb +51 -0
  75. data/lib/agile_proxy/model/user.rb +17 -0
  76. data/lib/agile_proxy/proxy_connection.rb +112 -0
  77. data/lib/agile_proxy/rack/get_only_cache.rb +30 -0
  78. data/lib/agile_proxy/route.rb +106 -0
  79. data/lib/agile_proxy/router.rb +99 -0
  80. data/lib/agile_proxy/server.rb +119 -0
  81. data/lib/agile_proxy/servers/api.rb +40 -0
  82. data/lib/agile_proxy/servers/request_spec.rb +40 -0
  83. data/lib/agile_proxy/servers/request_spec_direct.rb +35 -0
  84. data/lib/agile_proxy/version.rb +6 -0
  85. data/load_proxy.js +39 -0
  86. data/log/.gitkeep +0 -0
  87. data/spec/common_helper.rb +32 -0
  88. data/spec/fixtures/example_static_file.html +1 -0
  89. data/spec/fixtures/test-server.crt +15 -0
  90. data/spec/fixtures/test-server.key +15 -0
  91. data/spec/integration/helpers/request_spec_helper.rb +84 -0
  92. data/spec/integration/specs/lib/server_spec.rb +474 -0
  93. data/spec/integration_spec_helper.rb +16 -0
  94. data/spec/spec_helper.rb +39 -0
  95. data/spec/support/test_server.rb +105 -0
  96. data/spec/unit/agile_proxy/api/applications_spec.rb +102 -0
  97. data/spec/unit/agile_proxy/api/common_helper.rb +31 -0
  98. data/spec/unit/agile_proxy/api/recordings_spec.rb +115 -0
  99. data/spec/unit/agile_proxy/api/request_spec_recordings_spec.rb +119 -0
  100. data/spec/unit/agile_proxy/api/request_specs_spec.rb +159 -0
  101. data/spec/unit/agile_proxy/handlers/handler_spec.rb +8 -0
  102. data/spec/unit/agile_proxy/handlers/proxy_handler_spec.rb +138 -0
  103. data/spec/unit/agile_proxy/handlers/request_handler_spec.rb +76 -0
  104. data/spec/unit/agile_proxy/handlers/stub_handler_spec.rb +177 -0
  105. data/spec/unit/agile_proxy/model/recording_spec.rb +0 -0
  106. data/spec/unit/agile_proxy/model/request_spec_spec.rb +45 -0
  107. data/spec/unit/agile_proxy/model/response_spec.rb +38 -0
  108. data/spec/unit/agile_proxy/server_spec.rb +91 -0
  109. data/spec/unit/agile_proxy/servers/api_spec.rb +35 -0
  110. data/spec/unit/agile_proxy/servers/request_spec_direct_spec.rb +51 -0
  111. data/spec/unit/agile_proxy/servers/request_spec_spec.rb +35 -0
  112. metadata +736 -0
@@ -0,0 +1,16 @@
1
+ require 'require_all'
2
+
3
+ require 'agile_proxy'
4
+ require_all 'spec/support/**/*.rb'
5
+ require_all 'lib/agile_proxy/model'
6
+ require_all 'spec/integration/helpers'
7
+ require 'faker'
8
+ RSpec.configure do |config|
9
+ config.include AgileProxy::TestServer, :type => :integration
10
+ config.before :all, :type => :integration do
11
+ start_test_servers
12
+ start_proxy_server
13
+ debug_me = true
14
+ end
15
+
16
+ end
@@ -0,0 +1,39 @@
1
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
2
+ ENV['RACK_ENV'] ||= 'test'
3
+ require 'pry'
4
+ require 'active_record'
5
+ require 'shoulda-matchers'
6
+ require 'rack'
7
+ require 'logger'
8
+ require_relative './unit/agile_proxy/api/common_helper'
9
+ require_relative './common_helper'
10
+ require_relative '../lib/agile_proxy'
11
+ environment = 'test'
12
+ dbconfig = YAML.load(File.read(AgileProxy.config.database_config_file))
13
+ ActiveRecord::Base.establish_connection dbconfig[environment]
14
+
15
+ AgileProxy.configure do |config|
16
+ config.logger = Logger.new(File.expand_path('../../log/test.log', __FILE__))
17
+ end
18
+
19
+ RSpec.configure do |config|
20
+ include AgileProxy::TestServer
21
+ config.treat_symbols_as_metadata_keys_with_true_values = true
22
+ config.run_all_when_everything_filtered = true
23
+ config.filter_run :focus
24
+ config.order = 'random'
25
+
26
+ config.before :all do
27
+ start_test_servers
28
+ end
29
+
30
+ config.after :each do
31
+ AgileProxy.config.reset
32
+ end
33
+
34
+ config.mock_with :rspec do |mocks|
35
+ mocks.syntax = :expect
36
+ end
37
+ config.include AgileProxy::Test::Api::Common, api_test: true
38
+ config.include AgileProxy::Test::Common
39
+ end
@@ -0,0 +1,105 @@
1
+ require 'faraday'
2
+ require 'agile_proxy/cli'
3
+ require 'socket'
4
+
5
+
6
+
7
+
8
+ module AgileProxy
9
+ module TestServer
10
+ class DummyApi
11
+ def response(env)
12
+ [200, {}, "hello!"]
13
+ end
14
+ end
15
+ class Server
16
+ attr_accessor :server_port
17
+ def initialize(world)
18
+ @world = world
19
+ at_exit do
20
+ cleanup
21
+ end
22
+ #Thin::Logging.silent = true
23
+ end
24
+ def pids
25
+ @pids ||= []
26
+ end
27
+ def cleanup
28
+ until pids.empty?
29
+ begin
30
+ Process.kill('INT', pids.pop);
31
+ rescue Errno::ESRCH
32
+ #Do nothing
33
+ end
34
+ end
35
+ end
36
+ def available_port(qty = 1)
37
+ # use Addrinfo
38
+ results = []
39
+ sockets_opened = []
40
+ while results.length < qty
41
+ socket = Socket.new(:INET, :STREAM, 0)
42
+ socket.bind(Addrinfo.tcp("127.0.0.1", 0))
43
+ port = socket.local_address.ip_port
44
+ results.push port unless results.include? port
45
+ sockets_opened.push socket
46
+ end
47
+ sockets_opened.each do |socket|
48
+ socket.close
49
+ end
50
+ results
51
+ end
52
+
53
+ def ruby
54
+ RbConfig.ruby
55
+ end
56
+ def start_test_servers
57
+ ports = available_port(3)
58
+ puts "Starting test server on #{ports[0]}"
59
+ pids.push spawn(ruby, "echo_server.rb", '--address', 'localhost', '--port', "#{ports[0]}")
60
+ puts "Starting test server on #{ports[1]}"
61
+ pids.push spawn(ruby, "echo_server.rb", '--address', 'localhost', '--port', "#{ports[1]}", '--ssl')
62
+ puts "Starting test server on #{ports[2]}"
63
+ pids.push spawn({'STATUS_CODE' => '500'}, ruby, "echo_server.rb", '--address', 'localhost', '--port', "#{ports[2]}")
64
+ @world.instance_eval do
65
+ @http_url = "http://localhost:#{ports.shift}"
66
+ @https_url = "https://localhost:#{ports.shift}"
67
+ @error_url = "http://localhost:#{ports.shift}"
68
+ @http_url_no_proxy = "http://localhost:#{server_port}"
69
+ @https_url_no_proxy = "https://localhost:#{server_port}"
70
+ end
71
+ end
72
+
73
+
74
+ end
75
+
76
+ def initialize(rspecParams=nil)
77
+
78
+ end
79
+ def proxy_port
80
+ 3101
81
+ end
82
+ def api_port
83
+ 3021
84
+ end
85
+ def server_port
86
+ 3022
87
+ end
88
+
89
+
90
+ def start_test_servers
91
+ @test_servers = Server.new(self)
92
+ @test_servers.server_port = server_port
93
+ @test_servers.start_test_servers
94
+ end
95
+
96
+ def start_proxy_server
97
+ Thread.new do
98
+ cli = Cli.start(['start', proxy_port.to_s, server_port.to_s, api_port.to_s, '--env', 'test'])
99
+ end
100
+ sleep 10
101
+ end
102
+
103
+
104
+ end
105
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+ require_relative './common_helper'
3
+ require 'rack/test'
4
+
5
+ describe AgileProxy::Api::Applications, api_test: true do
6
+ include Rack::Test::Methods
7
+ include AgileProxy::Test::Api::Common
8
+ let(:applications_assoc_class) { Class.new }
9
+
10
+ describe 'GET /users/1/applications' do
11
+ let(:applications_result) do
12
+ [
13
+ { username: 'user1', password: 'password1', name: 'application1', record_requests: true, user_id: current_user.id }.stringify_keys,
14
+ { username: 'user1', password: 'password2', name: 'application2', record_requests: true, user_id: current_user.id }.stringify_keys,
15
+ { username: 'user1', password: 'password3', name: 'application3', record_requests: true, user_id: current_user.id }.stringify_keys
16
+ ]
17
+ end
18
+ before :each do
19
+ expect(current_user).to receive(:applications).and_return(applications_assoc_class)
20
+ expect(applications_assoc_class).to receive(:page).and_return applications_assoc_class
21
+ expect(applications_assoc_class).to receive(:per).and_return applications_assoc_class
22
+ expect(applications_assoc_class).to receive(:padding).and_return applications_assoc_class
23
+ expect(applications_assoc_class).to receive(:total_count).and_return 3
24
+ expect(applications_assoc_class).to receive(:num_pages).and_return 1
25
+ expect(applications_assoc_class).to receive(:current_page).and_return 1
26
+ expect(applications_assoc_class).to receive(:next_page).and_return 1
27
+ expect(applications_assoc_class).to receive(:prev_page).and_return 1
28
+ expect(applications_assoc_class).to receive(:count).and_return 3
29
+ expect(applications_assoc_class).to receive(:as_json).and_return(applications_result)
30
+ end
31
+ it 'returns a populated array of applications' do
32
+ get '/v1/users/1/applications'
33
+ expect(last_response.status).to eq(200)
34
+ expect(JSON.parse(last_response.body)).to eq('applications' => applications_result, 'total' => 3)
35
+ end
36
+ end
37
+ describe 'POST /users/1/applications' do
38
+ let(:create_attributes) { { username: 'user1', password: 'password', name: 'application1', record_requests: true } }
39
+ let(:to_be_created_attributes) { create_attributes.merge(user_id: current_user.id, record_requests: 'true').stringify_keys }
40
+ let(:created_attributes) { to_be_created_attributes }
41
+ let(:mock_application) { double('AgileProxy::Application', create_attributes) }
42
+ before :each do
43
+ expect(current_user).to receive(:applications).and_return applications_assoc_class
44
+ expect(applications_assoc_class).to receive(:create!).with(to_be_created_attributes).and_return(mock_application)
45
+ expect(mock_application).to receive(:as_json).and_return(to_be_created_attributes)
46
+ end
47
+ it 'Creates a new application and returns it' do
48
+ post '/v1/users/1/applications', create_attributes, 'Content-Type' => 'application/json'
49
+ expect(last_response.status).to eq(201)
50
+ expect(JSON.parse(last_response.body)).to eql(created_attributes)
51
+ end
52
+ end
53
+ describe 'DELETE /users/1/applications' do
54
+ before :each do
55
+ expect(current_user).to receive(:applications).and_return(applications_assoc_class)
56
+ expect(applications_assoc_class).to receive(:destroy_all)
57
+ end
58
+ it 'Should destroy all applications for the user' do
59
+ delete '/v1/users/1/applications'
60
+ expect(last_response.status).to eq(200)
61
+ expect(JSON.parse(last_response.body).symbolize_keys).to eql(applications: [], total: 0)
62
+ end
63
+ end
64
+ describe '/users/1/applications/10' do
65
+ let(:persisted_attributes) { { username: 'user1', password: 'password', name: 'application10', record_requests: true, id: 10 } }
66
+ let(:mock_application) { double('AgileProxy::Application', persisted_attributes) }
67
+ before :each do
68
+ expect(current_user).to receive(:applications).and_return(applications_assoc_class)
69
+ expect(applications_assoc_class).to receive(:where).with(id: '10').and_return applications_assoc_class
70
+ expect(applications_assoc_class).to receive(:first).and_return mock_application
71
+ expect(mock_application).to receive(:as_json).with({}).and_return persisted_attributes
72
+ end
73
+ context 'GET' do
74
+ it 'Should retrieve the application from persistence store' do
75
+ get '/v1/users/1/applications/10'
76
+ expect(last_response.status).to eq(200)
77
+ expect(JSON.parse(last_response.body).symbolize_keys).to eql(persisted_attributes)
78
+ end
79
+ end
80
+ context 'DELETE' do
81
+ it 'Should call destroy on the application found' do
82
+ expect(mock_application).to receive(:destroy)
83
+ delete '/v1/users/1/applications/10'
84
+ expect(last_response.status).to eq(200)
85
+ expect(JSON.parse(last_response.body).symbolize_keys).to eql(persisted_attributes)
86
+ end
87
+ end
88
+ context 'PUT' do
89
+ let(:to_be_updated_attributes) { { username: 'user1', password: 'password', name: 'application10 new name', record_requests: 'true' }.stringify_keys }
90
+ let(:updated_attributes) { to_be_updated_attributes.merge(record_requests: true).stringify_keys }
91
+ it 'Should call update_attributes on the application found' do
92
+ expect(mock_application).to receive(:update_attributes).with(to_be_updated_attributes)
93
+ persisted_attributes.merge! updated_attributes
94
+ persisted_attributes.delete(:id) # We dont allow modification of the id
95
+ put '/v1/users/1/applications/10', to_be_updated_attributes
96
+ expect(last_response.status).to eq(200)
97
+ expect(JSON.parse(last_response.body)).to eql(updated_attributes)
98
+
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,31 @@
1
+ module AgileProxy
2
+ module Test
3
+ module Api
4
+ # A common helper for all API unit tests
5
+ module Common
6
+ def self.included(base)
7
+ base.instance_eval do
8
+ before :each do
9
+ setup_active_record_environment!
10
+ def app
11
+ AgileProxy::Api::Root
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ def current_user
18
+ @__current_user ||= double('AgileProxy::User', id: 1, name: 'Test User')
19
+ end
20
+
21
+ def current_application
22
+ @__current_application ||= double('AgileProxy::Application', name: 'Default Application', id: 1)
23
+ end
24
+
25
+ def setup_active_record_environment!
26
+ allow(AgileProxy::User).to receive(:first).and_return current_user
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,115 @@
1
+ require 'spec_helper'
2
+ require_relative 'common_helper'
3
+ require 'rack/test'
4
+
5
+ describe AgileProxy::Api::Recordings, api_test: true do
6
+ include Rack::Test::Methods
7
+ include AgileProxy::Test::Api::Common
8
+ let(:applications_assoc_class) do
9
+ Class.new do
10
+ end
11
+ end
12
+ let(:recordings_assoc_class) do
13
+ Class.new do
14
+ def self.destroy_all
15
+ end
16
+ end
17
+ end
18
+ let(:application_instance) { applications_assoc_class.new }
19
+
20
+ before :each do
21
+ expect(current_user).to receive(:applications).and_return(applications_assoc_class)
22
+ expect(applications_assoc_class).to receive(:where).with(id: '1').and_return applications_assoc_class
23
+ expect(applications_assoc_class).to receive(:first).and_return application_instance
24
+ expect(application_instance).to receive(:recordings).and_return recordings_assoc_class
25
+
26
+ end
27
+ describe 'GET /users/1/applications/1/recordings' do
28
+ let(:recordings_result) do
29
+ [
30
+ {
31
+ application_id: 1,
32
+ request_headers: '{}',
33
+ request_body: '',
34
+ request_url: 'http://www.test.com/1',
35
+ request_method: 'GET',
36
+ response_headers: '{}',
37
+ response_body: '{}',
38
+ response_status: '200'
39
+ }.stringify_keys,
40
+ {
41
+ application_id: 1,
42
+ request_headers: '{}',
43
+ request_body: '',
44
+ request_url: 'http://www.test.com/2',
45
+ request_method: 'GET',
46
+ response_headers: '{}',
47
+ response_body: '{}',
48
+ response_status: '200'
49
+ }.stringify_keys,
50
+ {
51
+ application_id: 1,
52
+ request_headers: '{}',
53
+ request_body: '',
54
+ request_url: 'http://www.test.com/3',
55
+ request_method: 'GET',
56
+ response_headers: '{}',
57
+ response_body: '{}',
58
+ response_status: '200'
59
+ }.stringify_keys
60
+ ]
61
+ end
62
+ before :each do
63
+ expect(recordings_assoc_class).to receive(:page).and_return recordings_assoc_class
64
+ expect(recordings_assoc_class).to receive(:per).and_return recordings_assoc_class
65
+ expect(recordings_assoc_class).to receive(:padding).and_return recordings_assoc_class
66
+ expect(recordings_assoc_class).to receive(:total_count).and_return 3
67
+ expect(recordings_assoc_class).to receive(:num_pages).and_return 1
68
+ expect(recordings_assoc_class).to receive(:current_page).and_return 1
69
+ expect(recordings_assoc_class).to receive(:next_page).and_return 1
70
+ expect(recordings_assoc_class).to receive(:prev_page).and_return 1
71
+ expect(recordings_assoc_class).to receive(:count).and_return 3
72
+ expect(recordings_assoc_class).to receive(:as_json).with({}).and_return recordings_result
73
+
74
+ end
75
+ it 'returns a populated array of recordings' do
76
+ get '/v1/users/1/applications/1/recordings'
77
+ expect(last_response.status).to eq(200)
78
+ expect(JSON.parse(last_response.body)).to eq('recordings' => recordings_result, 'total' => 3)
79
+ end
80
+ end
81
+ describe 'DELETE /users/1/applications/1/recordings' do
82
+ it 'Should destroy all applications for the user' do
83
+ expect(recordings_assoc_class).to receive(:destroy_all)
84
+ delete '/v1/users/1/applications/1/recordings'
85
+ expect(last_response.status).to eq(200)
86
+ expect(JSON.parse(last_response.body).symbolize_keys).to eql(recordings: [], total: 0)
87
+ end
88
+ end
89
+ describe '/users/1/applications/1/recordings/10' do
90
+ let(:recording_instance) { recordings_assoc_class.new }
91
+ let(:persisted_attributes) { { request_headers: '', response_headers: {} } }
92
+ before :each do
93
+ expect(recordings_assoc_class).to receive(:where).with(id: '10').and_return recordings_assoc_class
94
+ expect(recordings_assoc_class).to receive(:first).and_return recording_instance
95
+ expect(recording_instance).to receive(:as_json).with({}).and_return persisted_attributes
96
+ end
97
+ context 'GET' do
98
+ it 'Should fetch an individual recording' do
99
+ get '/v1/users/1/applications/1/recordings/10'
100
+ expect(last_response.status).to eq(200)
101
+ expect(JSON.parse(last_response.body).symbolize_keys).to eql(persisted_attributes)
102
+
103
+ end
104
+ end
105
+ context 'DELETE' do
106
+ it 'Should delete an individual recording' do
107
+ expect(recording_instance).to receive(:destroy)
108
+ delete '/v1/users/1/applications/1/recordings/10'
109
+ expect(last_response.status).to eq(200)
110
+ expect(JSON.parse(last_response.body).symbolize_keys).to eql(persisted_attributes)
111
+ end
112
+ end
113
+ end
114
+
115
+ end
@@ -0,0 +1,119 @@
1
+ require 'spec_helper'
2
+ require_relative 'common_helper'
3
+ require 'rack/test'
4
+
5
+ describe AgileProxy::Api::RequestSpecRecordings, api_test: true do
6
+ include Rack::Test::Methods
7
+ include AgileProxy::Test::Api::Common
8
+ let(:applications_assoc_class) do
9
+ Class.new do
10
+ end
11
+ end
12
+ let(:recordings_assoc_class) do
13
+ Class.new do
14
+ def self.destroy_all
15
+ end
16
+ end
17
+ end
18
+ let(:application_instance) { applications_assoc_class.new }
19
+
20
+ before :each do
21
+ expect(current_user).to receive(:applications).and_return(applications_assoc_class)
22
+ expect(applications_assoc_class).to receive(:where).with(id: '1').and_return applications_assoc_class
23
+ expect(applications_assoc_class).to receive(:first).and_return application_instance
24
+ expect(application_instance).to receive(:recordings).and_return recordings_assoc_class
25
+
26
+ end
27
+ describe 'GET /users/1/applications/1/recordings' do
28
+ let(:recordings_result) do
29
+ [
30
+ {
31
+ application_id: 1,
32
+ request_headers: '{}',
33
+ request_body: '',
34
+ request_url: 'http://www.test.com/1',
35
+ request_method: 'GET',
36
+ response_headers: '{}',
37
+ response_body: '{}',
38
+ response_status: '200',
39
+ request_spec_id: 2
40
+ }.stringify_keys,
41
+ {
42
+ application_id: 1,
43
+ request_headers: '{}',
44
+ request_body: '',
45
+ request_url: 'http://www.test.com/2',
46
+ request_method: 'GET',
47
+ response_headers: '{}',
48
+ response_body: '{}',
49
+ response_status: '200',
50
+ request_spec_id: 2
51
+ }.stringify_keys,
52
+ {
53
+ application_id: 1,
54
+ request_headers: '{}',
55
+ request_body: '',
56
+ request_url: 'http://www.test.com/3',
57
+ request_method: 'GET',
58
+ response_headers: '{}',
59
+ response_body: '{}',
60
+ response_status: '200',
61
+ request_spec_id: 2
62
+ }.stringify_keys
63
+ ]
64
+ end
65
+ before :each do
66
+ expect(recordings_assoc_class).to receive(:where).with(request_spec_id: '2').and_return recordings_assoc_class
67
+ expect(recordings_assoc_class).to receive(:page).and_return recordings_assoc_class
68
+ expect(recordings_assoc_class).to receive(:per).and_return recordings_assoc_class
69
+ expect(recordings_assoc_class).to receive(:padding).and_return recordings_assoc_class
70
+ expect(recordings_assoc_class).to receive(:total_count).and_return 3
71
+ expect(recordings_assoc_class).to receive(:num_pages).and_return 1
72
+ expect(recordings_assoc_class).to receive(:current_page).and_return 1
73
+ expect(recordings_assoc_class).to receive(:next_page).and_return 1
74
+ expect(recordings_assoc_class).to receive(:prev_page).and_return 1
75
+ expect(recordings_assoc_class).to receive(:count).and_return 3
76
+ expect(recordings_assoc_class).to receive(:as_json).with({}).and_return recordings_result
77
+
78
+ end
79
+ it 'returns a populated array of recordings' do
80
+ get '/v1/users/1/applications/1/request_specs/2/recordings'
81
+ expect(last_response.status).to eq(200)
82
+ expect(JSON.parse(last_response.body)).to eq('recordings' => recordings_result, 'total' => 3)
83
+ end
84
+ end
85
+ describe 'DELETE /users/1/applications/1/request_specs/2/recordings' do
86
+ it 'Should destroy all applications for the request spec' do
87
+ expect(recordings_assoc_class).to receive(:destroy_all).with(request_spec_id: '2')
88
+ delete '/v1/users/1/applications/1/request_specs/2/recordings'
89
+ expect(last_response.status).to eq(200)
90
+ expect(JSON.parse(last_response.body).symbolize_keys).to eql(recordings: [], total: 0)
91
+ end
92
+ end
93
+ describe '/users/1/applications/1/request_specs/2/recordings/10' do
94
+ let(:recording_instance) { recordings_assoc_class.new }
95
+ let(:persisted_attributes) { { request_headers: '', response_headers: {} } }
96
+ before :each do
97
+ expect(recordings_assoc_class).to receive(:where).with(request_spec_id: '2', id: '10').and_return recordings_assoc_class
98
+ expect(recordings_assoc_class).to receive(:first).and_return recording_instance
99
+ expect(recording_instance).to receive(:as_json).with({}).and_return persisted_attributes
100
+ end
101
+ context 'GET' do
102
+ it 'Should fetch an individual recording' do
103
+ get '/v1/users/1/applications/1/request_specs/2/recordings/10'
104
+ expect(last_response.status).to eq(200)
105
+ expect(JSON.parse(last_response.body).symbolize_keys).to eql(persisted_attributes)
106
+
107
+ end
108
+ end
109
+ context 'DELETE' do
110
+ it 'Should delete an individual recording' do
111
+ expect(recording_instance).to receive(:destroy)
112
+ delete '/v1/users/1/applications/1/request_specs/2/recordings/10'
113
+ expect(last_response.status).to eq(200)
114
+ expect(JSON.parse(last_response.body).symbolize_keys).to eql(persisted_attributes)
115
+ end
116
+ end
117
+ end
118
+
119
+ end