diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/Gemfile.lock b/Gemfile.lock index c384b64..6dda87c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,36 +2,13 @@ PATH remote: . specs: icalialabs-firebase (0.1.0) - rails (>= 7.0.4) + rspec + rspec-rails + rubocop GEM remote: https://rubygems.org/ specs: - actioncable (7.0.4) - actionpack (= 7.0.4) - activesupport (= 7.0.4) - nio4r (~> 2.0) - websocket-driver (>= 0.6.1) - actionmailbox (7.0.4) - actionpack (= 7.0.4) - activejob (= 7.0.4) - activerecord (= 7.0.4) - activestorage (= 7.0.4) - activesupport (= 7.0.4) - mail (>= 2.7.1) - net-imap - net-pop - net-smtp - actionmailer (7.0.4) - actionpack (= 7.0.4) - actionview (= 7.0.4) - activejob (= 7.0.4) - activesupport (= 7.0.4) - mail (~> 2.5, >= 2.5.4) - net-imap - net-pop - net-smtp - rails-dom-testing (~> 2.0) actionpack (7.0.4) actionview (= 7.0.4) activesupport (= 7.0.4) @@ -39,85 +16,40 @@ GEM rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.2.0) - actiontext (7.0.4) - actionpack (= 7.0.4) - activerecord (= 7.0.4) - activestorage (= 7.0.4) - activesupport (= 7.0.4) - globalid (>= 0.6.0) - nokogiri (>= 1.8.5) actionview (7.0.4) activesupport (= 7.0.4) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) - activejob (7.0.4) - activesupport (= 7.0.4) - globalid (>= 0.3.6) - activemodel (7.0.4) - activesupport (= 7.0.4) - activerecord (7.0.4) - activemodel (= 7.0.4) - activesupport (= 7.0.4) - activestorage (7.0.4) - actionpack (= 7.0.4) - activejob (= 7.0.4) - activerecord (= 7.0.4) - activesupport (= 7.0.4) - marcel (~> 1.0) - mini_mime (>= 1.1.0) activesupport (7.0.4) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) + ast (2.4.2) builder (3.2.4) concurrent-ruby (1.1.10) crass (1.0.6) + diff-lcs (1.5.0) erubi (1.11.0) - globalid (1.0.0) - activesupport (>= 5.0) i18n (1.12.0) concurrent-ruby (~> 1.0) + json (2.6.2) loofah (2.19.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) - mail (2.7.1) - mini_mime (>= 0.1.1) - marcel (1.0.2) method_source (1.0.0) - mini_mime (1.1.2) minitest (5.16.3) - net-imap (0.3.1) - net-protocol - net-pop (0.1.2) - net-protocol - net-protocol (0.1.3) - timeout - net-smtp (0.3.2) - net-protocol - nio4r (2.5.8) nokogiri (1.13.8-x86_64-linux) racc (~> 1.4) + parallel (1.22.1) + parser (3.1.2.1) + ast (~> 2.4.1) racc (1.6.0) rack (2.2.4) rack-test (2.0.2) rack (>= 1.3) - rails (7.0.4) - actioncable (= 7.0.4) - actionmailbox (= 7.0.4) - actionmailer (= 7.0.4) - actionpack (= 7.0.4) - actiontext (= 7.0.4) - actionview (= 7.0.4) - activejob (= 7.0.4) - activemodel (= 7.0.4) - activerecord (= 7.0.4) - activestorage (= 7.0.4) - activesupport (= 7.0.4) - bundler (>= 1.15.0) - railties (= 7.0.4) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) @@ -130,7 +62,44 @@ GEM rake (>= 12.2) thor (~> 1.0) zeitwerk (~> 2.5) + rainbow (3.1.1) rake (13.0.6) + regexp_parser (2.6.0) + rexml (3.2.5) + rspec (3.11.0) + rspec-core (~> 3.11.0) + rspec-expectations (~> 3.11.0) + rspec-mocks (~> 3.11.0) + rspec-core (3.11.0) + rspec-support (~> 3.11.0) + rspec-expectations (3.11.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.11.0) + rspec-mocks (3.11.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.11.0) + rspec-rails (6.0.0) + actionpack (>= 6.1) + activesupport (>= 6.1) + railties (>= 6.1) + rspec-core (~> 3.11) + rspec-expectations (~> 3.11) + rspec-mocks (~> 3.11) + rspec-support (~> 3.11) + rspec-support (3.11.1) + rubocop (1.36.0) + json (~> 2.3) + parallel (~> 1.10) + parser (>= 3.1.2.1) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.20.1, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 3.0) + rubocop-ast (1.21.0) + parser (>= 3.1.1.0) + ruby-progressbar (1.11.0) sprockets (4.1.1) concurrent-ruby (~> 1.0) rack (> 1, < 3) @@ -139,12 +108,9 @@ GEM activesupport (>= 5.2) sprockets (>= 3.0.0) thor (1.2.1) - timeout (0.3.0) tzinfo (2.0.5) concurrent-ruby (~> 1.0) - websocket-driver (0.7.5) - websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.5) + unicode-display_width (2.3.0) zeitwerk (2.6.1) PLATFORMS diff --git a/icalialabs-firebase.gemspec b/icalialabs-firebase.gemspec index 2ebe810..3c0460e 100644 --- a/icalialabs-firebase.gemspec +++ b/icalialabs-firebase.gemspec @@ -22,5 +22,9 @@ Gem::Specification.new do |spec| Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"] end - spec.add_dependency "rails", ">= 7.0.4" + spec.add_dependency "rubocop" + spec.add_dependency "rspec" + spec.add_dependency "rspec-rails" + spec.add_dependency "capybara" + spec.add_dependency "cuprite" end diff --git a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb index cf63802..462bcf1 100644 --- a/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb +++ b/lib/generators/icalialabs_firebase/icalialabs_firebase_generator.rb @@ -1,41 +1,55 @@ class IcalialabsFirebaseGenerator < Rails::Generators::NamedBase - source_root File.expand_path("../../../", __dir__) + source_root File.expand_path('../../../', __dir__) def create_views - # ---------- Install argument. ------------- - if class_name == 'Install' - template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb', - File.join('app/controllers', class_path, "firebase_controller.rb") - route "get '/login', to: 'firebase#login'" - route "get '/signup', to: 'firebase#signup'" - route "post '/create_user', to: 'firebase#create_user'" - route "post '/create_session', to: 'firebase#create_session'" - route "delete '/logout', to: 'firebase#logout', as: 'logout'" - inject_into_file 'app/controllers/application_controller.rb', - after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY' + case class_name + when 'Install' + copy_templates_with_install + firebase_routes + inject_code_with_install + when 'Views' + directory 'app/views/firebase/', 'app/views/firebase/' + views_readme + else + error_readme + end + end + + private + + def copy_templates_with_install + template 'lib/generators/icalialabs_firebase/templates/firebase_controller.rb', + File.join('app/controllers', class_path, 'firebase_controller.rb') + template 'lib/generators/icalialabs_firebase/templates/firebase_spec.rb', + File.join('app/spec/system', class_path, 'firebase_spec.rb') + end + + def firebase_routes + route "get '/login', to: 'firebase#login', as: 'login'" + route "get '/signup', to: 'firebase#signup', as: 'signup'" + route "post '/create_user', to: 'firebase#create_user'" + route "post '/create_session', to: 'firebase#create_session'" + route "delete '/logout', to: 'firebase#logout', as: 'logout'" + end + + def inject_code_with_install + inject_into_file 'app/controllers/application_controller.rb', + after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY' helper_method :current_user def current_user @current_user ||= session[:user_id] end - RUBY + RUBY + end end - # ---------- Views argument. ------------- - elsif class_name == 'Views' - directory 'app/views/firebase/', 'app/views/firebase/' - inject_into_file 'app/views/layouts/application.html.erb', - after: "\n" do <<-"RUBY" - <%= notice if notice %> - <% if current_user.present? %> - <%= button_to "Log Out", logout_path, to: "pages#logout", method: :delete %> - <% else %> - <%= button_to 'Log in', login_path, method: :get %> - <%= button_to 'Sign up', signup_path, method: :get %> - <% end %> - RUBY + def views_readme + readme "lib/generators/icalialabs_firebase/templates/views_readme.md" end - end + + def error_readme + readme "lib/generators/icalialabs_firebase/templates/error_readme.md" end end diff --git a/lib/generators/icalialabs_firebase/templates/error_readme.md b/lib/generators/icalialabs_firebase/templates/error_readme.md new file mode 100644 index 0000000..dafa429 --- /dev/null +++ b/lib/generators/icalialabs_firebase/templates/error_readme.md @@ -0,0 +1,11 @@ + +====================================== + +Sorry, that command is not recognized. +Try one of the commands below: + + - rails g icalialabs_firebase install + - rails g icalialabs_firebase views + +====================================== + diff --git a/lib/generators/icalialabs_firebase/templates/firebase_controller.rb b/lib/generators/icalialabs_firebase/templates/firebase_controller.rb index f3fc0c0..a477d53 100644 --- a/lib/generators/icalialabs_firebase/templates/firebase_controller.rb +++ b/lib/generators/icalialabs_firebase/templates/firebase_controller.rb @@ -10,32 +10,36 @@ def login; end def signup; end def create_user - uri = URI("https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=HERE_GOES_MY_API_KEY") + uri = URI('https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=HERE_GOES_MY_API_KEY') response = Net::HTTP.post_form(uri, "email": @email, "password": @password) data = JSON.parse(response.body) - session[:user_id] = data["localId"] + session[:user_id] = data['localId'] session[:data] = data - redirect_to root_path, notice: "Sign Up successfully." if response.is_a?(Net::HTTPSuccess) + if response.is_a?(Net::HTTPSuccess) + redirect_to root_path, notice: 'Sign Up successfully.' + else + redirect_to signup_path, notice: 'Something happend, try again.' + end end def create_session - uri = URI("https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=HERE_GOES_MY_API_KEY") + uri = URI('https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=HERE_GOES_MY_API_KEY') response = Net::HTTP.post_form(uri, "email": @email, "password": @password) data = JSON.parse(response.body) if response.is_a?(Net::HTTPSuccess) - session[:user_id] = data["localId"] + session[:user_id] = data['localId'] session[:data] = data - redirect_to root_path, notice: "Log in successfully." + redirect_to root_path, notice: 'Log in successfully.' end end def logout session.clear - redirect_to root_path, notice: "Successfully log out" + redirect_to root_path, notice: 'Successfully log out' end private diff --git a/lib/generators/icalialabs_firebase/templates/firebase_spec.rb b/lib/generators/icalialabs_firebase/templates/firebase_spec.rb new file mode 100644 index 0000000..7a8bf67 --- /dev/null +++ b/lib/generators/icalialabs_firebase/templates/firebase_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'system_helper' + +RSpec.describe 'Firebase', type: :system do + scenario "User can see Sign Up page" do + visit signup_path + expect(page).to have_content 'Sign up' + end + + scenario "User can see Login page" do + visit login_path + expect(page).to have_content 'Log in' + end +end diff --git a/lib/generators/icalialabs_firebase/templates/views_readme.md b/lib/generators/icalialabs_firebase/templates/views_readme.md new file mode 100644 index 0000000..1572ef0 --- /dev/null +++ b/lib/generators/icalialabs_firebase/templates/views_readme.md @@ -0,0 +1,20 @@ + +======================================================= + +Insert this code below your body tag in 'app/views/layouts/application.html.erb': + +-------- + <%= notice if notice %> + + <% if current_user.present? %> + <%= button_to 'Log Out', logout_path, to: 'pages#logout', method: :delete, id: 'logout_button' %> + <% else %> + <%= button_to 'Log in', login_path, method: :get, id: 'login_button' %> + <%= button_to 'Sign up', signup_path, method: :get, id: 'signup_button' %> + <% end %> +--------- + + - Notice for showing messages. + - Buttons for Log in, Sign up and Log out. + +======================================================= diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..7951aa6 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,64 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +require File.expand_path('../dummy/config/environment', __FILE__) +require 'spec_helper' +ENV['RAILS_ENV'] ||= 'test' +require_relative '../config/environment' +# Prevent database truncation if the environment is production +abort("The Rails environment is running in production mode!") if Rails.env.production? +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } + +# Checks for pending migrations and applies them before tests are run. +# If you are not using ActiveRecord, you can remove these lines. +begin + ActiveRecord::Migration.maintain_test_schema! +rescue ActiveRecord::PendingMigrationError => e + abort e.to_s.strip +end +RSpec.configure do |config| + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{::Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # You can uncomment this line to turn off ActiveRecord support entirely. + # config.use_active_record = false + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, type: :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..a0d4080 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,94 @@ +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # https://relishapp.com/rspec/rspec-core/docs/configuration/zero-monkey-patching-mode + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end