Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation #7

Merged
merged 8 commits into from
Dec 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rubocop auto-fixes
  • Loading branch information
mikker committed Dec 27, 2017
commit f6c236226db2ab75f20033a21b4308ee23b3ea68
14 changes: 14 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
AllCops:
TargetRubyVersion: 2.4
Exclude:
- 'passwordless.gemspec'

Layout/AlignHash:
Exclude:
- 'Gemfile'

Style/IfUnlessModifier:
Enabled: false

Rails:
Enabled: true
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

begin
require 'bundler/setup'
rescue LoadError
Expand All @@ -8,7 +10,7 @@ require 'yard'
YARD::Rake::YardocTask.new
task docs: :yard

APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
APP_RAKEFILE = File.expand_path('../test/dummy/Rakefile', __FILE__)
load 'rails/tasks/engine.rake'
load 'rails/tasks/statistics.rake'

Expand Down
2 changes: 2 additions & 0 deletions app/controllers/passwordless/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Passwordless
class ApplicationController < ::ApplicationController
def passwordless_controller?
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/passwordless/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'bcrypt'

module Passwordless
Expand Down
4 changes: 3 additions & 1 deletion app/mailers/passwordless/mailer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Passwordless
class Mailer < ActionMailer::Base
default from: Passwordless.default_from_address
Expand All @@ -11,7 +13,7 @@ def magic_link(session)
send(authenticatable_resource_name).token_sign_in_url(session.token)

email_field = @session.authenticatable.class.passwordless_email_field
mail to: @session.authenticatable.send(email_field), subject: "Your magic link ✨"
mail to: @session.authenticatable.send(email_field), subject: 'Your magic link ✨'
end
end
end
2 changes: 2 additions & 0 deletions app/models/passwordless/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Passwordless
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
Expand Down
2 changes: 2 additions & 0 deletions app/models/passwordless/session.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Passwordless
class Session < ApplicationRecord
belongs_to :authenticatable, polymorphic: true
Expand Down
2 changes: 2 additions & 0 deletions bin/rails
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# This command will automatically be run when you run "rails" with Rails gems
# installed from the root of your application.

Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Passwordless::Engine.routes.draw do
get '/sign_in', to: 'sessions#new', as: :sign_in
post '/sign_in', to: 'sessions#create'
Expand Down
2 changes: 2 additions & 0 deletions db/migrate/20171104221735_create_passwordless_sessions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class CreatePasswordlessSessions < ActiveRecord::Migration[5.1]
def change
create_table :passwordless_sessions do |t|
Expand Down
2 changes: 2 additions & 0 deletions lib/passwordless.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'passwordless/engine'
require 'passwordless/url_safe_base_64_generator'

Expand Down
2 changes: 2 additions & 0 deletions lib/passwordless/controller_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Passwordless
module ControllerHelpers
def authenticate_by_cookie(authenticatable_class)
Expand Down
2 changes: 2 additions & 0 deletions lib/passwordless/engine.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Passwordless
class Engine < ::Rails::Engine
isolate_namespace Passwordless
Expand Down
2 changes: 2 additions & 0 deletions lib/passwordless/model_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Passwordless
module ModelHelpers
def passwordless_with(field)
Expand Down
2 changes: 2 additions & 0 deletions lib/passwordless/router_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Passwordless
module RouterHelpers
def passwordless_for(resource, at: nil, as: nil)
Expand Down
2 changes: 2 additions & 0 deletions lib/passwordless/url_safe_base_64_generator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Passwordless
class UrlSafeBase64Generator
def call(_session)
Expand Down
2 changes: 2 additions & 0 deletions lib/passwordless/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Passwordless
VERSION = '0.4.2'
end
1 change: 1 addition & 0 deletions lib/tasks/passwordless_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
# desc "Explaining what the task does"
# task :passwordless do
# # Task goes here
Expand Down
46 changes: 23 additions & 23 deletions test/controllers/passwordless/sessions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
# frozen_string_literal: true

require 'test_helper'

module Passwordless
class SessionsControllerTest < ActionDispatch::IntegrationTest
def create_session_for user
def create_session_for(user)
Session.create!(
authenticatable: user,
remote_addr: 'yes',
user_agent: 'James Bond'
)
end

test "requesting a magic link as an existing user" do
test 'requesting a magic link as an existing user' do
user = User.create email: 'a@a'

get "/users/sign_in"
get '/users/sign_in'
assert_equal 200, status

post "/users/sign_in", {
params: { passwordless: { email: user.email } },
headers: { 'User-Agent': 'an actual monkey' }
}
post '/users/sign_in',
params: { passwordless: { email: user.email } },
headers: { 'User-Agent': 'an actual monkey' }
assert_equal 200, status

assert_equal 1, ActionMailer::Base.deliveries.size
end

test "requesting a magic link as an unknown user" do
get "/users/sign_in"
test 'requesting a magic link as an unknown user' do
get '/users/sign_in'
assert_equal 200, status

post "/users/sign_in", {
params: { passwordless: { email: 'something_em@ilish' } },
headers: { 'User-Agent': 'an actual monkey' }
}
post '/users/sign_in',
params: { passwordless: { email: 'something_em@ilish' } },
headers: { 'User-Agent': 'an actual monkey' }
assert_equal 200, status

assert_equal 0, ActionMailer::Base.deliveries.size
end

test "signing in via a token" do
test 'signing in via a token' do
user = User.create email: 'a@a'
session = create_session_for user

get "/users/sign_in/#{session.token}"
follow_redirect!

assert_equal 200, status
assert_equal "/", path
assert_equal '/', path
refute_nil cookies[:user_id]
end

test "signing in and redirecting back" do
test 'signing in and redirecting back' do
user = User.create! email: 'a@a'

get "/secret"
get '/secret'
assert_equal 302, status

follow_redirect!
Expand All @@ -67,13 +67,13 @@ def create_session_for user
assert_equal '/secret', path
end

test "disabling redirecting back after sign in" do
test 'disabling redirecting back after sign in' do
_default = Passwordless.redirect_back_after_sign_in
Passwordless.redirect_back_after_sign_in = false

user = User.create! email: 'a@a'

get "/secret"
get '/secret'
assert_equal 302, status

follow_redirect!
Expand All @@ -88,13 +88,13 @@ def create_session_for user
Passwordless.redirect_back_after_sign_in = _default
end

test "trying to sign in with an unknown token" do
test 'trying to sign in with an unknown token' do
assert_raise ActiveRecord::RecordNotFound do
get "/users/sign_in/twin-hotdogs"
get '/users/sign_in/twin-hotdogs'
end
end

test "signing out" do
test 'signing out' do
user = User.create email: 'a@a'

session = create_session_for user
Expand All @@ -105,7 +105,7 @@ def create_session_for user
follow_redirect!

assert_equal 200, status
assert_equal "/", path
assert_equal '/', path
assert cookies[:user_id].blank?
end
end
Expand Down
2 changes: 2 additions & 0 deletions test/dummy/Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

Expand Down
2 changes: 2 additions & 0 deletions test/dummy/app/channels/application_cable/channel.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ApplicationCable
class Channel < ActionCable::Channel::Base
end
Expand Down
2 changes: 2 additions & 0 deletions test/dummy/app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ApplicationCable
class Connection < ActionCable::Connection::Base
end
Expand Down
4 changes: 3 additions & 1 deletion test/dummy/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ApplicationController < ActionController::Base
include Passwordless::ControllerHelpers

Expand All @@ -16,6 +18,6 @@ def authenticate_user!

save_passwordless_redirect_location!(User)

redirect_to root_path, flash: {error: 'Not worthy!'}
redirect_to root_path, flash: { error: 'Not worthy!' }
end
end
2 changes: 2 additions & 0 deletions test/dummy/app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class RegistrationsController < ApplicationController
def new
@user = User.new
Expand Down
2 changes: 2 additions & 0 deletions test/dummy/app/controllers/secrets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class SecretsController < ApplicationController
before_action :authenticate_user!

Expand Down
2 changes: 2 additions & 0 deletions test/dummy/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class UsersController < ApplicationController
def index
@users = User.all
Expand Down
2 changes: 2 additions & 0 deletions test/dummy/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

module ApplicationHelper
end
2 changes: 2 additions & 0 deletions test/dummy/app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

module UsersHelper
end
2 changes: 2 additions & 0 deletions test/dummy/app/jobs/application_job.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

class ApplicationJob < ActiveJob::Base
end
2 changes: 2 additions & 0 deletions test/dummy/app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ApplicationMailer < ActionMailer::Base
default from: '[email protected]'
layout 'mailer'
Expand Down
2 changes: 2 additions & 0 deletions test/dummy/app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
2 changes: 2 additions & 0 deletions test/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class User < ApplicationRecord
passwordless_with :email
end
2 changes: 2 additions & 0 deletions test/dummy/bin/bundle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
load Gem.bin_path('bundler', 'bundle')
2 changes: 2 additions & 0 deletions test/dummy/bin/rails
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
2 changes: 2 additions & 0 deletions test/dummy/bin/rake
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative '../config/boot'
require 'rake'
Rake.application.run
3 changes: 2 additions & 1 deletion test/dummy/bin/setup
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'pathname'
require 'fileutils'
include FileUtils
Expand All @@ -21,7 +23,6 @@ chdir APP_ROOT do
# Install JavaScript dependencies if using Yarn
# system('bin/yarn')


# puts "\n== Copying sample files =="
# unless File.exist?('config/database.yml')
# cp 'config/database.yml.sample', 'config/database.yml'
Expand Down
2 changes: 2 additions & 0 deletions test/dummy/bin/update
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'pathname'
require 'fileutils'
include FileUtils
Expand Down
Loading