ctws 0.1.5.alpha

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +295 -0
  4. data/Rakefile +21 -0
  5. data/app/auth/ctws/authenticate_user.rb +36 -0
  6. data/app/auth/ctws/authorize_api_request.rb +44 -0
  7. data/app/controllers/concerns/ctws/exception_handler.rb +58 -0
  8. data/app/controllers/concerns/ctws/response.rb +25 -0
  9. data/app/controllers/concerns/ctws/v1/exception_handler.rb +6 -0
  10. data/app/controllers/concerns/ctws/v1/response.rb +6 -0
  11. data/app/controllers/ctws/application_controller.rb +5 -0
  12. data/app/controllers/ctws/authentication_controller.rb +28 -0
  13. data/app/controllers/ctws/ctws_controller.rb +24 -0
  14. data/app/controllers/ctws/min_app_versions_controller.rb +55 -0
  15. data/app/controllers/ctws/users_controller.rb +39 -0
  16. data/app/controllers/ctws/v1/authentication_controller.rb +6 -0
  17. data/app/controllers/ctws/v1/ctws_controller.rb +9 -0
  18. data/app/controllers/ctws/v1/min_app_versions_controller.rb +6 -0
  19. data/app/controllers/ctws/v1/users_controller.rb +6 -0
  20. data/app/controllers/ctws/v2/ctws_controller.rb +9 -0
  21. data/app/controllers/ctws/v2/min_app_versions_controller.rb +6 -0
  22. data/app/jobs/ctws/application_job.rb +4 -0
  23. data/app/lib/ctws/json_web_token.rb +25 -0
  24. data/app/lib/ctws/message.rb +43 -0
  25. data/app/mailers/ctws/application_mailer.rb +6 -0
  26. data/app/models/ctws/application_record.rb +5 -0
  27. data/app/models/ctws/min_app_version.rb +22 -0
  28. data/config/initializers/ctws.rb +5 -0
  29. data/config/initializers/mime_types.rb +1 -0
  30. data/config/routes.rb +18 -0
  31. data/db/migrate/20170425110839_create_ctws_min_app_versions.rb +13 -0
  32. data/db/migrate/20170425110933_add_values_to_ctws_min_app_versions.rb +26 -0
  33. data/lib/ctws/engine.rb +13 -0
  34. data/lib/ctws/version.rb +3 -0
  35. data/lib/ctws.rb +18 -0
  36. data/lib/tasks/ctws_tasks.rake +4 -0
  37. data/spec/auth/ctws/authenticate_user_spec.rb +34 -0
  38. data/spec/auth/ctws/authorize_api_request_spec.rb +62 -0
  39. data/spec/controllers/ctws/ctws_controller_spec.rb +41 -0
  40. data/spec/controllers/ctws/users_controller_spec.rb +7 -0
  41. data/spec/dummy/Rakefile +6 -0
  42. data/spec/dummy/app/assets/config/manifest.js +5 -0
  43. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  44. data/spec/dummy/app/assets/javascripts/cable.js +13 -0
  45. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  46. data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
  47. data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
  48. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  49. data/spec/dummy/app/controllers/user_controller.rb +18 -0
  50. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  51. data/spec/dummy/app/helpers/user_helper.rb +2 -0
  52. data/spec/dummy/app/jobs/application_job.rb +2 -0
  53. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  54. data/spec/dummy/app/models/application_record.rb +3 -0
  55. data/spec/dummy/app/models/user.rb +4 -0
  56. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  57. data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
  58. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  59. data/spec/dummy/bin/bundle +3 -0
  60. data/spec/dummy/bin/rails +4 -0
  61. data/spec/dummy/bin/rake +4 -0
  62. data/spec/dummy/bin/setup +34 -0
  63. data/spec/dummy/bin/update +29 -0
  64. data/spec/dummy/config/application.rb +15 -0
  65. data/spec/dummy/config/boot.rb +5 -0
  66. data/spec/dummy/config/cable.yml +9 -0
  67. data/spec/dummy/config/database.yml +25 -0
  68. data/spec/dummy/config/environment.rb +5 -0
  69. data/spec/dummy/config/environments/development.rb +54 -0
  70. data/spec/dummy/config/environments/production.rb +86 -0
  71. data/spec/dummy/config/environments/test.rb +42 -0
  72. data/spec/dummy/config/initializers/application_controller_renderer.rb +6 -0
  73. data/spec/dummy/config/initializers/assets.rb +11 -0
  74. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  75. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
  76. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  77. data/spec/dummy/config/initializers/inflections.rb +16 -0
  78. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  79. data/spec/dummy/config/initializers/new_framework_defaults.rb +24 -0
  80. data/spec/dummy/config/initializers/session_store.rb +3 -0
  81. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  82. data/spec/dummy/config/locales/en.yml +23 -0
  83. data/spec/dummy/config/puma.rb +47 -0
  84. data/spec/dummy/config/routes.rb +5 -0
  85. data/spec/dummy/config/secrets.yml +22 -0
  86. data/spec/dummy/config/spring.rb +6 -0
  87. data/spec/dummy/config.ru +5 -0
  88. data/spec/dummy/db/development.sqlite3 +0 -0
  89. data/spec/dummy/db/migrate/20170622072636_create_users.rb +10 -0
  90. data/spec/dummy/db/schema.rb +39 -0
  91. data/spec/dummy/db/test.sqlite3 +0 -0
  92. data/spec/dummy/log/development.log +29 -0
  93. data/spec/dummy/log/test.log +31496 -0
  94. data/spec/dummy/public/404.html +67 -0
  95. data/spec/dummy/public/422.html +67 -0
  96. data/spec/dummy/public/500.html +66 -0
  97. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  98. data/spec/dummy/public/apple-touch-icon.png +0 -0
  99. data/spec/dummy/public/favicon.ico +0 -0
  100. data/spec/factories/ctws/ctws_user.rb +7 -0
  101. data/spec/factories/ctws_min_app_version.rb +9 -0
  102. data/spec/models/ctws/min_app_version_spec.rb +11 -0
  103. data/spec/models/ctws/user_spec.rb +8 -0
  104. data/spec/rails_helper.rb +88 -0
  105. data/spec/requests/ctws/authentication_spec.rb +47 -0
  106. data/spec/requests/ctws/min_app_version_spec.rb +169 -0
  107. data/spec/requests/ctws/users_spec.rb +46 -0
  108. data/spec/spec_helper.rb +27 -0
  109. data/spec/support/ctws/controller_spec_helper.rb +29 -0
  110. data/spec/support/ctws/request_spec_helper.rb +10 -0
  111. metadata +367 -0
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/404.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The page you were looking for doesn't exist.</h1>
62
+ <p>You may have mistyped the address or the page may have moved.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/422.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
File without changes
@@ -0,0 +1,7 @@
1
+ FactoryGirl.define do
2
+ factory :ctws_user, class: User do
3
+ email "email@test.com"
4
+ password "ruby123"
5
+ password_confirmation "ruby123"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ FactoryGirl.define do
2
+ factory :ctws_min_app_version, class: Ctws::MinAppVersion do
3
+ codename { Faker::Lorem.words(3) }
4
+ description { Faker::Lorem.sentences(1) }
5
+ platform {['ios', 'android', 'other'].sample}
6
+ min_version {"#{Faker::Number.digit}.#{Faker::Number.digit}.#{Faker::Number.digit}"}
7
+ store_uri { Faker::Internet.url }
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails_helper'
2
+
3
+ module Ctws
4
+ RSpec.describe MinAppVersion, type: :model do
5
+ it { should validate_presence_of(:codename) }
6
+ it { should validate_presence_of(:description) }
7
+ it { should validate_presence_of(:platform) }
8
+ it { should validate_presence_of(:min_version) }
9
+ it { should validate_presence_of(:store_uri) }
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ require 'rails_helper'
2
+
3
+ module Ctws
4
+ RSpec.describe User, type: :model do
5
+ it { should validate_presence_of(:email) }
6
+ it { should validate_presence_of(:password_digest) }
7
+ end
8
+ end
@@ -0,0 +1,88 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ ENV['RAILS_ENV'] ||= 'test'
3
+ # require File.expand_path('../../config/environment', __FILE__)
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+
6
+ # Prevent database truncation if the environment is production
7
+ abort("The Rails environment is running in production mode!") if Rails.env.production?
8
+ require 'spec_helper'
9
+ require 'rspec/rails'
10
+ require 'jwt'
11
+
12
+ # Add additional requires below this line. Rails is not loaded until this point!
13
+ require 'database_cleaner'
14
+ require 'factory_girl_rails'
15
+ # Requires supporting ruby files with custom matchers and macros, etc, in
16
+ # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
17
+ # run as spec files by default. This means that files in spec/support that end
18
+ # in _spec.rb will both be required and run as specs, causing the specs to be
19
+ # run twice. It is recommended that you do not name files matching this glob to
20
+ # end with _spec.rb. You can configure this pattern with the --pattern
21
+ # option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
22
+ #
23
+ # The following line is provided for convenience purposes. It has the downside
24
+ # of increasing the boot-up time by auto-requiring all files in the support
25
+ # directory. Alternatively, in the individual `*_spec.rb` files, manually
26
+ # require only the support files necessary.
27
+ #
28
+ # Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
29
+
30
+ # Checks for pending migration and applies them before tests are run.
31
+ # If you are not using ActiveRecord, you can remove this line.
32
+ ActiveRecord::Migration.maintain_test_schema!
33
+
34
+ # configure shoulda matchers to use rspec as the test framework and full matcher libraries for rails
35
+ Shoulda::Matchers.configure do |config|
36
+ config.integrate do |with|
37
+ with.test_framework :rspec
38
+ with.library :rails
39
+ end
40
+ end
41
+
42
+ RSpec.configure do |config|
43
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
44
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
45
+
46
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
47
+ # examples within a transaction, remove the following line or assign false
48
+ # instead of true.
49
+ config.use_transactional_fixtures = true
50
+
51
+ # add `FactoryGirl` methods
52
+ config.include FactoryGirl::Syntax::Methods
53
+
54
+ # start by truncating all the tables but then use the faster transaction strategy the rest of the time.
55
+ config.before(:suite) do
56
+ DatabaseCleaner.clean_with(:truncation)
57
+ DatabaseCleaner.strategy = :transaction
58
+ end
59
+
60
+ # start the transaction strategy as examples are run
61
+ config.around(:each) do |example|
62
+ DatabaseCleaner.cleaning do
63
+ example.run
64
+ end
65
+ end
66
+
67
+ # RSpec Rails can automatically mix in different behaviours to your tests
68
+ # based on their file location, for example enabling you to call `get` and
69
+ # `post` in specs under `spec/controllers`.
70
+ #
71
+ # You can disable this behaviour by removing the line below, and instead
72
+ # explicitly tag your specs with their type, e.g.:
73
+ #
74
+ # RSpec.describe UsersController, :type => :controller do
75
+ # # ...
76
+ # end
77
+ #
78
+ # The different available types are documented in the features, such as in
79
+ # https://relishapp.com/rspec/rspec-rails/docs
80
+ config.infer_spec_type_from_file_location!
81
+
82
+ config.include Ctws::RequestSpecHelper
83
+ config.include Ctws::ControllerSpecHelper
84
+ # Filter lines from Rails gems in backtraces.
85
+ config.filter_rails_from_backtrace!
86
+ # arbitrary gems may also be filtered via:
87
+ # config.filter_gems_from_backtrace("gem name")
88
+ end
@@ -0,0 +1,47 @@
1
+ require 'rails_helper'
2
+
3
+ module Ctws
4
+ RSpec.describe 'Ctws::Authentication', type: :request do
5
+ # Authentication test suite
6
+ describe 'POST ctws/v1/login' do
7
+ # create test user
8
+ let!(:ctws_user) { create(:ctws_user) }
9
+ # set headers for authorization
10
+ let(:headers) { valid_headers.except('Authorization') }
11
+ # set test valid and invalid credentials
12
+ let(:valid_credentials) do
13
+ {
14
+ email: ctws_user.email,
15
+ password: ctws_user.password
16
+ }.to_json
17
+ end
18
+ let(:invalid_credentials) do
19
+ {
20
+ email: Faker::Internet.email,
21
+ password: Faker::Internet.password
22
+ }.to_json
23
+ end
24
+
25
+ # set request.headers to our custom headers
26
+ # before { allow(request).to receive(:headers).and_return(headers) }
27
+
28
+ # returns auth token when request is valid
29
+ context 'When request is valid' do
30
+ before { post '/ctws/v1/login', params: valid_credentials, headers: headers }
31
+
32
+ it 'returns an authentication token' do
33
+ expect(json["data"]["attributes"]["auth_token"]).not_to be_nil
34
+ end
35
+ end
36
+
37
+ # returns failure message when request is invalid
38
+ context 'When request is invalid' do
39
+ before { post '/ctws/v1/login', params: invalid_credentials, headers: headers }
40
+
41
+ it 'returns a failure message' do
42
+ expect(json["errors"]["message"]).to match(/Invalid credentials/)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,169 @@
1
+ require 'rails_helper'
2
+ require 'support/ctws/controller_spec_helper.rb'
3
+ #require Rails.root.join('spec', 'support', 'ctws', 'controller_spec_helper.rb')
4
+
5
+ module Ctws
6
+ RSpec.describe 'Ctws::MinAppVersion API', type: :request do
7
+ # initialize test data
8
+ let(:ctws_user) { create(:ctws_user) }
9
+ # let!(:todo) { create(:todo, created_by: user.id) }
10
+ let!(:min_app_versions) { create_list(:ctws_min_app_version, 10) }
11
+ let(:min_app_version_id) { min_app_versions.first.id }
12
+ # authorize request
13
+ let(:headers) { { 'Authorization' => token_generator(ctws_user.id) } }
14
+ let(:invalid_headers) { { 'Authorization' => nil } }
15
+
16
+ # Test suite for GET /ctws/v1/min-app-version
17
+ describe 'GET /ctws/v1/min_app_version' do
18
+ # make HTTP get request before each example
19
+ before { get '/ctws/v1/min_app_version' }
20
+
21
+ it 'returns min_app_version_platforms' do
22
+ # Note `json` is a custom helper to parse JSON responses
23
+ expect(json).not_to be_empty
24
+ end
25
+
26
+ it 'returns success true value' do
27
+ expect_success
28
+ end
29
+
30
+ it 'returns status code 200' do
31
+ expect(response).to have_http_status(200)
32
+ end
33
+ end
34
+
35
+ # Test suite for GET /ctws/v1/min-app-versions
36
+ describe 'GET /ctws/v1/min_app_versions' do
37
+ # make HTTP get request before each example
38
+ before { get '/ctws/v1/min_app_versions', params: {}, headers: headers }
39
+
40
+ it 'returns min_app_versions' do
41
+ # Note `json` is a custom helper to parse JSON responses
42
+ expect(json).not_to be_empty
43
+ expect(json["data"].size).to eq(10)
44
+ end
45
+
46
+ it 'returns success true value' do
47
+ expect_success
48
+ end
49
+
50
+ it 'returns status code 200' do
51
+ expect(response).to have_http_status(200)
52
+ end
53
+ end
54
+
55
+ # Test suite for GET /ctws/v1/min_app_versions/:id
56
+ describe 'GET /ctws/v1/min_app_versions/:id' do
57
+ before { get "/ctws/v1/min_app_versions/#{min_app_version_id}", params: {}, headers: valid_headers }
58
+
59
+ context 'when the record exists' do
60
+ it 'returns the min_app_version' do
61
+ expect(json).not_to be_empty
62
+ expect(json["data"]['id']).to eq(min_app_version_id)
63
+ end
64
+
65
+ it 'returns success true value' do
66
+ expect_success
67
+ end
68
+
69
+ it 'returns status code 200' do
70
+ expect(response).to have_http_status(200)
71
+ end
72
+ end
73
+
74
+ context 'when the record does not exist' do
75
+ let(:min_app_version_id) { 100 }
76
+
77
+ it 'returns status code 404' do
78
+ expect(response).to have_http_status(404)
79
+ end
80
+
81
+ it 'returns success true value' do
82
+ expect_success false
83
+ end
84
+
85
+ it 'returns a not found message' do
86
+ expect(response.body).to match(/Couldn't find Ctws::MinAppVersion with 'id'=100/)
87
+ end
88
+ end
89
+ end
90
+
91
+ # Test suite for POST /ctws/v1/min_app_versions
92
+ describe 'POST /ctws/v1/min_app_versions' do
93
+ # valid payload
94
+ let(:valid_attributes) do
95
+ { codename: "Test Version test", description: "Lorem ipsum dolor sit amet.", platform: "ios", min_version: "2.0.1", store_uri: "https://store.example.com" }
96
+ end
97
+
98
+ context 'when the request is valid' do
99
+ # before { post '/ctws/v1/min_app_versions', params: valid_attributes, headers: headers }
100
+ before { post '/ctws/v1/min_app_versions', params: { min_app_version: valid_attributes }, headers: headers }
101
+
102
+ it 'creates a min_app_version' do
103
+ expect(json['data']['platform']).to eq('ios')
104
+ end
105
+
106
+ it 'returns success true value' do
107
+ expect_success
108
+ end
109
+
110
+ it 'returns status code 201' do
111
+ expect(response).to have_http_status(201)
112
+ end
113
+ end
114
+
115
+ context 'when the request is invalid' do
116
+ before { post '/ctws/v1/min_app_versions', params: { min_app_version: { codename: "Test Version" } }, headers: headers }
117
+
118
+ it 'returns status code 422' do
119
+ expect(response).to have_http_status(422)
120
+ end
121
+
122
+ it 'returns success true value' do
123
+ expect_success false
124
+ end
125
+
126
+ it 'returns a validation failure message' do
127
+ expect(response.body)
128
+ .to match(/Validation failed: Description can't be blank, Platform can't be blank, Min version can't be blank, Store uri can't be blank/)
129
+ # .to match(/Validation failed: Created by can't be blank/)
130
+ end
131
+ end
132
+ end
133
+
134
+ # Test suite for PUT /ctws/v1/min_app_versions/:id
135
+ describe 'PUT /ctws/v1/min_app_versions/:id' do
136
+ let(:valid_attributes) { { codename: "Test Version Edit", min_version: "3.0.0" } }
137
+
138
+ context 'when the record exists' do
139
+ before { put "/ctws/v1/min_app_versions/#{min_app_version_id}", params: { min_app_version: valid_attributes }, headers: headers }
140
+
141
+ it 'updates the record' do
142
+ expect(response.body).to be_empty
143
+ end
144
+
145
+ it 'response with no body' do
146
+ expect(response.body).to be_empty
147
+ end
148
+
149
+ it 'returns status code 204' do
150
+ expect(response).to have_http_status(204)
151
+ end
152
+ end
153
+ end
154
+
155
+ # Test suite for DELETE /ctws/v1/min_app_versions/:id
156
+ describe 'DELETE /ctws/v1/min_app_versions/:id' do
157
+ before { delete "/ctws/v1/min_app_versions/#{min_app_version_id}", params: {}, headers: headers }
158
+
159
+ it 'response with no body' do
160
+ expect(response.body).to be_empty
161
+ end
162
+
163
+ it 'returns status code 204' do
164
+ expect(response).to have_http_status(204)
165
+ end
166
+ end
167
+
168
+ end
169
+ end
@@ -0,0 +1,46 @@
1
+ require 'rails_helper'
2
+
3
+ module Ctws
4
+ RSpec.describe 'Ctws::User API', type: :request do
5
+ let(:ctws_user) { build(:ctws_user) }
6
+ let(:headers) { valid_headers.except('Authorization') }
7
+ let(:valid_attributes) do
8
+ attributes_for(:ctws_user, email: ctws_user.email, password_confirmation: ctws_user.password_confirmation)
9
+ end
10
+
11
+ # User signup test suite
12
+ # http --form POST :3000/ctws/v1/signup email='hola@agusti.cat', password_confirmation: "123456"
13
+ describe 'POST /ctws/v1/signup' do
14
+ context 'when valid request' do
15
+ before { post '/ctws/v1/signup', params: valid_attributes.to_json, headers: headers }
16
+
17
+ it 'creates a new user' do
18
+ expect(response).to have_http_status(201)
19
+ end
20
+
21
+ it 'returns success message' do
22
+ expect(json["data"]["attributes"]["message"]).to match(/Account created successfully/)
23
+ end
24
+
25
+ it 'returns an authentication token' do
26
+ expect(json["data"]["attributes"]["auth_token"]).not_to be_nil
27
+ end
28
+ end
29
+
30
+ context 'when invalid request' do
31
+ before { post '/ctws/v1/signup', params: {}, headers: headers }
32
+
33
+ it 'does not create a new user' do
34
+ expect(response).to have_http_status(422)
35
+ end
36
+
37
+ it 'returns failure message' do
38
+ expect(json["errors"]['message']).not_to be_nil
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ # curl -X POST -H 'Content-Type: application/json' -F "email:agusti.br@coditramuntana.com" -F "password: 123456789" http://localhost:3000/ws/v1/signup
46
+ # {"success":true,"data":{"message":"Account created successfully","auth_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxLCJleHAiOjE0OTQ1MTY3NjR9.CWETXkwdlS8jhhu0RHYbYhcBdkpZw1ySoVMCTGgUL6g"}}%
@@ -0,0 +1,27 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+
3
+ require File.expand_path("../../spec/dummy/config/environment.rb", __FILE__)
4
+ require 'rspec/rails'
5
+ require 'factory_girl_rails'
6
+ require 'shoulda-matchers'
7
+ require 'faker'
8
+ require 'database_cleaner'
9
+ require 'jwt'
10
+
11
+ require 'ctws'
12
+
13
+ puts Bundler.default_gemfile
14
+ puts ENV['BUNDLE_GEMFILE'].inspect
15
+
16
+ Rails.backtrace_cleaner.remove_silencers!
17
+
18
+ # Load support files
19
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
20
+ Dir[Rails.root.join("spec/factories/**/*.rb")].each {|f| require f}
21
+
22
+ RSpec.configure do |config|
23
+ config.mock_with :rspec
24
+ config.use_transactional_fixtures = true
25
+ config.infer_base_class_for_anonymous_controllers = false
26
+ config.order = "random"
27
+ end
@@ -0,0 +1,29 @@
1
+ module Ctws
2
+ module ControllerSpecHelper
3
+ # generate tokens from user id
4
+ def token_generator(user_id)
5
+ Ctws::JsonWebToken.encode(user_id: user_id)
6
+ end
7
+
8
+ # generate expired tokens from user id
9
+ def expired_token_generator(user_id)
10
+ Ctws::JsonWebToken.encode({ user_id: user_id }, (Time.now.to_i - 10))
11
+ end
12
+
13
+ # return valid headers
14
+ def valid_headers
15
+ {
16
+ "Authorization" => token_generator(ctws_user.id),
17
+ "Content-Type" => "application/json"
18
+ }
19
+ end
20
+
21
+ # return invalid headers
22
+ def invalid_headers
23
+ {
24
+ "Authorization" => nil,
25
+ "Content-Type" => "application/json"
26
+ }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ module Ctws
2
+ module RequestSpecHelper
3
+ def json
4
+ JSON.parse(response.body)
5
+ end
6
+ def expect_success(value= true)
7
+ # expect(json["success"]).to eq(value)
8
+ end
9
+ end
10
+ end