diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..b754bc1 --- /dev/null +++ b/.env.example @@ -0,0 +1,7 @@ +DOCKER_REGISTRY_HOST=docker.io +DOCKER_IMAGE=mosharaf13/google_scrapper_ruby +BRANCH_TAG=latest +PORT=80 +CI=true +TEST_RETRY=0 +RAILS_SERVE_STATIC_FILES=true diff --git a/Dockerfile b/Dockerfile index ef63398..914a272 100644 --- a/Dockerfile +++ b/Dockerfile @@ -52,6 +52,9 @@ RUN if [ "$BUILD_ENV" = "test" ]; then \ WORKDIR $APP_HOME +# Copy ENV file +COPY .env.example .env + # Skip installing gem documentation RUN mkdir -p /usr/local/etc \ && { \ diff --git a/app/views/devise/sessions/new.erb b/app/views/devise/sessions/new.erb new file mode 100644 index 0000000..ac9ee2a --- /dev/null +++ b/app/views/devise/sessions/new.erb @@ -0,0 +1,33 @@ +
+

Log in

+ +
+
+ <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> +
+ <%= f.label :email %>
+ <%= f.email_field :email, autofocus: true, autocomplete: 'email', class: 'form-control' %> +
+ +
+ <%= f.label :password %>
+ <%= f.password_field :password, autocomplete: 'current-password', class: 'form-control' %> +
+ + <% if devise_mapping.rememberable? %> +
+ <%= f.check_box :remember_me, class: 'form-check-input' %> + <%= f.label :remember_me, class: 'form-check-label' %> +
+ <% end %> + +
+
+ <%= f.submit 'Log in', class: 'btn btn-primary m-1' %> +
+ <%= render 'devise/shared/links' %> +
+ <% end %> +
+
+
diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index 88980bb..c8f153e 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -1,9 +1,3 @@ -<% if user_signed_in? %> -
Welcome <%= current_user.email %>
- <%= button_to 'Sign out', destroy_user_session_path, method: :delete %> -<% else %> - <%= button_to 'Sign in', new_user_session_path %> -<% end %>

Welcome to the world of scrapping!!

diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 2a1935a..0da2e62 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -6,13 +6,19 @@ <%= csrf_meta_tags %> <%= csp_meta_tag %> - <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> - <%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %> - + <%= stylesheet_link_tag 'application', 'data-turbo-track': 'reload' %> + <%= javascript_include_tag 'application', 'data-turbo-track': 'reload', defer: true %> + -

<%= notice %>

-

<%= alert %>

- <%= yield %> + <%= render 'shared/header' %> + +
+
+ <%= render 'shared/flashes' %> + + <%= yield %> +
+
diff --git a/app/views/shared/_flashes.erb b/app/views/shared/_flashes.erb new file mode 100644 index 0000000..40e99f3 --- /dev/null +++ b/app/views/shared/_flashes.erb @@ -0,0 +1,7 @@ +<% if notice %> +

<%= notice %>

+<% end %> + +<% if alert %> +

<%= alert %>

+<% end %> diff --git a/app/views/shared/_header.erb b/app/views/shared/_header.erb new file mode 100644 index 0000000..8770bf1 --- /dev/null +++ b/app/views/shared/_header.erb @@ -0,0 +1,14 @@ +
+
+
+ <%= link_to 'Google Scrapper', root_url, class: 'navbar-brand col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0' %> + + <% if user_signed_in? %> +
Welcome <%= current_user.email %>
+ <%= button_to 'Sign out', destroy_user_session_path, method: :delete, class: 'btn btn-outline-light sign-out-button' %> + <% else %> + <%= button_to 'Sign in', new_user_session_path, method: :get, class: 'btn btn-outline-light sign-in-button' %> + <% end %> +
+
+
diff --git a/spec/support/devise.rb b/spec/support/devise.rb new file mode 100644 index 0000000..f2d43a6 --- /dev/null +++ b/spec/support/devise.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +RSpec.configure do |config| + config.include Devise::Test::ControllerHelpers, type: :view + config.include Devise::Test::ControllerHelpers, type: :helper + config.include Devise::Test::IntegrationHelpers, type: :feature + config.include Devise::Test::IntegrationHelpers, type: :request +end diff --git a/spec/support/system.rb b/spec/support/system.rb index 5001ae7..d4144fe 100644 --- a/spec/support/system.rb +++ b/spec/support/system.rb @@ -2,6 +2,8 @@ RSpec.configure do |config| config.before(:each, type: :system) do + config.include Warden::Test::Helpers, type: :system + # use headless_chrome, by default driven_by :headless_chrome diff --git a/spec/support/user_authentication.rb b/spec/support/user_authentication.rb new file mode 100644 index 0000000..9f7080d --- /dev/null +++ b/spec/support/user_authentication.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +def login_as_user(user = Fabricate(:user)) + login_as user +end diff --git a/spec/systems/authentications/sign_in/sign_in_spec.rb b/spec/systems/authentications/sign_in/sign_in_spec.rb new file mode 100644 index 0000000..83a0a80 --- /dev/null +++ b/spec/systems/authentications/sign_in/sign_in_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'Sign in page', type: :system do + let(:selectors) do + { + auth_form: '.new_user', + email_field: 'user[email]', + password_field: 'user[password]' + } + end + + context 'given the Sign in button is clicked' do + context 'given the credentials are valid' do + it 'displays the home page' do + visit new_user_session_path + user = Fabricate.build(:user) + submit_authentication_form(user.email, user.password) + + expect(page).to have_current_path(root_path) + end + end + + context 'given the credentials are invalid' do + it 'displays the Sign in page' do + visit new_user_session_path + user = Fabricate.build(:user, password: 'password') + + submit_authentication_form(user.email, 'invalid') + + expect(page).to have_current_path(root_path) + end + + it 'shows the error message' do + visit new_user_session_path + user = Fabricate.build(:user, password: 'password') + + submit_authentication_form(user.email, 'invalid') + + expect(page).to have_text 'Invalid Email or password.' + end + end + end + + private + + def submit_authentication_form(email, password) + within selectors[:auth_form] do + fill_in selectors[:email_field], with: email + fill_in selectors[:password_field], with: password + click_button 'Log in' + end + end +end diff --git a/spec/systems/authentications/sign_out/sign_out_spec.rb b/spec/systems/authentications/sign_out/sign_out_spec.rb new file mode 100644 index 0000000..3802d6f --- /dev/null +++ b/spec/systems/authentications/sign_out/sign_out_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'Sign out', type: :system do + let(:selectors) do + { + sign_out_button: '.sign-out-button' + } + end + + it 'redirects to the root page' do + login_as_user + visit root_path + + click_link selectors[:sign_out_button] + + expect(page).to have_current_path(root_path) + end +end