|
47 | 47 | end |
48 | 48 | end |
49 | 49 |
|
| 50 | + describe "#org_member?" do |
| 51 | + let(:members_and_roles) do |
| 52 | + { |
| 53 | + "alice" => "MEMBER", |
| 54 | + "bob" => "ADMIN" |
| 55 | + } |
| 56 | + end |
| 57 | + |
| 58 | + before do |
| 59 | + allow(subject).to receive(:members_and_roles_from_rest).and_return(members_and_roles) |
| 60 | + end |
| 61 | + |
| 62 | + it "performs case-insensitive lookups" do |
| 63 | + expect(subject.org_member?("ALIce")).to eq(true) |
| 64 | + expect(subject.org_member?("charles")).to eq(false) |
| 65 | + end |
| 66 | + |
| 67 | + it "reuses the normalized membership set" do |
| 68 | + expect(subject.org_member?("alice")).to eq(true) |
| 69 | + normalized_members = Entitlements.cache[:github_org_members]["https://github.fake/api/v3|kittensinc"][:normalized_members] |
| 70 | + expect(subject.org_member?("bob")).to eq(true) |
| 71 | + expect(Entitlements.cache[:github_org_members]["https://github.fake/api/v3|kittensinc"][:normalized_members]).to equal(normalized_members) |
| 72 | + end |
| 73 | + |
| 74 | + it "shares the normalized membership set between services for the same organization signature" do |
| 75 | + other = described_class.new( |
| 76 | + addr: "https://github.fake/api/v3", |
| 77 | + org: "kittensinc", |
| 78 | + token: "DifferentToken", |
| 79 | + ou: "ou=kittensinc,ou=GitHub,dc=github,dc=fake", |
| 80 | + ignore_not_found: false |
| 81 | + ) |
| 82 | + |
| 83 | + expect(subject.org_member?("alice")).to eq(true) |
| 84 | + normalized_members = Entitlements.cache[:github_org_members]["https://github.fake/api/v3|kittensinc"][:normalized_members] |
| 85 | + expect(other.org_member?("bob")).to eq(true) |
| 86 | + expect(Entitlements.cache[:github_org_members]["https://github.fake/api/v3|kittensinc"][:normalized_members]).to equal(normalized_members) |
| 87 | + end |
| 88 | + |
| 89 | + it "isolates normalized membership sets by organization and GitHub instance" do |
| 90 | + other_org = described_class.new( |
| 91 | + addr: "https://github.fake/api/v3", |
| 92 | + org: "puppiesinc", |
| 93 | + token: "GoPackGo", |
| 94 | + ou: "ou=puppiesinc,ou=GitHub,dc=github,dc=fake", |
| 95 | + ignore_not_found: false |
| 96 | + ) |
| 97 | + other_instance = described_class.new( |
| 98 | + addr: "https://github.example/api/v3", |
| 99 | + org: "kittensinc", |
| 100 | + token: "GoPackGo", |
| 101 | + ou: "ou=kittensinc,ou=GitHub,dc=github,dc=example", |
| 102 | + ignore_not_found: false |
| 103 | + ) |
| 104 | + allow(other_org).to receive(:members_and_roles_from_rest).and_return("charles" => "MEMBER") |
| 105 | + allow(other_instance).to receive(:members_and_roles_from_rest).and_return("david" => "MEMBER") |
| 106 | + |
| 107 | + expect(subject.org_member?("alice")).to eq(true) |
| 108 | + expect(other_org.org_member?("alice")).to eq(false) |
| 109 | + expect(other_org.org_member?("charles")).to eq(true) |
| 110 | + expect(other_instance.org_member?("alice")).to eq(false) |
| 111 | + expect(other_instance.org_member?("david")).to eq(true) |
| 112 | + end |
| 113 | + end |
| 114 | + |
50 | 115 | describe "#enterprise?" do |
51 | 116 | it "returns false if an instance is not enterprise" do |
52 | 117 | stub_request(:get, "https://github.fake/api/v3/meta"). |
|
122 | 187 |
|
123 | 188 | # First load should read from the cache. |
124 | 189 | expect(subject.org_members).to eq(answer.map { |k, v| [k, v.downcase] }.to_h) |
| 190 | + expect(subject.org_member?("monalisa")).to eq(true) |
| 191 | + normalized_members = Entitlements.cache[:github_org_members]["https://github.fake/api/v3|kittensinc"][:normalized_members] |
125 | 192 |
|
126 | 193 | # Invalidating cache should force a re-read. |
127 | 194 | answer_2 = answer.dup |
|
137 | 204 | # Check that the re-read has occurred and the correct result is achieved. |
138 | 205 | expect(subject).not_to receive(:members_and_roles_from_graphql) # Should already be in object's cache |
139 | 206 | expect(subject.org_members).to eq(answer_2.map { |k, v| [k, v.downcase] }.to_h) |
| 207 | + expect(subject.org_member?("ragamuffin")).to eq(true) |
| 208 | + expect(Entitlements.cache[:github_org_members]["https://github.fake/api/v3|kittensinc"][:normalized_members]).not_to equal(normalized_members) |
140 | 209 | end |
141 | 210 | end |
142 | 211 |
|
|
0 commit comments