pollett 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +81 -0
  4. data/Rakefile +25 -0
  5. data/app/controllers/concerns/pollett/controller.rb +37 -0
  6. data/app/controllers/pollett/application_controller.rb +4 -0
  7. data/app/controllers/pollett/keys_controller.rb +5 -0
  8. data/app/controllers/pollett/sessions_controller.rb +5 -0
  9. data/app/controllers/pollett/users_controller.rb +5 -0
  10. data/app/mailers/pollett/mailer.rb +5 -0
  11. data/app/models/concerns/pollett/user.rb +40 -0
  12. data/app/models/pollett/context.rb +5 -0
  13. data/app/models/pollett/key.rb +5 -0
  14. data/app/models/pollett/session.rb +5 -0
  15. data/app/serializers/pollett/key_serializer.rb +5 -0
  16. data/app/serializers/pollett/session_serializer.rb +5 -0
  17. data/app/serializers/user_serializer.rb +3 -0
  18. data/app/services/pollett/authenticate_user.rb +5 -0
  19. data/app/services/pollett/change_password.rb +5 -0
  20. data/app/services/pollett/create_session.rb +5 -0
  21. data/app/services/pollett/register_user.rb +5 -0
  22. data/app/services/pollett/reset_password.rb +5 -0
  23. data/app/views/pollett/mailer/reset.text.erb +5 -0
  24. data/app/views/pollett/mailer/welcome.text.erb +1 -0
  25. data/config/locales/en.yml +14 -0
  26. data/config/routes.rb +11 -0
  27. data/db/migrate/20150226024506_create_pollett_contexts.rb +21 -0
  28. data/lib/generators/pollett/install/install_generator.rb +123 -0
  29. data/lib/generators/pollett/install/templates/db/migrate/add_pollett_to_users.rb +21 -0
  30. data/lib/generators/pollett/install/templates/db/migrate/create_users.rb +15 -0
  31. data/lib/generators/pollett/install/templates/initializer.rb +3 -0
  32. data/lib/generators/pollett/install/templates/user.rb +3 -0
  33. data/lib/pollett.rb +20 -0
  34. data/lib/pollett/concerns.rb +5 -0
  35. data/lib/pollett/concerns/controllers.rb +3 -0
  36. data/lib/pollett/concerns/controllers/keys_controller.rb +37 -0
  37. data/lib/pollett/concerns/controllers/sessions_controller.rb +43 -0
  38. data/lib/pollett/concerns/controllers/users_controller.rb +28 -0
  39. data/lib/pollett/concerns/mailers.rb +1 -0
  40. data/lib/pollett/concerns/mailers/mailer.rb +24 -0
  41. data/lib/pollett/concerns/models.rb +3 -0
  42. data/lib/pollett/concerns/models/context.rb +42 -0
  43. data/lib/pollett/concerns/models/key.rb +13 -0
  44. data/lib/pollett/concerns/models/session.rb +20 -0
  45. data/lib/pollett/concerns/serializers.rb +2 -0
  46. data/lib/pollett/concerns/serializers/context_serializer.rb +21 -0
  47. data/lib/pollett/concerns/serializers/user_serializer.rb +16 -0
  48. data/lib/pollett/concerns/services.rb +5 -0
  49. data/lib/pollett/concerns/services/authenticate_user.rb +21 -0
  50. data/lib/pollett/concerns/services/change_password.rb +21 -0
  51. data/lib/pollett/concerns/services/create_session.rb +27 -0
  52. data/lib/pollett/concerns/services/register_user.rb +29 -0
  53. data/lib/pollett/concerns/services/reset_password.rb +27 -0
  54. data/lib/pollett/configuration.rb +34 -0
  55. data/lib/pollett/engine.rb +12 -0
  56. data/lib/pollett/rspec.rb +6 -0
  57. data/lib/pollett/testing/request_helper.rb +106 -0
  58. data/lib/pollett/version.rb +3 -0
  59. data/spec/dummy/README.rdoc +28 -0
  60. data/spec/dummy/Rakefile +6 -0
  61. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  62. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  63. data/spec/dummy/app/controllers/application_controller.rb +12 -0
  64. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  65. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  66. data/spec/dummy/app/models/user.rb +3 -0
  67. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  68. data/spec/dummy/app/views/layouts/mailer.text.erb +6 -0
  69. data/spec/dummy/bin/bundle +3 -0
  70. data/spec/dummy/bin/rails +4 -0
  71. data/spec/dummy/bin/rake +4 -0
  72. data/spec/dummy/bin/setup +29 -0
  73. data/spec/dummy/config.ru +4 -0
  74. data/spec/dummy/config/application.rb +32 -0
  75. data/spec/dummy/config/boot.rb +5 -0
  76. data/spec/dummy/config/database.yml +85 -0
  77. data/spec/dummy/config/environment.rb +5 -0
  78. data/spec/dummy/config/environments/development.rb +41 -0
  79. data/spec/dummy/config/environments/production.rb +79 -0
  80. data/spec/dummy/config/environments/test.rb +42 -0
  81. data/spec/dummy/config/initializers/active_model_serializers.rb +1 -0
  82. data/spec/dummy/config/initializers/assets.rb +11 -0
  83. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  84. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  85. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  86. data/spec/dummy/config/initializers/inflections.rb +16 -0
  87. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  88. data/spec/dummy/config/initializers/session_store.rb +3 -0
  89. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  90. data/spec/dummy/config/locales/en.yml +23 -0
  91. data/spec/dummy/config/routes.rb +3 -0
  92. data/spec/dummy/config/secrets.yml +22 -0
  93. data/spec/dummy/db/migrate/20150226030314_enable_uuid_extension.rb +5 -0
  94. data/spec/dummy/db/migrate/20150226030315_create_users.rb +15 -0
  95. data/spec/dummy/db/migrate/20150226030316_create_pollett_contexts.pollett.rb +22 -0
  96. data/spec/dummy/db/schema.rb +49 -0
  97. data/spec/dummy/log/development.log +1315 -0
  98. data/spec/dummy/log/test.log +181283 -0
  99. data/spec/dummy/public/404.html +67 -0
  100. data/spec/dummy/public/422.html +67 -0
  101. data/spec/dummy/public/500.html +66 -0
  102. data/spec/dummy/public/favicon.ico +0 -0
  103. data/spec/factories/pollett_context.rb +5 -0
  104. data/spec/factories/pollett_key.rb +5 -0
  105. data/spec/factories/pollett_session.rb +5 -0
  106. data/spec/factories/user.rb +7 -0
  107. data/spec/mailers/pollett/mailer_spec.rb +73 -0
  108. data/spec/rails_helper.rb +19 -0
  109. data/spec/requests/keys_spec.rb +67 -0
  110. data/spec/requests/sessions_spec.rb +176 -0
  111. data/spec/requests/user_spec.rb +41 -0
  112. data/spec/spec_helper.rb +9 -0
  113. data/spec/support/email_helper.rb +9 -0
  114. metadata +323 -0
@@ -0,0 +1,27 @@
1
+ module Pollett
2
+ module Concerns
3
+ module Services
4
+ module ResetPassword
5
+ extend ActiveSupport::Concern
6
+
7
+ include Servitore::Service
8
+
9
+ included do
10
+ param_reader :email
11
+ end
12
+
13
+ def call
14
+ Pollett.config.user_model.find_by_normalized_email(email).tap do |user|
15
+ deliver_email(user) if user
16
+ end
17
+ end
18
+
19
+ private
20
+ def deliver_email(user)
21
+ user.update!(reset_token: Pollett.generate_token)
22
+ Mailer.reset(user).deliver_later
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,34 @@
1
+ module Pollett
2
+ def self.configure
3
+ yield config
4
+ end
5
+
6
+ def self.config
7
+ @config ||= Configuration.new
8
+ end
9
+
10
+ class Configuration
11
+ attr_accessor :user_model,
12
+ :minimum_password_length,
13
+ :send_welcome_email,
14
+ :parent_mailer,
15
+ :from_email,
16
+ :reset_url,
17
+ :whitelist,
18
+ :timeout
19
+
20
+ def initialize
21
+ @user_model = ::User
22
+ @minimum_password_length = 8
23
+ @send_welcome_email = true
24
+ @parent_mailer = ::ApplicationMailer
25
+ @reset_url = ->(token) { "https://example.com/#{token}/reset" }
26
+ @whitelist = []
27
+ @timeout = 2.weeks
28
+ end
29
+
30
+ def user_model_name
31
+ "::#{user_model.name}"
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ module Pollett
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Pollett
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec, fixture: false
7
+ g.fixture_replacement :factory_girl, dir: "spec/factories"
8
+ g.assets false
9
+ g.helper false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ require "rspec/rails"
2
+ require "pollett/testing/request_helper"
3
+
4
+ RSpec.configure do |config|
5
+ config.include Pollett::Testing::RequestHelper, type: :request
6
+ end
@@ -0,0 +1,106 @@
1
+ module Pollett
2
+ module Testing
3
+ module RequestHelper
4
+ module ClassMethods
5
+ def it_requires_authentication(method, path)
6
+ it "requires authentication" do
7
+ begin
8
+ json_request(method, path)
9
+ expect_status(401)
10
+ rescue Pollett::Unauthorized
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ module InstanceMethods
17
+ def json
18
+ @json ||= JSON.parse(response.body, symbolize_names: true)
19
+ end
20
+
21
+ def data
22
+ @data ||= json[:data]
23
+ end
24
+
25
+ def errors
26
+ @errors ||= json[:errors]
27
+ end
28
+
29
+ def meta
30
+ @meta ||= json[:meta]
31
+ end
32
+
33
+ def jsonapi
34
+ @jsonapi ||= json[:jsonapi]
35
+ end
36
+
37
+ def links
38
+ @links ||= json[:links]
39
+ end
40
+
41
+ def included
42
+ @included ||= json[:included]
43
+ end
44
+
45
+ def expect_status(status)
46
+ expect(response.status).to eq(status)
47
+ end
48
+
49
+ def expect_keys(hash, *keys)
50
+ keys.each { |k| expect(hash).to have_key(k) }
51
+ end
52
+
53
+ def a_head(path, context, params = nil)
54
+ authenticated_request(:head, path, context, params)
55
+ end
56
+
57
+ def a_get(path, context, params = nil)
58
+ authenticated_request(:get, path, context, params)
59
+ end
60
+
61
+ def a_post(path, context, params = nil)
62
+ authenticated_request(:post, path, context, params)
63
+ end
64
+
65
+ def a_patch(path, context, params = nil)
66
+ authenticated_request(:patch, path, context, params)
67
+ end
68
+
69
+ def a_put(path, context, params = nil)
70
+ authenticated_request(:put, path, context, params)
71
+ end
72
+
73
+ def a_delete(path, context, params = nil)
74
+ authenticated_request(:delete, path, context, params)
75
+ end
76
+
77
+ def authenticated_request(method, path, context, params)
78
+ json_request(method, path, params, auth_header_for(context))
79
+ end
80
+
81
+ def json_request(method, path, params = nil, headers = {})
82
+ send(method, path, params_for(method, params), headers.merge({
83
+ "CONTENT_TYPE" => "application/json"
84
+ }))
85
+ end
86
+
87
+ def params_for(method, params)
88
+ if [:post, :patch, :put].include?(method) && params
89
+ JSON.generate(params)
90
+ else
91
+ params
92
+ end
93
+ end
94
+
95
+ def auth_header_for(context)
96
+ { "HTTP_AUTHORIZATION" => ActionController::HttpAuthentication::Token.encode_credentials(context.id) }
97
+ end
98
+ end
99
+
100
+ def self.included(receiver)
101
+ receiver.extend ClassMethods
102
+ receiver.send :include, InstanceMethods
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,3 @@
1
+ module Pollett
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,12 @@
1
+ class ApplicationController < ActionController::Base
2
+ include Pollett::Controller
3
+
4
+ # Prevent CSRF attacks by raising an exception.
5
+ # For APIs, you may want to use :null_session instead.
6
+ protect_from_forgery with: :exception
7
+
8
+ private
9
+ def render_list(list)
10
+ render json: list, status: :ok
11
+ end
12
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,4 @@
1
+ class ApplicationMailer < ActionMailer::Base
2
+ default from: "from@example.com"
3
+ layout "mailer"
4
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ include Pollett::User
3
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,6 @@
1
+ Hi there,
2
+
3
+ <%= yield %>
4
+
5
+ Cheers!
6
+ - The Pollett Team
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pathname'
3
+
4
+ # path to your application root.
5
+ APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
6
+
7
+ Dir.chdir APP_ROOT do
8
+ # This script is a starting point to setup your application.
9
+ # Add necessary setup steps to this file:
10
+
11
+ puts "== Installing dependencies =="
12
+ system "gem install bundler --conservative"
13
+ system "bundle check || bundle install"
14
+
15
+ # puts "\n== Copying sample files =="
16
+ # unless File.exist?("config/database.yml")
17
+ # system "cp config/database.yml.sample config/database.yml"
18
+ # end
19
+
20
+ puts "\n== Preparing database =="
21
+ system "bin/rake db:setup"
22
+
23
+ puts "\n== Removing old logs and tempfiles =="
24
+ system "rm -f log/*"
25
+ system "rm -rf tmp/cache"
26
+
27
+ puts "\n== Restarting application server =="
28
+ system "touch tmp/restart.txt"
29
+ end
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails.application
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ # Pick the frameworks you want:
4
+ require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ require "action_mailer/railtie"
7
+ require "action_view/railtie"
8
+ require "sprockets/railtie"
9
+ # require "rails/test_unit/railtie"
10
+
11
+ Bundler.require(*Rails.groups)
12
+ require "pollett"
13
+
14
+ module Dummy
15
+ class Application < Rails::Application
16
+ # Settings in config/environments/* take precedence over those specified here.
17
+ # Application configuration should go into files in config/initializers
18
+ # -- all .rb files in that directory are automatically loaded.
19
+
20
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
21
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
22
+ # config.time_zone = 'Central Time (US & Canada)'
23
+
24
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
25
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
26
+ # config.i18n.default_locale = :de
27
+
28
+ # Do not swallow errors in after_commit/after_rollback callbacks.
29
+ config.active_record.raise_in_transactional_callbacks = true
30
+ end
31
+ end
32
+
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,85 @@
1
+ # PostgreSQL. Versions 8.2 and up are supported.
2
+ #
3
+ # Install the pg driver:
4
+ # gem install pg
5
+ # On OS X with Homebrew:
6
+ # gem install pg -- --with-pg-config=/usr/local/bin/pg_config
7
+ # On OS X with MacPorts:
8
+ # gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
9
+ # On Windows:
10
+ # gem install pg
11
+ # Choose the win32 build.
12
+ # Install PostgreSQL and put its /bin directory on your path.
13
+ #
14
+ # Configure Using Gemfile
15
+ # gem 'pg'
16
+ #
17
+ default: &default
18
+ adapter: postgresql
19
+ encoding: unicode
20
+ # For details on connection pooling, see rails configuration guide
21
+ # http://guides.rubyonrails.org/configuring.html#database-pooling
22
+ pool: 5
23
+
24
+ development:
25
+ <<: *default
26
+ database: dummy_development
27
+
28
+ # The specified database role being used to connect to postgres.
29
+ # To create additional roles in postgres see `$ createuser --help`.
30
+ # When left blank, postgres will use the default role. This is
31
+ # the same name as the operating system user that initialized the database.
32
+ #username: dummy
33
+
34
+ # The password associated with the postgres role (username).
35
+ #password:
36
+
37
+ # Connect on a TCP socket. Omitted by default since the client uses a
38
+ # domain socket that doesn't need configuration. Windows does not have
39
+ # domain sockets, so uncomment these lines.
40
+ #host: localhost
41
+
42
+ # The TCP port the server listens on. Defaults to 5432.
43
+ # If your server runs on a different port number, change accordingly.
44
+ #port: 5432
45
+
46
+ # Schema search path. The server defaults to $user,public
47
+ #schema_search_path: myapp,sharedapp,public
48
+
49
+ # Minimum log levels, in increasing order:
50
+ # debug5, debug4, debug3, debug2, debug1,
51
+ # log, notice, warning, error, fatal, and panic
52
+ # Defaults to warning.
53
+ #min_messages: notice
54
+
55
+ # Warning: The database defined as "test" will be erased and
56
+ # re-generated from your development database when you run "rake".
57
+ # Do not set this db to the same as development or production.
58
+ test:
59
+ <<: *default
60
+ database: dummy_test
61
+
62
+ # As with config/secrets.yml, you never want to store sensitive information,
63
+ # like your database password, in your source code. If your source code is
64
+ # ever seen by anyone, they now have access to your database.
65
+ #
66
+ # Instead, provide the password as a unix environment variable when you boot
67
+ # the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
68
+ # for a full rundown on how to provide these environment variables in a
69
+ # production deployment.
70
+ #
71
+ # On Heroku and other platform providers, you may have a full connection URL
72
+ # available as an environment variable. For example:
73
+ #
74
+ # DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
75
+ #
76
+ # You can use this database configuration with:
77
+ #
78
+ # production:
79
+ # url: <%= ENV['DATABASE_URL'] %>
80
+ #
81
+ production:
82
+ <<: *default
83
+ database: dummy_production
84
+ username: dummy
85
+ password: <%= ENV['DUMMY_DATABASE_PASSWORD'] %>