effective_test_bot 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d9fcb782d42810e22945e31a628388904113afc5
4
- data.tar.gz: 646460eeb7f12195f8f178699ce9572ffd183900
3
+ metadata.gz: 56a5ca16769d862bb00827de7e8a90d5237bba0e
4
+ data.tar.gz: c04f6efc857cc2d34d8e379730e4856dedbffca4
5
5
  SHA512:
6
- metadata.gz: 8795f0ea8358bad1108443ef1422a6f0db4bc8e708390f2c3503807dbe44cde4dd7b2788c95e599417c820fa634b60b57f59f2b93c4c07ab221ab119893f4ba4
7
- data.tar.gz: 8aa91d926c3b63aa2ca04a72f02266568239a860562cd9ca5727563f50a7719f45583a38ff1c3f987a359d75a9620b22346d780c92c35704e1eb30c8dd195363
6
+ metadata.gz: 06509576f68274751a0952c467e8a7870c55d8eca12ac2aaacfaf76d150d113b091d3f12038b74aa1facedfa8567eb78eed81a5d1f7af7d97ad8762ed5b64608
7
+ data.tar.gz: ccf648a5bb36c3c78641efcc0aedfdd8a3105e08316fbea4b8405ad746641a4a5e348fb7d8c0b06d4d14451c14a5089750ff585a9d506f63b0e72360600582fd
data/README.md CHANGED
@@ -84,3 +84,4 @@ rake spec
84
84
  5. Bonus points for test coverage
85
85
  6. Create new Pull Request
86
86
 
87
+ #page.save_screenshot('sign-in-test-1.png')
@@ -0,0 +1,137 @@
1
+ module EffectiveTestBotHelper
2
+ def sign_up(email = Faker::Internet.email, password = Faker::Internet.password)
3
+ visit new_user_registration_path
4
+
5
+ within('form#new_user') do
6
+ fill_form(:email => email, :password => password, :password_confirmation => password)
7
+ submit_form
8
+ end
9
+
10
+ assert_equal page.status_code, 200
11
+ assert_content I18n.t('devise.registrations.signed_up')
12
+
13
+ User.find_by_email(email)
14
+ end
15
+
16
+ def sign_in(user) # Warden::Test::Helpers
17
+ if user.kind_of?(String)
18
+ login_as(User.find_by_email(user))
19
+ else
20
+ login_as(user)
21
+ end
22
+ end
23
+
24
+ # def sign_in(user)
25
+ # visit new_user_session_path
26
+
27
+ # within('form#new_user') do
28
+ # fill_form(:email => email, :password => password)
29
+ # page.save_screenshot('signin.png')
30
+ # submit_form
31
+ # page.save_screenshot('signin2.png')
32
+ # end
33
+
34
+ # assert_equal page.status_code, 200
35
+ # assert_content I18n.t('devise.sessions.signed_in')
36
+ # end
37
+
38
+ # fill_form(:email => 'somethign@soneone.com', :password => 'blahblah', 'user.last_name' => 'hlwerewr')
39
+ def fill_form(fills = {})
40
+ fills = HashWithIndifferentAccess.new(fills)
41
+
42
+ all('input,select,textarea').each do |field|
43
+ case [field.tag_name, field['type']].compact.join('_')
44
+ when 'input_text', 'input_email', 'input_password', 'input_tel', 'input_number', 'textarea'
45
+ field.set(fill_value(field, fills))
46
+ when 'input_checkbox', 'input_radio'
47
+ field.set(fill_value(field, fills)) # TODO
48
+ when 'select'
49
+ field.select(fill_value(field, fills), match: :first)
50
+ when 'input_file'
51
+ puts "Warning, input_file not yet supported"
52
+ when 'input_submit'
53
+ # Do nothing
54
+ else
55
+ raise "unsupported field type #{[field.tag_name, field['type']].compact.join('_')}"
56
+ end
57
+ end
58
+ end
59
+
60
+ # Operates on just string keys
61
+ def fill_value(field, fills = nil)
62
+ attributes = field['name'].to_s.gsub(']', '').split('[') # user[something_attributes][last_name] => ['user', 'something_attributes', 'last_name']
63
+ field_name = [field.tag_name, field['type']].compact.join('_')
64
+ fill_value = nil
65
+
66
+ if fills.present?
67
+ key = nil
68
+ attributes.reverse_each do |name|
69
+ key = (key.present? ? "#{name}.#{key}" : name)
70
+
71
+ if fills.key?(key)
72
+ fill_value = fills[key]
73
+
74
+ if field_name == 'select'
75
+ break
76
+ else
77
+ return fill_value
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ case field_name
84
+ when 'input_email'
85
+ Faker::Internet.email
86
+ when 'input_number'
87
+ Faker::Number.number(4)
88
+ when 'input_password'
89
+ Faker::Internet.password
90
+ when 'input_tel'
91
+ dig = (1..9).to_a
92
+ "#{dig.sample}#{dig.sample}#{dig.sample}-#{dig.sample}#{dig.sample}#{dig.sample}-#{dig.sample}#{dig.sample}#{dig.sample}#{dig.sample}"
93
+ when 'input_text'
94
+ classes = field['class'].to_s.split(' ')
95
+
96
+ if classes.include?('date') # Let's assume this is a date input.
97
+ Faker::Date.backward(365).strftime('%y-%m-%d')
98
+ elsif classes.include?('datetime')
99
+ Faker::Date.backward(365).strftime('%y-%m-%d %H:%m')
100
+ elsif attributes.last.to_s.include?('first_name')
101
+ Faker::Name.first_name
102
+ elsif attributes.last.to_s.include?('last_name')
103
+ Faker::Name.last_name
104
+ elsif attributes.last.to_s.include?('name')
105
+ Faker::Name.name
106
+ else
107
+ Faker::Lorem.word
108
+ end
109
+ when 'select'
110
+ if fill_value.present? # It might be a value or a label
111
+ field.all('option').each do |option|
112
+ return option.text if option.text == fill_value || option.value.to_s == fill_value.to_s
113
+ end
114
+ end
115
+
116
+ field.all('option').select { |option| option.value.present? }.sample.try(:text) || ''
117
+ when 'textarea'
118
+ Faker::Lorem.sentence
119
+ when 'input_checkbox'
120
+ [true, false].sample
121
+ when 'input_radio'
122
+ binding.pry
123
+ else
124
+ raise "fill_value unsupported field type: #{field['type']}"
125
+ end
126
+ end
127
+
128
+ def submit_form(label = nil)
129
+ if label.present?
130
+ click_on(label)
131
+ #find_field(label).click
132
+ else
133
+ first(:css, "input[type='submit']").click
134
+ end
135
+ end
136
+
137
+ end
@@ -7,5 +7,12 @@ module EffectiveTestBot
7
7
  # Set up our defaults, as per our initializer template
8
8
  eval File.read("#{config.root}/lib/generators/templates/effective_test_bot.rb")
9
9
  end
10
+
11
+ initializer 'effective_test_bot.test_suite' do |app|
12
+ Rails.application.config.to_prepare do
13
+ ActionDispatch::IntegrationTest.send(:include, EffectiveTestBotHelper)
14
+ end
15
+ end
16
+
10
17
  end
11
18
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveTestBot
2
- VERSION = '0.0.7'.freeze
2
+ VERSION = '0.0.8'.freeze
3
3
  end
@@ -1,17 +1,51 @@
1
- ENV["RAILS_ENV"] = "test"
1
+ ENV['RAILS_ENV'] = 'test'
2
2
  require File.expand_path("../../config/environment", __FILE__)
3
- require "rails/test_help"
4
- require "minitest/rails"
3
+ require 'rails/test_help'
4
+ require 'minitest/rails'
5
+ require 'minitest/rails/capybara'
6
+ require 'minitest/pride'
7
+ require 'minitest/reporters'
5
8
 
6
- # To add Capybara feature tests add `gem "minitest-rails-capybara"`
7
- # to the test group in the Gemfile and uncomment the following:
8
- require "minitest/rails/capybara"
9
+ require 'shoulda-matchers'
10
+ require 'shoulda'
9
11
 
10
- # Uncomment for awesome colorful output
11
- require "minitest/pride"
12
+ require 'capybara/webkit'
13
+ require 'capybara-screenshot/minitest'
12
14
 
13
15
  class ActiveSupport::TestCase
14
16
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
15
17
  fixtures :all
16
18
  # Add more helper methods to be used by all tests here...
17
19
  end
20
+
21
+ class ActionDispatch::IntegrationTest
22
+ # Make the Capybara DSL available in all integration tests
23
+ include Capybara::DSL
24
+ include Capybara::Assertions
25
+ include Capybara::Screenshot::MiniTestPlugin
26
+ include Warden::Test::Helpers if defined?(Devise)
27
+ end
28
+
29
+ Capybara.default_driver = :webkit
30
+ Capybara.javascript_driver = :webkit
31
+ Capybara::Screenshot.autosave_on_failure = true
32
+ Capybara::Screenshot.prune_strategy = :keep_last_run
33
+ Capybara::Screenshot.webkit_options = { width: 1024, height: 768 }
34
+
35
+ Rake::Task['db:seed'].invoke
36
+
37
+ #Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
38
+
39
+
40
+
41
+ # # Make all database transactions use the same thread
42
+ # class ActiveRecord::Base
43
+ # mattr_accessor :shared_connection
44
+ # @@shared_connection = nil
45
+
46
+ # def self.connection
47
+ # @@shared_connection || retrieve_connection
48
+ # end
49
+ # end
50
+
51
+ # ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
@@ -25,12 +25,12 @@ namespace :test do
25
25
  # Rails.application.load_seed
26
26
 
27
27
  Rails::TestTask.new('effective_test_bot' => 'test:prepare') do |t|
28
- puts "Read effective_test_bot rake task!"
29
-
30
- puts "#{File.dirname(__FILE__)}/../../test/**/*_test.rb"
31
- puts FileList["#{File.dirname(__FILE__)}/../../test/**/*_test.rb"]
32
-
33
28
  t.libs << 'test'
34
29
  t.test_files = FileList["#{File.dirname(__FILE__)}/../../test/**/*_test.rb"]
35
30
  end
36
31
  end
32
+
33
+ # Rake::Task['db:fixtures:load'].enhance do
34
+ # Rake::Task['db:seed'].invoke
35
+ # end
36
+
@@ -0,0 +1,84 @@
1
+ require 'test_helper'
2
+
3
+ if defined?(Devise) && defined?(User)
4
+ module TestBot
5
+ class SignInTest < ActionDispatch::IntegrationTest
6
+ let(:user) { users(:normal) }
7
+
8
+ test 'valid sign in' do
9
+ visit new_user_session_path
10
+
11
+ binding.pry
12
+
13
+ within('form#new_user') do
14
+ fill_in 'user_email', with: user.email
15
+ fill_in 'user_password', with: 'password'
16
+ find('input[type=submit]').click
17
+ end
18
+
19
+ assert_equal 200, page.status_code
20
+ assert_content I18n.t('devise.sessions.signed_in')
21
+
22
+ assert_equal 1, User.find_by_email(user.email).sign_in_count
23
+ end
24
+
25
+ test 'invalid sign in' do
26
+ visit new_user_session_path
27
+
28
+ binding.pry
29
+
30
+ within('form#new_user') do
31
+ fill_in 'user_email', with: user.email
32
+ fill_in 'user_password', with: 'not-correct-password'
33
+ find('input[type=submit]').click
34
+ end
35
+
36
+ assert_equal 200, page.status_code
37
+ assert_content I18n.t('devise.failure.invalid', authentication_keys: Devise.authentication_keys.join(', '))
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ # require 'rails_helper'
44
+
45
+ # if defined?(Devise) && defined?(User)
46
+
47
+ # feature 'Devise Sign In' do
48
+ # let(:user) do
49
+ # User.new(email: "user_#{Time.now.to_i}@example.com", password: '1234567890').tap { |u| u.save(validate: false) }
50
+ # end
51
+
52
+ # it 'allows a valid sign in' do
53
+ # visit new_user_session_path
54
+
55
+ # within('form#new_user') do
56
+ # fill_in 'user_email', with: user.email
57
+ # fill_in 'user_password', with: user.password
58
+
59
+ # find('input[type=submit]').click
60
+ # end
61
+
62
+ # expect(page.status_code).to eq 200
63
+ # expect(page).to have_content I18n.t('devise.sessions.signed_in')
64
+
65
+ # existing_user = User.find_by_email(user.email)
66
+ # expect(existing_user.sign_in_count).to eq 1
67
+
68
+ # it 'prevents sign in when provided an invalid password' do
69
+ # visit new_user_session_path
70
+
71
+ # within('form#new_user') do
72
+ # fill_in 'user_email', with: user.email
73
+ # fill_in 'user_password', with: 'invalid_password'
74
+
75
+ # find('input[type=submit]').click
76
+ # end
77
+
78
+ # expect(page.status_code).to eq 200
79
+ # expect(page).to have_content I18n.t('devise.failure.invalid', authentication_keys: Devise.authentication_keys.join(', '))
80
+ # end
81
+
82
+ # end
83
+
84
+ # end # /defined?(Devise)
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ module TestBot
4
+ class HomePageTest < ActionDispatch::IntegrationTest
5
+ test 'home page loads successfully' do
6
+ visit root_path
7
+ assert_equal page.status_code, 200
8
+ end
9
+
10
+
11
+ # let(:something) { User.new(:email => 'homepagetest@someone.com') }
12
+
13
+ # test "sanity" do
14
+ # visit root_path
15
+ # page.save_screenshot('something.png')
16
+
17
+ # assert_equal current_path, 'http://something.com', 'unexpected root_url'
18
+
19
+ # assert_content "Testbot home page spec!"
20
+ # end
21
+
22
+ # test "does something" do
23
+ # visit root_path
24
+ # page.must_have_content "Test bot home page spec!"
25
+ # end
26
+
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ module TestBot
4
+ class DatabaseTest < ActiveSupport::TestCase
5
+ test 'should be 1 user record available' do
6
+ assert_equal 1, User.count
7
+ end
8
+
9
+ test 'should be 0 user records available after delete' do
10
+ User.destroy_all
11
+ assert_equal 0, User.count
12
+ end
13
+
14
+ test 'should be 2 user records if I create one' do
15
+ assert_equal 1, User.count
16
+ assert User.create(:email => 'someone@somehting.com', :password => '123456789', :password_confirmation => '123456789')
17
+
18
+ assert_equal 2, User.count
19
+
20
+ end
21
+
22
+
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ if defined?(Devise) && defined?(User)
4
+ module TestBot
5
+ class UserTest < ActiveSupport::TestCase
6
+ let(:user) { User.new() }
7
+
8
+ should validate_presence_of(:email)
9
+ should validate_presence_of(:password)
10
+ should validate_presence_of(:encrypted_password)
11
+
12
+ test "user fails validation when password and confirmation mismatch" do
13
+ user.password = '123456789'
14
+ user.password_confirmation = '987654321'
15
+
16
+ refute user.valid?, 'user should be invalid with mismatched passwords'
17
+ end
18
+ end
19
+ end
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_test_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-08 00:00:00.000000000 Z
11
+ date: 2015-06-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest-reporters
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: capybara
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +150,34 @@ dependencies:
136
150
  - - ">="
137
151
  - !ruby/object:Gem::Version
138
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: shoulda-matchers
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: faker
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :runtime
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
139
181
  description: A shared library of rails model & capybara-based feature tests that should
140
182
  pass in every Rails application.
141
183
  email:
@@ -147,6 +189,7 @@ files:
147
189
  - MIT-LICENSE
148
190
  - README.md
149
191
  - Rakefile
192
+ - app/helpers/effective_test_bot_helper.rb
150
193
  - lib/effective_test_bot.rb
151
194
  - lib/effective_test_bot/engine.rb
152
195
  - lib/effective_test_bot/version.rb
@@ -161,8 +204,10 @@ files:
161
204
  - spec/features/devise/sign_up_spec.rb
162
205
  - spec/features/home_page_spec.rb
163
206
  - spec/models/user_spec.rb
164
- - test/features/home_page_test.rb
165
- - test/models/user_test.rb
207
+ - test/test_bot/integration/devise/sign_in_test.rb
208
+ - test/test_bot/integration/home_page_test.rb
209
+ - test/test_bot/models/database_test.rb
210
+ - test/test_bot/models/user_test.rb
166
211
  homepage: https://github.com/code-and-effect/effective_test_bot
167
212
  licenses:
168
213
  - MIT
@@ -1,9 +0,0 @@
1
- require "test_helper"
2
-
3
- class HomePageTest < Capybara::Rails::TestCase
4
- test "sanity" do
5
- visit root_path
6
- assert_content page, "Test bot home page spec!"
7
- end
8
-
9
- end
@@ -1,42 +0,0 @@
1
- require 'test_helper'
2
-
3
- if defined?(Devise) && defined?(User)
4
-
5
- class UserTest < ActiveSupport::TestCase
6
- #should validate_presence_of(:email)
7
- #should validate_presence_of(:password)
8
- #should validate_presence_of(:encrypted_password)
9
-
10
- def user
11
- User.new()
12
- end
13
-
14
- test "User fails validation when password and confirmation mismatch" do
15
- user.password = '123456789'
16
- user.password_confirmation = '987654321'
17
-
18
- assert user.valid?, 'Test bot model yo!'
19
- end
20
- end
21
- end
22
-
23
-
24
- # require 'rails_helper'
25
-
26
- # if defined?(Devise) && defined?(User)
27
-
28
- # describe User, type: :model do
29
- # let(:user) { User.new() }
30
-
31
- # it { should validate_presence_of(:email) }
32
- # it { should validate_presence_of(:password) }
33
- # it { should validate_presence_of(:encrypted_password) }
34
-
35
- # it 'fails validation when password and password_confirmation are different' do
36
- # user.password = '123456789'
37
- # user.password_confirmation = '987654321'
38
- # expect(user.valid?).to eq false
39
- # end
40
- # end
41
-
42
- # end # /defined?(Devise) && defined?(User)