cadenero 0.0.2.b4 → 0.0.2.b5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +8 -8
  2. data/README.md +19 -4
  3. data/app/controllers/cadenero/v1/account/users_controller.rb +4 -3
  4. data/app/controllers/cadenero/v1/accounts_controller.rb +3 -5
  5. data/app/extenders/controllers/application_controller_decorator.rb +4 -5
  6. data/app/models/cadenero/member.rb +43 -1
  7. data/app/models/cadenero/user.rb +5 -5
  8. data/app/models/cadenero/v1/account.rb +17 -10
  9. data/config/initializers/apartment.rb +0 -1
  10. data/config/initializers/warden/strategies/password.rb +1 -1
  11. data/db/migrate/20130612061604_create_cadenero_v1_accounts.rb +1 -1
  12. data/db/migrate/20130715174857_add_auth_token_to_cadenero_members.rb +6 -0
  13. data/db/seeds.rb +3 -3
  14. data/lib/cadenero.rb +6 -6
  15. data/lib/cadenero/engine.rb +1 -1
  16. data/lib/cadenero/testing_support/authentication_helpers.rb +55 -18
  17. data/lib/cadenero/testing_support/subdomain_helpers.rb +1 -1
  18. data/lib/cadenero/version.rb +1 -1
  19. data/lib/generators/cadenero/install_generator.rb +4 -9
  20. data/spec/controllers/cadenero/v1/accounts_controller_spec.rb +3 -5
  21. data/spec/controllers/cadenero/v1/users_controller_spec.rb +53 -0
  22. data/spec/dummy/config/initializers/cadenero.rb +13 -0
  23. data/spec/dummy/db/schema.rb +3 -1
  24. data/spec/dummy/log/development.log +8 -0
  25. data/spec/dummy/log/test.log +39150 -0
  26. data/spec/features/accounts/sign_up_spec.rb +1 -1
  27. data/spec/features/users/sign_in_spec.rb +49 -24
  28. data/spec/features/users/sign_up_spec.rb +8 -15
  29. data/spec/generators/install_generator_spec.rb +41 -6
  30. data/spec/models/cadenero/member_spec.rb +11 -1
  31. metadata +5 -2
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YWFkM2QwNGFkNGJjNGVlOGM0ZmVkNDIzZDI2NTdlM2IwYTgxZjMxMQ==
4
+ Y2FmOTI0YTdlNWExZjkzM2M0NTliNjk4M2VhNWZkYTI2YTE2ZTc2Mw==
5
5
  data.tar.gz: !binary |-
6
- NzIyNDk3ZDdiMjE3ZWVmOTBmOWY0MTUxYTRhMDU1ZjEyYzE2MzViNg==
6
+ NzU4M2I5ZDgxZjI0NjFkMzk1MDQwYmY5ZTY5Mjk0ZTAyNGRjZjViMw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ODUwOGQ0OGFlNmYwZjc1MzE4YjAxZTMwZGZhZDJlZTVkZmY3NmIxZGYxMWFm
10
- OGFjYzkwYzI2ZTYyMzlkZjdkM2JjOGI4Y2I1Njg2ZDVkY2NkMDgyNTVhNTY1
11
- ZmZiYTcyMWU1MTE4NGQ0MWUzMWVhYTg5YTc0MzdiODVmYjlkODI=
9
+ MmZjMTBhYmVkY2E1ODgwM2EwNTQyMTFhY2ZhMzA5ZjlmZTJkM2E0ODFiYzgz
10
+ MDQ0ODg1MGViY2RhYjcwMDIyZTU0Mzg4M2Q1MGUyYWM0NmQ4MzJmNTcyYTZm
11
+ YmZiOGM5N2Y4MDVjN2FhYWRjMDk0NDdjYjk2OTAwZWYyZTEyYTQ=
12
12
  data.tar.gz: !binary |-
13
- MjU0OWZjYTBhMWIxZjc5NmRjNDgwOTQ4MzU2M2ZjNWY3ZTM4NDk0Zjc4Yzlk
14
- MWQ4MjllZWM0OTA3M2U3MmRlZDYwNDlkNTA2ZWY5NGJkNmQ3YmVjMzJkMjNl
15
- YTRmNjI1MWRiNGYwZTM1NDRhMjdiNDBiYzA2NTI1NjhmZWUwMzg=
13
+ NmJiMTY3YmM3NmZhNzIwMDhmODFkMjc3MzlkMzFjN2RmOTYwNjg5N2NkNGNm
14
+ OTRmNzcyZTIxMjU4ZjgxMDdjNzFlNDYxNGNhOGE4ZDQ2MjVjYTM5NDZkMmQz
15
+ N2E3MGY0MWUwNDk2ZWM5MThjYjQ1YzUzNGMwZDNmMWUxMmRlMTM=
data/README.md CHANGED
@@ -16,7 +16,8 @@ Authentication Engine for Rails.API multitenant RESTful APIs based on Warden. It
16
16
  ## Information
17
17
 
18
18
  ### Why Cadenero?
19
- **"Cadenero"** is the spanish word for ["Bouncer (doorman)"](http://en.wikipedia.org/wiki/Bouncer_(doorman\)). The main function of **Cadenero** is to be a resource for authenticating consumers of the services that the API provides. As the real bouncers, **Cadenero** aims to provide security, check authorized access, to refuse entry for intoxication, aggressive behavior or non-compliance with statutory or establishment rules.
19
+ **"Cadenero"** is the spanish word for ["Bouncer (doorman)"](http://en.wikipedia.org/wiki/Bouncer_(doorman\)). The main function of **Cadenero** is to be a resource for authenticating consumers of the services that the API provides. As the real bouncers, **Cadenero** aims to provide security, check authorized access, to refuse entry for intoxication, aggressive behavior or non-compliance with statutory or establishment rules.
20
+ You can use [Warden](https://github.com/hassox/warden) or [Devise](https://github.com/plataformatec/devise) directly but for API apps the rewritting and monkey patching can be messy.
20
21
 
21
22
  ### Installing **Cadenero**
22
23
 
@@ -30,7 +31,7 @@ Generate first your Rails app as usual using:
30
31
 
31
32
  In the `Gemfile` add the following lines:
32
33
  ```ruby
33
- gem 'cadenero', '~> 0.0.2.b4'
34
+ gem 'cadenero', '~> 0.0.2.b5'
34
35
  gem 'pg'
35
36
  ```
36
37
 
@@ -111,7 +112,7 @@ Have fun!
111
112
  You can check them running:
112
113
 
113
114
  ```
114
- rake routes
115
+ $ rake routes
115
116
  ```
116
117
  ### Documentation
117
118
  You can review the YARD docs in: http://rubydoc.info/github/AgilTec/cadenero/frames
@@ -122,7 +123,7 @@ You can review the YARD docs in: http://rubydoc.info/github/AgilTec/cadenero/fra
122
123
  - [ ] Examples of use and demo
123
124
 
124
125
  ### Versions
125
- **Cadenero** use [Semantic Versioning 2.0.0](http://semver.org/) the current version is: 0.0.2-alpha meaning MAJOR.MINOR.PATCH format
126
+ **Cadenero** use [Semantic Versioning 2.0.0](http://semver.org/) the current version is: 0.0.2-b5 meaning MAJOR.MINOR.PATCH format
126
127
 
127
128
  ### Bug reports
128
129
 
@@ -140,6 +141,20 @@ https://github.com/AgilTec/cadenero/Contributing
140
141
 
141
142
  You will usually want to write tests for your changes using BDD tools as RSpec, Rack::Test and Capybara. To run the test suite, go into **Cadenero**'s top-level directory and run "bundle install" and "rspec". For the tests to pass, you will need to have a Postgresql server running on your system.
142
143
 
144
+ #### Running the Specs
145
+ **Cadenero** use [RSpec](https://github.com/rspec/rspec) and [Capybara](https://github.com/jnicklas/capybara). If you want to extend **Cadenero** please fork and clone the repo. To run the specs you only need to do:
146
+ ```
147
+ $ RAILS_ENV=test bundle exec rake db:create
148
+ $ RAILS_ENV=test bundle exec rake db:migrate
149
+ $ bundle exec rspec spec
150
+ ```
151
+
152
+ You can `binstub` the command bins to avoid writing `bundle exec`. You only need to write:
153
+ ```
154
+ $ bundle binstubs rspec-core
155
+ $ bundle binstubs rake
156
+ ```
157
+
143
158
  ### Warden
144
159
 
145
160
  **Cadenero** is based on Warden, which is a general Rack authentication framework created by Daniel Neighman. We encourage you to read more about Warden here: https://github.com/hassox/warden
@@ -9,13 +9,14 @@ module Cadenero
9
9
  module V1
10
10
  # Controller for managing users for specific accounts
11
11
  class Account::UsersController < Cadenero::ApplicationController
12
+ before_filter :authenticate_user!, except: :create
12
13
  # Create a [Cadenero::User] based on the params sended by the client as a JSON with the user inrormation
13
14
  #
14
15
  # @example Posting the user data to be created in an account via the subdomain
15
- # post "http://#{account.subdomain}.example.com/v1/users",
16
+ # post "http://#{account.subdomain}.example.com/v1/users",
16
17
  # user: { email: "user@example.com", password: "password", password_confirmation: "password" }
17
18
  #
18
- # @return render JSON of [Cadenero::User] created and the status 201 Created: The request has been
19
+ # @return render JSON of [Cadenero::User] created and the status 201 Created: The request has been
19
20
  # fulfilled and resulted in a new resource being created.
20
21
  def create
21
22
  account = Cadenero::V1::Account.where(subdomain: request.subdomain).first
@@ -23,7 +24,7 @@ module Cadenero
23
24
  force_authentication!(@user)
24
25
  render json: @user, status: :created
25
26
  end
26
-
27
+
27
28
  # Send as JSON the user that match the params[:user]
28
29
  def show
29
30
  @user = current_account.users.where(id: params[:id]).first
@@ -13,17 +13,15 @@ module Cadenero
13
13
  # Create a [Cadenero::V1::Account] based on the params sended by the client as a JSON with the account inrormation
14
14
  #
15
15
  # @example Posting the account data to be created in a subdomain
16
- # post "http://www.example.com/v1/accounts",
17
- # account: { name: "Testy", subdomain: "test",
16
+ # post "http://www.example.com/v1/accounts",
17
+ # account: { name: "Testy", subdomain: "test",
18
18
  # owner_attributes: {email: "testy@example.com", password: "changeme", password_confirmation: "changeme"} }
19
19
  #
20
- # @return render JSON of [Cadenero::V1::Account] created and the status 201 Created: The request has been
20
+ # @return render JSON of [Cadenero::V1::Account] created and the status 201 Created: The request has been
21
21
  # fulfilled and resulted in a new resource being created.
22
22
  def create
23
23
  @account = Cadenero::V1::Account.create_with_owner(params[:account])
24
24
  if @account.valid?
25
- @account.create_schema
26
- @account.ensure_authentication_token!
27
25
  force_authentication!(@account.owner)
28
26
  render json: @account, status: :created
29
27
  else
@@ -28,17 +28,16 @@
28
28
  end
29
29
  end
30
30
 
31
- # Check to see if there is an authenticated user
31
+ # Check to see if there is an authenticated user
32
32
  def user_signed_in?
33
- env['warden'].authenticated?(:user)
33
+ env['warden'].authenticated?(:user) unless env['warden'].nil?
34
34
  end
35
35
 
36
36
  # it the user is not authenticated returns a 422 and an informative error with the link for sign
37
37
  def authenticate_user!
38
- Rails.logger.info "env['warden'].authenticated?(:user): #{env['warden'].authenticated?(:user)}"
39
38
  unless user_signed_in?
40
- errors = %Q{Please sign in. posting the user json credentials as: {"user": {"email": "testy2@example.com", "password": "changeme"}} to /v1/sessions}
41
- render json: {errors: errors, links: "/v1/sessions"}, status: 422
39
+ @errors = %Q{Please sign in. posting the user json credentials as: {"user": {"email": "testy2@example.com", "password": "changeme"}} to /v1/sessions}
40
+ render json: {errors: @errors, links: "/v1/sessions"}, status: 422
42
41
  end
43
42
  end
44
43
 
@@ -1,8 +1,50 @@
1
1
  module Cadenero
2
2
  # Defines that a Cadenero::User is member of an Cadenero::V1::Account
3
3
  class Member < ActiveRecord::Base
4
+ attr_accessible :account_id, :user_id
4
5
  belongs_to :account, :class_name => "Cadenero::V1::Account"
5
6
  belongs_to :user, :class_name => "Cadenero::User"
6
- # attr_accessible :title, :body
7
+ after_create :ensure_auth_token!
8
+
9
+ # Generate authentication token unless already exists.
10
+ def ensure_auth_token
11
+ reset_auth_token if auth_token.blank?
12
+ end
13
+
14
+ # Generate authentication token unless already exists and save the record.
15
+ def ensure_auth_token!
16
+ reset_auth_token! if auth_token.blank?
17
+ end
18
+
19
+ # Generate new authentication token (a.k.a. "single access token").
20
+ def reset_auth_token
21
+ self.auth_token = self.class.auth_token
22
+ end
23
+
24
+ # Generate new authentication token and save the record.
25
+ def reset_auth_token!
26
+ reset_auth_token
27
+ save(:validate => false)
28
+ end
29
+
30
+ class << self
31
+ # Generate a token checking if one does not already exist in the database.
32
+ def auth_token
33
+ generate_token(:auth_token)
34
+ end
35
+
36
+ protected
37
+ # Generate a token by looping and ensuring does not already exist.
38
+ # @param [String] column is the name of the column that has the authentication token
39
+ # @return {String]} a unique generated auth_token
40
+ def generate_token(column)
41
+ loop do
42
+ token = SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz')
43
+ break token unless Member.where({ column => token }).first
44
+ end
45
+ end
46
+ end
47
+
7
48
  end
49
+
8
50
  end
@@ -1,5 +1,5 @@
1
1
  module Cadenero
2
- # Defines a user of one or more accounts for the multitenant Rails App
2
+ # Defines a user of one or more accounts for the multitenant Rails App
3
3
  class User < ActiveRecord::Base
4
4
  attr_accessible :email, :password, :password_confirmation
5
5
  has_secure_password
@@ -7,10 +7,10 @@ module Cadenero
7
7
  has_many :members, class_name: "Cadenero::Member"
8
8
  has_many :memberships, through: :members, source: :account
9
9
 
10
- # Obtain the authentication_token from the account to be use for the User
11
- def auth_token
12
- accounts.map{|acc| acc.authentication_token}
10
+ # Obtain the authentication_token from the members to be use for the User
11
+ def auth_token
12
+ members.map{|member| member.auth_token}
13
13
  end
14
-
14
+
15
15
  end
16
16
  end
@@ -4,7 +4,7 @@ module Cadenero::V1
4
4
  belongs_to :owner, :class_name => "Cadenero::User"
5
5
  has_many :members, :class_name => "Cadenero::Member"
6
6
  has_many :users, :through => :members, :class_name => "Cadenero::User"
7
-
7
+
8
8
  accepts_nested_attributes_for :owner
9
9
  attr_accessible :name, :subdomain, :owner_attributes, :owner
10
10
  validates :subdomain, :presence => true, :uniqueness => true
@@ -12,26 +12,28 @@ module Cadenero::V1
12
12
  after_create :ensure_authentication_token!
13
13
 
14
14
  # Creates an account and assign the provided [Cadenero::User] as owner to the account
15
- # @param [Hash] params list
15
+ # @param [Hash] params list
16
16
  # @example
17
- # Example for the params JSON: {name: "Testy", subdomain: "test",
18
- # owner_attributes: {email: "testy@example.com", password: "changeme",
17
+ # Example for the params JSON: {name: "Testy", subdomain: "test",
18
+ # owner_attributes: {email: "testy@example.com", password: "changeme",
19
19
  # password_confirmation: "changeme"} }
20
- # @return the [Cadenero::V1::Account] created
20
+ # @return [Cadenero::V1::Account] created
21
21
  # @note because this model uses accepts_nested_attributes_for :owner the JSOB should have owner_attributes
22
22
  def self.create_with_owner(params={})
23
23
  account = new(params)
24
24
  if account.save
25
25
  account.users << account.owner
26
+ account.create_schema
27
+ account.ensure_authentication_token!
26
28
  end
27
29
  account
28
30
  end
29
31
 
30
- # Gets the account for the specified subdomain and guards errors
31
- # @param [String] params subdomain
32
+ # Gets the account for the specified subdomain and guards errors
33
+ # @param [String] subdomain
32
34
  # @example
33
35
  # get_by_subdomain("www")
34
- # @return the [Cadenero::V1::Account] for that subdomain
36
+ # @return [Cadenero::V1::Account] for that subdomain
35
37
  def self.get_by_subdomain(subdomain)
36
38
  account = find_by_subdomain(subdomain)
37
39
  if account
@@ -46,6 +48,11 @@ module Cadenero::V1
46
48
  Apartment::Database.create(subdomain)
47
49
  end
48
50
 
51
+ # Obtain the auth_token from the members to be use for the Account
52
+ def auth_token
53
+ members.map{|member| member.auth_token}
54
+ end
55
+
49
56
  # Generate authentication token unless already exists.
50
57
  def ensure_authentication_token
51
58
  reset_authentication_token if authentication_token.blank?
@@ -75,8 +82,8 @@ module Cadenero::V1
75
82
 
76
83
  protected
77
84
  # Generate a token by looping and ensuring does not already exist.
78
- # @params [String] column is the name of the column that has the authentication token
79
- # @return a unique generated authentication_token
85
+ # @param [String] column is the name of the column that has the authentication token
86
+ # @return {String]} a unique generated authentication_token
80
87
  def generate_token(column)
81
88
  loop do
82
89
  token = SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz')
@@ -3,7 +3,6 @@ require File.expand_path('../../../app/extenders/middleware/robustness', __FILE_
3
3
  Rails.application.config.middleware.use(Robustness)
4
4
  Rails.application.config.middleware.use(Apartment::Elevators::Subdomain)
5
5
 
6
-
7
6
  Apartment.configure do |config|
8
7
  config.excluded_models = ["Cadenero::V1::Account",
9
8
  "Cadenero::Member",
@@ -15,7 +15,7 @@ Warden::Strategies.add(:password) do
15
15
  def valid?
16
16
  subdomain.present? && json_params["user"]
17
17
  end
18
-
18
+
19
19
  def authenticate!
20
20
  account = Cadenero::V1::Account.get_by_subdomain(subdomain)
21
21
  if account
@@ -3,7 +3,7 @@ class CreateCadeneroV1Accounts < ActiveRecord::Migration
3
3
  create_table :cadenero_accounts do |t|
4
4
  t.string :name
5
5
  t.string :subdomain
6
- t.string :authentication_token
6
+ t.string :authentication_token
7
7
  t.references :owner
8
8
 
9
9
  t.timestamps
@@ -0,0 +1,6 @@
1
+ class AddAuthTokenToCadeneroMembers < ActiveRecord::Migration
2
+ def change
3
+ add_column :cadenero_members, :auth_token, :string
4
+ add_index :cadenero_members, :auth_token
5
+ end
6
+ end
@@ -1,7 +1,7 @@
1
- @account = Cadenero::V1::Account.create!(name: Cadenero.default_account_name,
2
- subdomain: Cadenero.default_account_subdomain,
1
+ @account = Cadenero::V1::Account.create!(name: Cadenero.default_account_name,
2
+ subdomain: Cadenero.default_account_subdomain,
3
3
  owner: Cadenero::User.create!(email: Cadenero.default_user_email,
4
- password: Cadenero.default_user_password,
4
+ password: Cadenero.default_user_password,
5
5
  password_confirmation: Cadenero.default_user_password))
6
6
 
7
7
  @account.create_schema
@@ -26,12 +26,12 @@ require 'warden'
26
26
  require 'apartment'
27
27
 
28
28
  module Cadenero
29
- mattr_accessor :base_path,
30
- :user_class,
31
- :default_account_name,
32
- :default_account_subdomain,
33
- :default_user_email,
34
- :default_user_password
29
+ mattr_accessor :base_path,
30
+ :user_class,
31
+ :default_account_name,
32
+ :default_account_subdomain,
33
+ :default_user_email,
34
+ :default_user_password
35
35
 
36
36
  class << self
37
37
  # @return the base path for the Cadenero named routes
@@ -7,7 +7,7 @@ module Cadenero
7
7
  manager.default_strategies :password
8
8
  end
9
9
 
10
- config.generators do |g|
10
+ config.generators do |g|
11
11
  g.test_framework :rspec
12
12
  g.integration_tool :rspec
13
13
  end
@@ -5,21 +5,21 @@ module Cadenero
5
5
  module AuthenticationHelpers
6
6
  # creates a dummy user for testing
7
7
  # @return a dummy user JSON parameters for sign up
8
- def create_account_user
9
- @user ||= { email: "user@example.com", password: "password", password_confirmation: "password" }
8
+ def create_user_params_json(suffix = nil)
9
+ @user = { email: "user#{suffix}@example.com", password: "password", password_confirmation: "password" }
10
10
  end
11
11
  # @param user [Cadenero::User]
12
12
  # @return [JSON] a dummy user JSON parameters for sign in
13
- def account_user(user)
13
+ def account_user_params_json(user)
14
14
  @user = { email: user.email, password: "password" }
15
15
  end
16
16
 
17
17
  # find an account in the Database using the email of the owner
18
18
  # @return [Cadenero::V1::Account] the corresponding account that was founded
19
19
  def find_account_by_email
20
- @account = Cadenero::V1::Account.where(name: create_account_user[:email]).first
20
+ @account = Cadenero::V1::Account.where(name: create_user_params_json[:email]).first
21
21
  end
22
-
22
+
23
23
  # find an account in the Database using the name of the owner
24
24
  # @return [Cadenero::V1::Account] the corresponding account that was founded
25
25
  def find_account_by_name
@@ -39,7 +39,7 @@ module Cadenero
39
39
  # @param msg [JSON] the errors: as JSON
40
40
  def expected_json_errors(msg)
41
41
  expect(last_response.body).to eql(msg)
42
- expect(last_response.status).to eq 422
42
+ expect(last_response.status).to eq 422
43
43
  end
44
44
 
45
45
  # Expect that the JSON response will be a default error message when the user has not signed in yet
@@ -52,26 +52,63 @@ module Cadenero
52
52
 
53
53
  # Sign up a dummy user for testing
54
54
  # @return [Cadenero::V1::Account] the corresponding account that was founded
55
- def sign_up_user(url)
56
- post "#{url}/v1/users", format: :json, user: create_account_user
55
+ def sign_up_user(url, suffix=nil)
56
+ post "#{url}/v1/users", format: :json, user: create_user_params_json(suffix)
57
57
  find_account_by_email
58
58
  end
59
59
 
60
- # Expect that a owner sign in successfuly to an account
61
- # @param account [Cadenero::V1::Account]
62
- # @return email [String] for the last response user
63
- def successful_sign_in_owner(account)
64
- sign_in_user sessions_url, account_user(account.owner)
65
- expect(last_response.status).to eq 201
66
- expect(json_last_response_body["user"]["account_ids"]).to eq [account.id]
60
+ # Expect that the last_response JSON to have an auth_token and that should equal to the provided auth_token
61
+ # @param [String] subject
62
+ # @param [Array] auth_token
63
+ def expect_auth_token(subject, auth_token)
67
64
  expect(json_last_response_body).to have_content "auth_token"
68
- expect(json_last_response_body["user"]["auth_token"]).to eq [account.authentication_token]
65
+ expect(json_last_response_body[subject]["auth_token"]).to eq auth_token
66
+ end
67
+
68
+ # Expect that the last_response JSON key subject for the ids_key to have the ids_values
69
+ # @param [String] subject The key to look in the JSON
70
+ # @param [String] ids_key THe key for the subject that identify the ids
71
+ # @param [Array] ids_values THe array of expected ids values
72
+ # @param [Integer] http_code Optional expected returned HTTP Code from last_response
73
+ def expect_subject_ids_to_have(subject, ids_key, ids_values, http_code=201)
74
+ expect(last_response.status).to eq http_code
75
+ expect(json_last_response_body[subject][ids_key]).to eq ids_values
76
+ end
77
+
78
+ # Expect that a owner sign in successfuly to one of his accounts
79
+ # @param [Cadenero::V1::Account] account
80
+ # @return [String] email for the last response user
81
+ def successful_sign_in_owner(account)
82
+ sign_in_user sessions_url, account_user_params_json(account.owner)
83
+ expect_subject_ids_to_have("user", "account_ids", [account.id])
84
+ expect_auth_token("user", account.auth_token)
85
+ return json_last_response_body["user"]["email"]
86
+ end
87
+
88
+ # Expect that a user sign in successfuly to an account
89
+ # @param [Cadenero::V1::Account] account
90
+ # @return [String] email for the last response user
91
+ def successful_sign_in_user(account, user)
92
+ sign_in_user sessions_url, user
93
+ expect_subject_ids_to_have("user", "membership_ids", [account.id])
94
+ return json_last_response_body["user"]["email"]
95
+ end
96
+
97
+ # Expect that a user sign in successfuly to an account
98
+ # @param [Cadenero::V1::Account] account
99
+ # @return [String] email for the last response user
100
+ def successful_sign_up_user_in_existing_account(account, suffix=nil)
101
+ url = "http://#{account.subdomain}.example.com/"
102
+ sign_up_user url, suffix
103
+ expect(last_request.url).to eq "#{url}v1/users"
104
+ get "#{url}v1/users/#{json_last_response_body['user']['id']}"
105
+ expect_subject_ids_to_have("user", "membership_ids", [account.id], 200)
69
106
  return json_last_response_body["user"]["email"]
70
107
  end
71
108
 
72
109
  # creates a dummy account for testing
73
110
  # @return [JSON] a dummy account JSON parameters
74
- def create_account
111
+ def create_account_params_json
75
112
  @visitor ||= { name: "Testy", subdomain: "test", owner_attributes:
76
113
  {email: "testy@example.com", password: "changeme", password_confirmation: "changeme"} }
77
114
  end
@@ -79,7 +116,7 @@ module Cadenero
79
116
  # Sign up a dummy account for testing
80
117
  # @return [Cadenero::V1::Account] the corresponding account that was founded
81
118
  def sign_up_account
82
- post "/v1/accounts", format: :json, account: create_account
119
+ post "/v1/accounts", format: :json, account: create_account_params_json
83
120
  find_account_by_name
84
121
  end
85
122