graphql_devise 0.11.4 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.travis.yml +7 -0
  4. data/Appraisals +14 -0
  5. data/CHANGELOG.md +56 -0
  6. data/Gemfile +2 -0
  7. data/README.md +219 -20
  8. data/Rakefile +2 -0
  9. data/app/controllers/graphql_devise/application_controller.rb +6 -5
  10. data/app/controllers/graphql_devise/concerns/set_user_by_token.rb +31 -0
  11. data/app/controllers/graphql_devise/graphql_controller.rb +4 -0
  12. data/app/helpers/graphql_devise/application_helper.rb +2 -0
  13. data/app/helpers/graphql_devise/mailer_helper.rb +4 -2
  14. data/app/models/graphql_devise/concerns/model.rb +10 -0
  15. data/app/views/graphql_devise/mailer/confirmation_instructions.html.erb +1 -1
  16. data/app/views/graphql_devise/mailer/reset_password_instructions.html.erb +1 -1
  17. data/config/locales/en.yml +1 -0
  18. data/config/routes.rb +4 -0
  19. data/graphql_devise.gemspec +6 -4
  20. data/lib/generators/graphql_devise/install_generator.rb +30 -5
  21. data/lib/graphql_devise.rb +26 -10
  22. data/lib/graphql_devise/concerns/controller_methods.rb +2 -0
  23. data/lib/graphql_devise/default_operations/mutations.rb +8 -6
  24. data/lib/graphql_devise/default_operations/resolvers.rb +4 -2
  25. data/lib/graphql_devise/engine.rb +2 -0
  26. data/lib/graphql_devise/errors/authentication_error.rb +9 -0
  27. data/lib/graphql_devise/{detailed_user_error.rb → errors/detailed_user_error.rb} +3 -1
  28. data/lib/graphql_devise/errors/error_codes.rb +8 -0
  29. data/lib/graphql_devise/errors/execution_error.rb +6 -0
  30. data/lib/graphql_devise/{user_error.rb → errors/user_error.rb} +3 -1
  31. data/lib/graphql_devise/model/with_email_updater.rb +74 -0
  32. data/lib/graphql_devise/mount_method/operation_preparer.rb +4 -2
  33. data/lib/graphql_devise/mount_method/operation_preparers/custom_operation_preparer.rb +2 -0
  34. data/lib/graphql_devise/mount_method/operation_preparers/default_operation_preparer.rb +8 -2
  35. data/lib/graphql_devise/mount_method/operation_preparers/gql_name_setter.rb +3 -1
  36. data/lib/graphql_devise/mount_method/operation_preparers/mutation_field_setter.rb +5 -2
  37. data/lib/graphql_devise/mount_method/operation_preparers/resolver_type_setter.rb +3 -1
  38. data/lib/graphql_devise/mount_method/operation_preparers/resource_name_setter.rb +4 -2
  39. data/lib/graphql_devise/mount_method/operation_sanitizer.rb +2 -0
  40. data/lib/graphql_devise/mount_method/option_sanitizer.rb +2 -0
  41. data/lib/graphql_devise/mount_method/option_sanitizers/array_checker.rb +2 -0
  42. data/lib/graphql_devise/mount_method/option_sanitizers/class_checker.rb +2 -0
  43. data/lib/graphql_devise/mount_method/option_sanitizers/hash_checker.rb +2 -0
  44. data/lib/graphql_devise/mount_method/option_sanitizers/string_checker.rb +2 -0
  45. data/lib/graphql_devise/mount_method/option_validators/provided_operations_validator.rb +2 -0
  46. data/lib/graphql_devise/mount_method/option_validators/skip_only_validator.rb +2 -0
  47. data/lib/graphql_devise/mount_method/option_validators/supported_operations_validator.rb +2 -0
  48. data/lib/graphql_devise/mount_method/options_validator.rb +2 -0
  49. data/lib/graphql_devise/mount_method/supported_options.rb +2 -0
  50. data/lib/graphql_devise/mutations/base.rb +2 -0
  51. data/lib/graphql_devise/mutations/login.rb +2 -0
  52. data/lib/graphql_devise/mutations/logout.rb +2 -0
  53. data/lib/graphql_devise/mutations/resend_confirmation.rb +5 -5
  54. data/lib/graphql_devise/mutations/send_password_reset.rb +7 -2
  55. data/lib/graphql_devise/mutations/sign_up.rb +5 -6
  56. data/lib/graphql_devise/mutations/update_password.rb +2 -0
  57. data/lib/graphql_devise/rails/routes.rb +7 -72
  58. data/lib/graphql_devise/resolvers/base.rb +2 -0
  59. data/lib/graphql_devise/resolvers/check_password_token.rb +2 -0
  60. data/lib/graphql_devise/resolvers/confirm_account.rb +4 -2
  61. data/lib/graphql_devise/resolvers/dummy.rb +2 -0
  62. data/lib/graphql_devise/resource_loader.rb +89 -0
  63. data/lib/graphql_devise/schema.rb +2 -0
  64. data/lib/graphql_devise/schema_plugin.rb +114 -0
  65. data/lib/graphql_devise/types/authenticatable_type.rb +2 -0
  66. data/lib/graphql_devise/types/credential_type.rb +2 -0
  67. data/lib/graphql_devise/types/mutation_type.rb +2 -0
  68. data/lib/graphql_devise/types/query_type.rb +2 -0
  69. data/lib/graphql_devise/version.rb +3 -1
  70. data/spec/dummy/Rakefile +2 -0
  71. data/spec/dummy/app/controllers/api/v1/graphql_controller.rb +38 -3
  72. data/spec/dummy/app/controllers/application_controller.rb +2 -0
  73. data/spec/dummy/app/graphql/dummy_schema.rb +21 -0
  74. data/spec/dummy/app/graphql/interpreter_schema.rb +11 -0
  75. data/spec/dummy/app/graphql/mutations/login.rb +2 -0
  76. data/spec/dummy/app/graphql/mutations/register_confirmed_user.rb +2 -0
  77. data/spec/dummy/app/graphql/mutations/sign_up.rb +2 -0
  78. data/spec/dummy/app/graphql/mutations/update_user.rb +22 -0
  79. data/spec/dummy/app/graphql/resolvers/public_user.rb +2 -0
  80. data/spec/dummy/app/graphql/resolvers/user_show.rb +2 -0
  81. data/spec/dummy/app/graphql/types/base_object.rb +2 -0
  82. data/spec/dummy/app/graphql/types/custom_admin_type.rb +2 -0
  83. data/spec/dummy/app/graphql/types/mutation_type.rb +4 -1
  84. data/spec/dummy/app/graphql/types/query_type.rb +12 -0
  85. data/spec/dummy/app/graphql/types/user_type.rb +2 -0
  86. data/spec/dummy/app/jobs/application_job.rb +2 -0
  87. data/spec/dummy/app/mailers/application_mailer.rb +2 -0
  88. data/spec/dummy/app/models/admin.rb +2 -0
  89. data/spec/dummy/app/models/application_record.rb +2 -0
  90. data/spec/dummy/app/models/guest.rb +2 -0
  91. data/spec/dummy/app/models/schema_user.rb +13 -0
  92. data/spec/dummy/app/models/user.rb +2 -0
  93. data/spec/dummy/app/models/users.rb +2 -0
  94. data/spec/dummy/app/models/users/customer.rb +2 -0
  95. data/spec/dummy/config.ru +2 -0
  96. data/spec/dummy/config/application.rb +2 -0
  97. data/spec/dummy/config/environment.rb +2 -0
  98. data/spec/dummy/config/environments/development.rb +2 -0
  99. data/spec/dummy/config/environments/production.rb +2 -0
  100. data/spec/dummy/config/environments/test.rb +2 -0
  101. data/spec/dummy/config/initializers/application_controller_renderer.rb +2 -0
  102. data/spec/dummy/config/initializers/backtrace_silencers.rb +2 -0
  103. data/spec/dummy/config/initializers/cors.rb +2 -0
  104. data/spec/dummy/config/initializers/devise_token_auth.rb +2 -0
  105. data/spec/dummy/config/initializers/filter_parameter_logging.rb +2 -0
  106. data/spec/dummy/config/initializers/i18n.rb +2 -0
  107. data/spec/dummy/config/initializers/inflections.rb +2 -0
  108. data/spec/dummy/config/initializers/mime_types.rb +2 -0
  109. data/spec/dummy/config/initializers/wrap_parameters.rb +2 -0
  110. data/spec/dummy/config/puma.rb +2 -0
  111. data/spec/dummy/config/routes.rb +5 -0
  112. data/spec/dummy/config/spring.rb +2 -0
  113. data/spec/dummy/db/migrate/20190815114303_create_users.rb +2 -0
  114. data/spec/dummy/db/migrate/20190824215150_add_auth_available_to_users.rb +2 -0
  115. data/spec/dummy/db/migrate/20190916012505_create_admins.rb +2 -0
  116. data/spec/dummy/db/migrate/20191013213045_create_guests.rb +2 -0
  117. data/spec/dummy/db/migrate/20200321121807_create_users_customers.rb +2 -0
  118. data/spec/dummy/db/migrate/20200621182414_remove_uncofirmed_email_from_admins.rb +7 -0
  119. data/spec/dummy/db/migrate/20200623003142_create_schema_users.rb +46 -0
  120. data/spec/dummy/db/schema.rb +30 -2
  121. data/spec/dummy/db/seeds.rb +2 -0
  122. data/spec/factories/admins.rb +2 -0
  123. data/spec/factories/guests.rb +2 -0
  124. data/spec/factories/schema_users.rb +13 -0
  125. data/spec/factories/users.rb +2 -0
  126. data/spec/factories/users_customers.rb +2 -0
  127. data/spec/generators/graphql_devise/install_generator_spec.rb +23 -0
  128. data/spec/graphql_devise/model/with_email_updater_spec.rb +131 -0
  129. data/spec/graphql_devise_spec.rb +2 -0
  130. data/spec/models/user_spec.rb +2 -0
  131. data/spec/rails_helper.rb +3 -1
  132. data/spec/requests/graphql_controller_spec.rb +82 -0
  133. data/spec/requests/mutations/additional_mutations_spec.rb +2 -0
  134. data/spec/requests/mutations/additional_queries_spec.rb +2 -0
  135. data/spec/requests/mutations/login_spec.rb +2 -0
  136. data/spec/requests/mutations/logout_spec.rb +2 -0
  137. data/spec/requests/mutations/resend_confirmation_spec.rb +46 -29
  138. data/spec/requests/mutations/send_password_reset_spec.rb +42 -12
  139. data/spec/requests/mutations/sign_up_spec.rb +2 -0
  140. data/spec/requests/mutations/update_password_spec.rb +2 -0
  141. data/spec/requests/queries/check_password_token_spec.rb +2 -0
  142. data/spec/requests/queries/confirm_account_spec.rb +9 -1
  143. data/spec/requests/user_controller_spec.rb +237 -23
  144. data/spec/services/mount_method/operation_preparer_spec.rb +10 -3
  145. data/spec/services/mount_method/operation_preparers/custom_operation_preparer_spec.rb +3 -1
  146. data/spec/services/mount_method/operation_preparers/default_operation_preparer_spec.rb +17 -8
  147. data/spec/services/mount_method/operation_preparers/gql_name_setter_spec.rb +2 -0
  148. data/spec/services/mount_method/operation_preparers/mutation_field_setter_spec.rb +20 -4
  149. data/spec/services/mount_method/operation_preparers/resolver_type_setter_spec.rb +2 -0
  150. data/spec/services/mount_method/operation_preparers/resource_name_setter_spec.rb +3 -1
  151. data/spec/services/mount_method/operation_sanitizer_spec.rb +2 -0
  152. data/spec/services/mount_method/option_sanitizer_spec.rb +2 -0
  153. data/spec/services/mount_method/option_sanitizers/array_checker_spec.rb +2 -0
  154. data/spec/services/mount_method/option_sanitizers/class_checker_spec.rb +2 -0
  155. data/spec/services/mount_method/option_sanitizers/hash_checker_spec.rb +2 -0
  156. data/spec/services/mount_method/option_sanitizers/string_checker_spec.rb +2 -0
  157. data/spec/services/mount_method/option_validators/provided_operations_validator_spec.rb +2 -0
  158. data/spec/services/mount_method/option_validators/skip_only_validator_spec.rb +2 -0
  159. data/spec/services/mount_method/option_validators/supported_operations_validator_spec.rb +2 -0
  160. data/spec/services/mount_method/options_validator_spec.rb +2 -0
  161. data/spec/services/resource_loader_spec.rb +84 -0
  162. data/spec/services/schema_plugin_spec.rb +28 -0
  163. data/spec/spec_helper.rb +2 -0
  164. data/spec/support/contexts/graphql_request.rb +2 -0
  165. data/spec/support/factory_bot.rb +2 -0
  166. data/spec/support/matchers/auth_headers_matcher.rb +2 -0
  167. data/spec/support/matchers/not_change_matcher.rb +2 -0
  168. data/spec/support/requests/auth_helpers.rb +2 -0
  169. data/spec/support/requests/json_helpers.rb +2 -0
  170. metadata +120 -87
  171. data/lib/graphql_devise/error_codes.rb +0 -5
  172. data/spec/support/generators/file_helpers.rb +0 -12
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails'
2
4
  require 'active_model/railtie'
3
5
  require 'active_job/railtie'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Load the Rails application.
2
4
  require_relative 'application'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.application.configure do
2
4
  # Settings specified here will take precedence over those in config/application.rb.
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.application.configure do
2
4
  # Settings specified here will take precedence over those in config/application.rb.
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.application.configure do
2
4
  # Settings specified here will take precedence over those in config/application.rb.
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  # ActiveSupport::Reloader.to_prepare do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  # Avoid CORS issues when API is called from the frontend app.
@@ -37,6 +37,8 @@ DeviseTokenAuth.setup do |config|
37
37
  # password is updated.
38
38
  config.check_current_password_before_update = :password
39
39
 
40
+ config.default_confirm_success_url = 'https://google.com'
41
+
40
42
  # By default we will use callbacks for single omniauth.
41
43
  # It depends on fields like email, provider and uid.
42
44
  # config.default_callbacks = true
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  # Configure sensitive parameters which will be filtered from the log file.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  I18n.exception_handler = lambda do |exception, locale, key, options|
2
4
  raise exception
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  # Add new inflection rules using the following format. Inflections
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  # Add new mime types for use in respond_to blocks:
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Be sure to restart your server when you modify this file.
2
4
 
3
5
  # This file contains settings for ActionController::ParamsWrapper which
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Puma can serve each request in a thread from an internal thread pool.
2
4
  # The `threads` method setting takes two numbers: a minimum and maximum.
3
5
  # Any libraries that use thread pools should be configured to match
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.application.routes.draw do
2
4
  mount_graphql_devise_for 'User', at: '/api/v1/graphql_auth', operations: {
3
5
  login: Mutations::Login,
@@ -27,5 +29,8 @@ Rails.application.routes.draw do
27
29
  at: '/api/v1/user_customer/graphql_auth'
28
30
  )
29
31
 
32
+ get '/api/v1/graphql', to: 'api/v1/graphql#graphql'
30
33
  post '/api/v1/graphql', to: 'api/v1/graphql#graphql'
34
+ post '/api/v1/interpreter', to: 'api/v1/graphql#interpreter'
35
+ post '/api/v1/failing', to: 'api/v1/graphql#failing_resource_name'
31
36
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  %w[
2
4
  .ruby-version
3
5
  .rbenv-vars
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CreateUsers < ActiveRecord::Migration[5.2]
2
4
  def change
3
5
  create_table :users do |t|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddAuthAvailableToUsers < ActiveRecord::Migration[5.2]
2
4
  def change
3
5
  add_column :users, :auth_available, :boolean, null: false, default: true
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CreateAdmins < ActiveRecord::Migration[5.2]
2
4
  def change
3
5
  create_table :admins do |t|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CreateGuests < ActiveRecord::Migration[6.0]
2
4
  def change
3
5
  create_table :guests do |t|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CreateUsersCustomers < ActiveRecord::Migration[6.0]
2
4
  def change
3
5
  create_table :users_customers do |t|
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class RemoveUncofirmedEmailFromAdmins < ActiveRecord::Migration[6.0]
4
+ def change
5
+ remove_column :admins, :unconfirmed_email, :string
6
+ end
7
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateSchemaUsers < ActiveRecord::Migration[6.0]
4
+ def change
5
+ create_table :schema_users do |t|
6
+ ## Required
7
+ t.string :provider, null: false, default: 'email'
8
+ t.string :uid, null: false, default: ''
9
+
10
+ ## Database authenticatable
11
+ t.string :encrypted_password, null: false, default: ''
12
+
13
+ ## Recoverable
14
+ t.string :reset_password_token
15
+ t.datetime :reset_password_sent_at
16
+ t.boolean :allow_password_change, default: false
17
+
18
+ ## Confirmable
19
+ t.string :confirmation_token
20
+ t.datetime :confirmed_at
21
+ t.datetime :confirmation_sent_at
22
+
23
+ # Trackable
24
+ t.datetime :current_sign_in_at
25
+ t.datetime :last_sign_in_at
26
+ t.string :last_sign_in_ip
27
+ t.string :current_sign_in_ip
28
+ t.integer :sign_in_count
29
+
30
+ ## User Info
31
+ t.string :name
32
+ t.string :email
33
+
34
+ ## Tokens
35
+ t.text :tokens
36
+
37
+ t.timestamps
38
+ end
39
+
40
+ add_index :schema_users, :email, unique: true
41
+ add_index :schema_users, [:uid, :provider], unique: true
42
+ add_index :schema_users, :reset_password_token, unique: true
43
+ add_index :schema_users, :confirmation_token, unique: true
44
+ add_index :schema_users, :unlock_token, unique: true
45
+ end
46
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file is auto-generated from the current state of the database. Instead
2
4
  # of editing this file, please use the migrations feature of Active Record to
3
5
  # incrementally modify your database, and then regenerate this schema definition.
@@ -10,7 +12,7 @@
10
12
  #
11
13
  # It's strongly recommended that you check this file into your version control system.
12
14
 
13
- ActiveRecord::Schema.define(version: 2020_03_21_121807) do
15
+ ActiveRecord::Schema.define(version: 2020_06_23_003142) do
14
16
 
15
17
  create_table "admins", force: :cascade do |t|
16
18
  t.string "provider", default: "email", null: false
@@ -22,7 +24,6 @@ ActiveRecord::Schema.define(version: 2020_03_21_121807) do
22
24
  t.string "confirmation_token"
23
25
  t.datetime "confirmed_at"
24
26
  t.datetime "confirmation_sent_at"
25
- t.string "unconfirmed_email"
26
27
  t.string "email"
27
28
  t.text "tokens"
28
29
  t.datetime "created_at", null: false
@@ -54,6 +55,33 @@ ActiveRecord::Schema.define(version: 2020_03_21_121807) do
54
55
  t.index ["uid", "provider"], name: "index_guests_on_uid_and_provider", unique: true
55
56
  end
56
57
 
58
+ create_table "schema_users", force: :cascade do |t|
59
+ t.string "provider", default: "email", null: false
60
+ t.string "uid", default: "", null: false
61
+ t.string "encrypted_password", default: "", null: false
62
+ t.string "reset_password_token"
63
+ t.datetime "reset_password_sent_at"
64
+ t.boolean "allow_password_change", default: false
65
+ t.string "confirmation_token"
66
+ t.datetime "confirmed_at"
67
+ t.datetime "confirmation_sent_at"
68
+ t.datetime "current_sign_in_at"
69
+ t.datetime "last_sign_in_at"
70
+ t.string "last_sign_in_ip"
71
+ t.string "current_sign_in_ip"
72
+ t.integer "sign_in_count"
73
+ t.string "name"
74
+ t.string "email"
75
+ t.text "tokens"
76
+ t.datetime "created_at", precision: 6, null: false
77
+ t.datetime "updated_at", precision: 6, null: false
78
+ t.index "\"unlock_token\"", name: "index_schema_users_on_unlock_token", unique: true
79
+ t.index ["confirmation_token"], name: "index_schema_users_on_confirmation_token", unique: true
80
+ t.index ["email"], name: "index_schema_users_on_email", unique: true
81
+ t.index ["reset_password_token"], name: "index_schema_users_on_reset_password_token", unique: true
82
+ t.index ["uid", "provider"], name: "index_schema_users_on_uid_and_provider", unique: true
83
+ end
84
+
57
85
  create_table "users", force: :cascade do |t|
58
86
  t.string "provider", default: "email", null: false
59
87
  t.string "uid", default: "", null: false
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file should contain all the record creation needed to seed the database with its default values.
2
4
  # The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  FactoryBot.define do
2
4
  factory :admin do
3
5
  email { Faker::Internet.unique.email }
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  FactoryBot.define do
2
4
  factory :guest do
3
5
  email { Faker::Internet.unique.email }
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ FactoryBot.define do
4
+ factory :schema_user do
5
+ name { Faker::FunnyName.two_word_name }
6
+ email { Faker::Internet.unique.email }
7
+ password { Faker::Internet.password }
8
+
9
+ trait :confirmed do
10
+ confirmed_at { Time.zone.now }
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  FactoryBot.define do
2
4
  factory :user do
3
5
  name { Faker::FunnyName.two_word_name }
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  FactoryBot.define do
2
4
  factory :users_customer, class: 'Users::Customer' do
3
5
  name { Faker::FunnyName.two_word_name }
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Generators are not automatically loaded by Rails
2
4
  require 'rails_helper'
3
5
  require 'generators/graphql_devise/install_generator'
@@ -17,6 +19,24 @@ RSpec.describe GraphqlDevise::InstallGenerator, type: :generator do
17
19
  run_generator(args)
18
20
  end
19
21
 
22
+ context 'when mount option is schema' do
23
+ let(:args) { ['Admin', '--mount', 'GqldDummySchema'] }
24
+
25
+ it 'mounts the SchemaPlugin' do
26
+ assert_file 'config/initializers/devise.rb'
27
+ assert_file 'config/initializers/devise_token_auth.rb', /^\s{2}#{Regexp.escape('config.change_headers_on_each_request = false')}/
28
+ assert_file 'config/locales/devise.en.yml'
29
+
30
+ assert_migration 'db/migrate/devise_token_auth_create_admins.rb'
31
+
32
+ assert_file 'app/models/admin.rb', /^\s{2}devise :.+include GraphqlDevise::Concerns::Model/m
33
+
34
+ assert_file 'app/controllers/application_controller.rb', /^\s{2}include GraphqlDevise::Concerns::SetUserByToken/
35
+
36
+ assert_file 'app/graphql/gqld_dummy_schema.rb', /\s+#{Regexp.escape("GraphqlDevise::ResourceLoader.new('Admin')")}/
37
+ end
38
+ end
39
+
20
40
  context 'when passing no params to the generator' do
21
41
  let(:args) { [] }
22
42
 
@@ -59,5 +79,8 @@ RSpec.describe GraphqlDevise::InstallGenerator, type: :generator do
59
79
  FileUtils.cd(File.join(destination_root, '..')) do
60
80
  `rails new gqld_dummy -S -C --skip-action-mailbox --skip-action-text -T --skip-spring --skip-bundle --skip-keeps -G --skip-active-storage -J --skip-listen --skip-bootsnap`
61
81
  end
82
+ FileUtils.cd(File.join(destination_root, '../gqld_dummy')) do
83
+ `rails generate graphql:install`
84
+ end
62
85
  end
63
86
  end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe GraphqlDevise::Model::WithEmailUpdater do
6
+ describe '#call' do
7
+ subject(:updater) { described_class.new(resource, attributes).call }
8
+
9
+ context 'when the model does not have an unconfirmed_email column' do
10
+ let(:resource) { create(:admin, :confirmed) }
11
+
12
+ context 'when attributes contain email' do
13
+ let(:attributes) { { email: 'new@gmail.com', schema_url: 'http://localhost/test', confirmation_success_url: 'https://google.com' } }
14
+
15
+ it 'does not postpone email update' do
16
+ expect do
17
+ updater
18
+ resource.reload
19
+ end.to change(resource, :email).from(resource.email).to('new@gmail.com').and(
20
+ change(resource, :uid).from(resource.uid).to('new@gmail.com')
21
+ )
22
+ end
23
+ end
24
+ end
25
+
26
+ context 'when the model has an unconfirmed_email column' do
27
+ let(:resource) { create(:user, :confirmed) }
28
+
29
+ context 'when attributes do not contain email' do
30
+ let(:attributes) { { name: 'Updated Name', schema_url: 'http://localhost/test', confirmation_success_url: 'https://google.com' } }
31
+
32
+ it 'updates resource, ignores url params' do
33
+ expect do
34
+ updater
35
+ resource.reload
36
+ end.to change(resource, :name).from(resource.name).to('Updated Name')
37
+ end
38
+ end
39
+
40
+ context 'when attributes contain email' do
41
+ context 'when schema_url is missing' do
42
+ let(:attributes) { { email: 'new@gmail.com', name: 'Updated Name' } }
43
+
44
+ it 'raises an error' do
45
+ expect { updater }.to raise_error(
46
+ GraphqlDevise::Error,
47
+ 'Method `update_with_email` requires attributes `confirmation_success_url` and `schema_url` for email reconfirmation to work'
48
+ )
49
+ end
50
+
51
+ context 'when email will not change' do
52
+ let(:attributes) { { email: resource.email, name: 'changed' } }
53
+
54
+ it 'updates name and does not raise an error' do
55
+ expect do
56
+ updater
57
+ resource.reload
58
+ end.to change(resource, :name).from(resource.name).to('changed').and(
59
+ not_change(resource, :email).from(resource.email)
60
+ ).and(
61
+ not_change(ActionMailer::Base.deliveries, :count).from(0)
62
+ )
63
+ end
64
+ end
65
+ end
66
+
67
+ context 'when only confirmation_success_url is missing' do
68
+ let(:attributes) { { email: 'new@gmail.com', name: 'Updated Name', schema_url: 'http://localhost/test' } }
69
+
70
+ it 'uses DTA default_confirm_success_url on the email' do
71
+ expect { updater }.to change(ActionMailer::Base.deliveries, :count).by(1)
72
+
73
+ email = ActionMailer::Base.deliveries.first
74
+ expect(email.body.decoded).to include(CGI.escape('https://google.com'))
75
+ end
76
+ end
77
+
78
+ context 'when both required urls are provided' do
79
+ let(:attributes) { { email: 'new@gmail.com', name: 'Updated Name', schema_url: 'http://localhost/test', confirmation_success_url: 'https://google.com' } }
80
+
81
+ it 'postpones email update' do
82
+ expect do
83
+ updater
84
+ resource.reload
85
+ end.to not_change(resource, :email).from(resource.email).and(
86
+ not_change(resource, :uid).from(resource.uid)
87
+ ).and(
88
+ change(resource, :unconfirmed_email).from(nil).to('new@gmail.com')
89
+ ).and(
90
+ change(resource, :name).from(resource.name).to('Updated Name')
91
+ )
92
+ end
93
+
94
+ it 'sends out a confirmation email to the unconfirmed_email' do
95
+ expect { updater }.to change(ActionMailer::Base.deliveries, :count).by(1)
96
+
97
+ email = ActionMailer::Base.deliveries.first
98
+ expect(email.to).to contain_exactly('new@gmail.com')
99
+ end
100
+
101
+ context 'when email value is the same on the DB' do
102
+ let(:attributes) { { email: resource.email, name: 'changed', schema_url: 'http://localhost/test', confirmation_success_url: 'https://google.com' } }
103
+
104
+ it 'updates attributes and does not send confirmation email' do
105
+ expect do
106
+ updater
107
+ resource.reload
108
+ end.to change(resource, :name).from(resource.name).to('changed').and(
109
+ not_change(resource, :email).from(resource.email)
110
+ ).and(
111
+ not_change(ActionMailer::Base.deliveries, :count).from(0)
112
+ )
113
+ end
114
+ end
115
+
116
+ context 'when provided params are invalid' do
117
+ let(:attributes) { { email: 'newgmail.com', name: '', schema_url: 'http://localhost/test', confirmation_success_url: 'https://google.com' } }
118
+
119
+ it 'returns false and adds errors to the model' do
120
+ expect(updater).to be_falsey
121
+ expect(resource.errors.full_messages).to contain_exactly(
122
+ 'Email is not an email',
123
+ "Name can't be blank"
124
+ )
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end