Skip to content

Commit

Permalink
[JEDI-340] Filter out inactive users (#382)
Browse files Browse the repository at this point in the history
https://runway.powerhrg.com/backlog_items/JEDI-340

---------

Co-authored-by: Carlos Palhares <chjunior@gmail.com>
  • Loading branch information
codemonium and xjunior authored Sep 3, 2024
1 parent 77958df commit b2a6b6b
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 33 deletions.
2 changes: 1 addition & 1 deletion audiences/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
audiences (1.2.2)
audiences (1.3.0)
rails (>= 6.0)

GEM
Expand Down
4 changes: 4 additions & 0 deletions audiences/docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

# Version 1.3.0 (2024-09-03)

- Filter out inactive users by default [#382](https://github.com/powerhome/audiences/pull/382)

# Version 1.2.2 (2024-08-21)

- Permit configured resource attributes [#375](https://github.com/powerhome/audiences/pull/375)
Expand Down
2 changes: 1 addition & 1 deletion audiences/gemfiles/rails_6_1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
audiences (1.2.2)
audiences (1.3.0)
rails (>= 6.0)

GEM
Expand Down
2 changes: 1 addition & 1 deletion audiences/gemfiles/rails_7_0.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
audiences (1.2.2)
audiences (1.3.0)
rails (>= 6.0)

GEM
Expand Down
2 changes: 1 addition & 1 deletion audiences/gemfiles/rails_7_1.gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
audiences (1.2.2)
audiences (1.3.0)
rails (>= 6.0)

GEM
Expand Down
3 changes: 2 additions & 1 deletion audiences/lib/audiences/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ module Audiences
# @see `resource`.
#
config_accessor :resources do
{ Users: Scim::Resource.new(type: :Users, attributes: ["photos" => %w[type value]]) }
{ Users: Scim::Resource.new(type: :Users, attributes: ["active", { "photos" => %w[type value] }],
filter: "active eq true") }
end

#
Expand Down
14 changes: 12 additions & 2 deletions audiences/lib/audiences/scim/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
module Audiences
module Scim
class Resource
attr_accessor :options, :type, :attributes
attr_accessor :options, :type, :attributes, :filter

def initialize(type:, attributes: [], **options)
def initialize(type:, attributes: [], filter: nil, **options)
@type = type
@options = options
@attributes = ["id", "externalId", "displayName", *attributes]
@filter = filter
end

def query(**options)
options_filter = options.delete(:filter)
ResourcesQuery.new(Scim.client, resource: self,
attributes: scim_attributes,
filter: merged_filter(options_filter),
**@options, **options)
end

Expand All @@ -29,6 +32,13 @@ def scim_attributes
end
end.join(",")
end

def merged_filter(filter)
return @filter unless filter
return filter unless @filter

"(#{@filter}) and (#{filter})"
end
end
end
end
2 changes: 1 addition & 1 deletion audiences/lib/audiences/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Audiences
VERSION = "1.2.2"
VERSION = "1.3.0"
end
18 changes: 9 additions & 9 deletions audiences/spec/lib/audiences_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
let(:token) { Audiences::Context.for(baseball_club).signed_key }

it "updates an audience context from a given key and params" do
stub_request(:get, "http://example.com/scim/v2/Users?attributes=id,externalId,displayName,photos.type,photos.value")
stub_request(:get, "http://example.com/scim/v2/Users?attributes=id,externalId,displayName,active,photos.type,photos.value&filter=active%20eq%20true")
.with(query: {})
.to_return(status: 200, body: { "Resources" => [] }.to_json)

Expand All @@ -26,17 +26,17 @@
end

it "updates group criterion" do
stub_request(:get, "http://example.com/scim/v2/Users?attributes=id,externalId,displayName,photos.type,photos.value")
.with(query: { filter: "groups.value eq 1 OR groups.value eq 2" })
stub_request(:get, "http://example.com/scim/v2/Users?attributes=id,externalId,displayName,active,photos.type,photos.value&filter=active%20eq%20true")
.with(query: { filter: "(active eq true) and (groups.value eq 1 OR groups.value eq 2)" })
.to_return(status: 200, body: { "Resources" => [] }.to_json)
stub_request(:get, "http://example.com/scim/v2/Users?attributes=id,externalId,displayName,photos.type,photos.value")
.with(query: { filter: "groups.value eq 3 OR groups.value eq 4" })
stub_request(:get, "http://example.com/scim/v2/Users?attributes=id,externalId,displayName,active,photos.type,photos.value&filter=active%20eq%20true")
.with(query: { filter: "(active eq true) and (groups.value eq 3 OR groups.value eq 4)" })
.to_return(status: 200, body: { "Resources" => [] }.to_json)
stub_request(:get, "http://example.com/scim/v2/Users?attributes=id,externalId,displayName,photos.type,photos.value")
.with(query: { filter: "groups.value eq 5 OR groups.value eq 6" })
stub_request(:get, "http://example.com/scim/v2/Users?attributes=id,externalId,displayName,active,photos.type,photos.value&filter=active%20eq%20true")
.with(query: { filter: "(active eq true) and (groups.value eq 5 OR groups.value eq 6)" })
.to_return(status: 200, body: { "Resources" => [] }.to_json)
stub_request(:get, "http://example.com/scim/v2/Users?attributes=id,externalId,displayName,photos.type,photos.value")
.with(query: { filter: "groups.value eq 7 OR groups.value eq 8" })
stub_request(:get, "http://example.com/scim/v2/Users?attributes=id,externalId,displayName,active,photos.type,photos.value&filter=active%20eq%20true")
.with(query: { filter: "(active eq true) and (groups.value eq 7 OR groups.value eq 8)" })
.to_return(status: 200, body: { "Resources" => [] }.to_json)

updated_context = Audiences.update(
Expand Down
2 changes: 1 addition & 1 deletion audiences/spec/models/audiences/context_users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{ "externalId" => 1414 },
],
}
stub_request(:get, "http://example.com/scim/v2/Users?attributes=id,externalId,displayName,photos.type,photos.value")
stub_request(:get, "http://example.com/scim/v2/Users?attributes=id,externalId,displayName,active,photos.type,photos.value&filter=active%20eq%20true")
.to_return(status: 200, body: response.to_json)

context = Audiences::Context.new(match_all: true)
Expand Down
5 changes: 3 additions & 2 deletions audiences/spec/models/audiences/criterion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
let(:context) { Audiences::Context.for(owner) }

it "fetches the criteria matching users" do
attrs = "id,externalId,displayName,photos.type,photos.value"
stub_request(:get, "http://example.com/scim/v2/Users?attributes=#{attrs}&filter=groups.value eq 123")
attrs = "id,externalId,displayName,active,photos.type,photos.value"
stub_request(:get, "http://example.com/scim/v2/Users?attributes=#{attrs}" \
"&filter=(active eq true) and (groups.value eq 123)")
.to_return(status: 200, body: { "Resources" => [{ "externalId" => 13 }] }.to_json)

criterion = context.criteria.create!(groups: { Departments: [{ id: 123 }] })
Expand Down
14 changes: 7 additions & 7 deletions audiences/spec/models/audiences/criterion_users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
],
}

attrs = "id,externalId,displayName,photos.type,photos.value"
attrs = "id,externalId,displayName,active,photos.type,photos.value"
stub_request(:get, "http://example.com/scim/v2/Users?attributes=#{attrs}" \
"&filter=groups.value eq 1 OR groups.value eq 3")
"&filter=(active eq true) and (groups.value eq 1 OR groups.value eq 3)")
.to_return(status: 200, body: response.to_json)

users = Audiences::CriterionUsers.new(criterion).to_a
Expand Down Expand Up @@ -45,12 +45,12 @@
],
}

attrs = "id,externalId,displayName,photos.type,photos.value"
attrs = "id,externalId,displayName,active,photos.type,photos.value"
stub_request(:get, "http://example.com/scim/v2/Users?attributes=#{attrs}" \
"&filter=groups.value eq 1 OR groups.value eq 3")
"&filter=(active eq true) and (groups.value eq 1 OR groups.value eq 3)")
.to_return(status: 200, body: response1or3.to_json)
stub_request(:get, "http://example.com/scim/v2/Users?attributes=#{attrs}" \
"&filter=groups.value eq 5 OR groups.value eq 6")
"&filter=(active eq true) and (groups.value eq 5 OR groups.value eq 6)")
.to_return(status: 200, body: response5or6.to_json)

users = Audiences::CriterionUsers.new(criterion).to_a
Expand Down Expand Up @@ -80,9 +80,9 @@
],
}

attrs = "id,externalId,displayName,photos.type,photos.value"
attrs = "id,externalId,displayName,active,photos.type,photos.value"
stub_request(:get, "http://example.com/scim/v2/Users?attributes=#{attrs}" \
"&filter=groups.value eq 1 OR groups.value eq 3")
"&filter=(active eq true) and (groups.value eq 1 OR groups.value eq 3)")
.to_return(status: 200, body: response1or3.to_json)

stub_request(:get, "http://example.com/scim/v2/Users?attributes=#{attrs}&filter=")
Expand Down
12 changes: 6 additions & 6 deletions audiences/spec/requests/contexts_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
end

it "updates the audience context to match all" do
stub_request(:get, "http://example.com/scim/v2/Users?attributes=id,externalId,displayName,photos.type,photos.value")
stub_request(:get, "http://example.com/scim/v2/Users?attributes=id,externalId,displayName,active,photos.type,photos.value&filter=active%20eq%20true")
.to_return(status: 200, body: users_response.to_json, headers: {})

put audience_context_path(example_owner, :members), as: :json, params: { match_all: true }
Expand Down Expand Up @@ -78,18 +78,18 @@
end

it "allows updating the group criteria" do
attrs = "id,externalId,displayName,photos.type,photos.value"
attrs = "id,externalId,displayName,active,photos.type,photos.value"
stub_request(:get, "http://example.com/scim/v2/Users?attributes=#{attrs}" \
"&filter=groups.value eq 123")
"&filter=(active eq true) and (groups.value eq 123)")
.to_return(status: 200, body: users_response.to_json, headers: {})
stub_request(:get, "http://example.com/scim/v2/Users?attributes=#{attrs}" \
"&filter=groups.value eq 321")
"&filter=(active eq true) and (groups.value eq 321)")
.to_return(status: 200, body: users_response.to_json, headers: {})
stub_request(:get, "http://example.com/scim/v2/Users?attributes=#{attrs}" \
"&filter=groups.value eq 789")
"&filter=(active eq true) and (groups.value eq 789)")
.to_return(status: 200, body: users_response.to_json, headers: {})
stub_request(:get, "http://example.com/scim/v2/Users?attributes=#{attrs}" \
"&filter=groups.value eq 987")
"&filter=(active eq true) and (groups.value eq 987)")
.to_return(status: 200, body: users_response.to_json, headers: {})

put audience_context_path(example_owner, :members),
Expand Down

0 comments on commit b2a6b6b

Please sign in to comment.