active_entry 1.2.3 → 1.2.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 22965fa52ab633ba26b494ec4d4e75d9b21d3a0daa4274b38a4369c7af1d8a90
4
- data.tar.gz: 9e06a922ddf9421f6e5355498af672e125c87a476b92f622d506363083f4bd02
3
+ metadata.gz: ab6abb6cb8a8069912d414cd395c1b57ee8327152f60ff9808db387eae70d3e2
4
+ data.tar.gz: 48cf2369782b109f88208be35553075d253d78bd45151e5ca1c9e634143f8433
5
5
  SHA512:
6
- metadata.gz: 047a796891e8faca672e58b0dba5fe1b1cf06494880abc40411ee4f58aa9d24bb47d275c33ded3184b0884fbe71f78de263d4c1e7383b879601b7fcd4c717d41
7
- data.tar.gz: 5e08a364e44dd87a8b9dfcb8923f2675e95ce7d75cb30b7439aa57fb325df81236cc0cb8760beb32bfaee69d2c46ba9a30454b0e896d443ac27ef1c1b13aecd2
6
+ metadata.gz: f65b2eb50012c246c91c98df7ef238de33ff1469893e611a58eea569843d9ec45bfe79099afc1f94e2bfe898a20d9e70fdb0678b190fde9f63dcf5051590e248
7
+ data.tar.gz: 3f2b4ba32b292375292fa3e7c9c040de2cbb8d812f206b6c27e52fe7eb9da0013a5de6ae49c1052467fa07e5a83f73a4fc450b426963fbe74e14ef04ce231edf
data/README.md CHANGED
@@ -1,6 +1,15 @@
1
- [<img src="active_entry_logo.png" alt="Active Entry Logo" width="250px"/>](https://github.com/TFM-Agency/active_entry)
2
-
3
- # Active Entry - Simple and flexible authentication and authorization [![Ruby](https://github.com/TFM-Agency/active_entry/actions/workflows/ci-rspec.yml/badge.svg)](https://github.com/TFM-Agency/active_entry/actions/workflows/ci-rspec.yml)
1
+ <p align="center">
2
+ <a href="https://github.com/TFM-Agency/active_entry">
3
+ <img src="https://raw.githubusercontent.com/TFM-Agency/active_entry/main/active_entry_logo.svg" alt="Active Entry Logo" width="350px"/>
4
+ </a>
5
+ </p>
6
+
7
+ # Active Entry - Simple and flexible authentication and authorization
8
+ [![Gem Version](https://badge.fury.io/rb/active_entry.svg)](https://badge.fury.io/rb/active_entry)
9
+ [![Ruby](https://github.com/TFM-Agency/active_entry/actions/workflows/ci-rspec.yml/badge.svg)](https://github.com/TFM-Agency/active_entry/actions/workflows/ci-rspec.yml)
10
+ ![Coverage](https://raw.githubusercontent.com/TFM-Agency/active_entry/main/coverage/coverage_badge_total.svg)
11
+ [![Maintainability](https://api.codeclimate.com/v1/badges/3db0f653be6bdfe0fdac/maintainability)](https://codeclimate.com/github/TFM-Agency/active_entry/maintainability)
12
+ [![Documentation](https://img.shields.io/badge/docs-rdoc.info-blue.svg)](https://rubydoc.info/github/TFM-Agency/active_entry/main)
4
13
 
5
14
  Active Entry is a simple and secure authentication and authorization system for your Rails application, which lets you to authenticate and authorize directly in your controllers.
6
15
 
@@ -75,8 +84,8 @@ Now you just have to catch this error and react accordingly. Rails has the conve
75
84
  class ApplicationController < ActionController::Base
76
85
  # ...
77
86
 
78
- rescue_from ActiveEntry::NotAuthenticatedError, with: :not_authenticated
79
- rescue_from ActiveEntry::NotAuthorizedError, with: :not_authorized
87
+ rescue_from ActiveEntry::NotAuthenticatedError, with: :not_authenticated unless Rails.env.test?
88
+ rescue_from ActiveEntry::NotAuthorizedError, with: :not_authorized unless Rails.env.test?
80
89
 
81
90
  private
82
91
 
@@ -134,6 +143,8 @@ The are some more helpers that check for more than one RESTful action:
134
143
  * `update_action?` - If something will be updated. Actions: `edit`, `update`
135
144
  * `destroy_action?` - If something will be destroyed. Action: `destroy`
136
145
  * `delete_action?` - Alias for `destroy_action?`. Action: `destroy`
146
+ * `collection_action?` - If the called action is a collection action. Actions: `index`, `new`, `create`
147
+ * `member_action?` - Everything that is not a collection action. Including non-RESTful actions.
137
148
 
138
149
  So you can for example do:
139
150
 
@@ -206,6 +217,60 @@ class ApplicationController < ActionController::Base
206
217
  end
207
218
  end
208
219
  ```
220
+ ## Testing authentication and authorization
221
+ If you check for the Rails environment with `unless Rails.env.test?` in your `rescue_from` statement you can easily test your authentication and authorization in your tests.
222
+
223
+ ```ruby
224
+ class ApplicationController < ActionController::Base
225
+ # ...
226
+ rescue_from ActiveEntry::NotAuthenticatedError, with: :not_authenticated unless Rails.env.test?
227
+ rescue_from ActiveEntry::NotAuthorizedError, with: :not_authorized unless Rails.env.test?
228
+ # ...
229
+ end
230
+ ```
231
+
232
+ Now you can catch `ActiveEntry::NotAuthenticatedError` / `ActiveEntry::NotAuthorizedError` in your test site like this:
233
+
234
+ ```ruby
235
+ require "rails_helper"
236
+
237
+ RSpec.describe "Users", type: :request do
238
+ describe "Authentication" do
239
+ context "#index" do
240
+ context "authenticated" do
241
+ it "as signed in user" do
242
+ sign_in_as user
243
+ expect{ get users_path }.to_not raise_error ActiveEntry::NotAuthenticatedError
244
+ end
245
+ end
246
+
247
+ context "not authenticated" do
248
+ it "as stranger" do
249
+ expect{ get users_path }.to raise_error ActiveEntry::NotAuthenticatedError
250
+ end
251
+ end
252
+ end
253
+ end
254
+
255
+ describe "Authorization" do
256
+ context "#index" do
257
+ context "authorized" do
258
+ it "as admin" do
259
+ sign_in_as admin
260
+ expect{ get users_path }.to_not raise_error ActiveEntry::NotAuthorizedError
261
+ end
262
+ end
263
+
264
+ context "not authenticated" do
265
+ it "as non-admin" do
266
+ sign_in_as user
267
+ expect{ get users_path }.to raise_error ActiveEntry::NotAuthorizedError
268
+ end
269
+ end
270
+ end
271
+ end
272
+ end
273
+ ```
209
274
 
210
275
  ## Contributing
211
276
  Create pull requests on Github and help us to improve this Gem. There are some guidelines to follow:
@@ -215,4 +280,4 @@ Create pull requests on Github and help us to improve this Gem. There are some g
215
280
  * Document methods that aren't self-explaining (we are using [YARD](http://yardoc.org/))
216
281
 
217
282
  ## License
218
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
283
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -3,6 +3,7 @@
3
3
  # Helper methods for your controller
4
4
  # to identify RESTful actions.
5
5
  module ActiveEntry
6
+ # @!visibility private
6
7
  def method_missing method_name, *args
7
8
  method_name_str = method_name.to_s
8
9
 
@@ -79,4 +80,20 @@ module ActiveEntry
79
80
  action_name == 'destroy'
80
81
  end
81
82
  alias delete_action? destroy_action?
83
+
84
+ # @return [Boolean]
85
+ # True if called action
86
+ # is index, new or create.
87
+ def collection_action?
88
+ action_name == 'index' ||
89
+ action_name == 'new' ||
90
+ action_name == 'create'
91
+ end
92
+
93
+ # @return [Boolean]
94
+ # True if called action
95
+ # is not a collection action.
96
+ def member_action?
97
+ !collection_action?
98
+ end
82
99
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveEntry
2
- VERSION = '1.2.3'
2
+ VERSION = '1.2.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_entry
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - TFM Agency GmbH
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-03-04 00:00:00.000000000 Z
12
+ date: 2021-03-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails