|
4 | 4 | # Use the class we provide to test the module |
5 | 5 | subject { GitHub::App::Auth::AuthClass.new } |
6 | 6 |
|
7 | | - describe ".app_installation_client" do |
8 | | - let(:repo) { "test/test-repo" } |
| 7 | + let(:github_client) { instance_double(Octokit::Client) } |
| 8 | + let(:installation_id) { "54321" } |
| 9 | + let(:org) { "test-org" } |
| 10 | + let(:repo) { "test/test-repo" } |
| 11 | + let(:token) { "test-token" } |
| 12 | + let(:user) { "test-user" } |
9 | 13 |
|
10 | | - it "returns an Octokit::Client authorized to an app" do |
| 14 | + describe ".app_installation_client" do |
| 15 | + it "returns an Octokit::Client authorized to an app installation" do |
11 | 16 | expect(subject).to receive(:app_installation_token) |
12 | | - .with(repo, {}) |
13 | | - .and_return("test-token") |
| 17 | + .with(repo, {}) |
| 18 | + .and_return("test-token") |
14 | 19 | expect(subject.app_installation_client(repo)).to be_kind_of(Octokit::Client) |
15 | 20 | end |
| 21 | + |
| 22 | + it "returns an Octokit::Client authorized to an app installation" do |
| 23 | + expect(subject).to receive(:app_installation_token) |
| 24 | + .with(repo, {}) |
| 25 | + .and_return("test-token") |
| 26 | + expected_output = "DEPRECATED: app_installation_client will be removed in v0.4.0, use repository_installation_client instead\n" |
| 27 | + expect { subject.app_installation_client(repo) }.to output(expected_output).to_stdout |
| 28 | + end |
16 | 29 | end |
17 | 30 |
|
18 | 31 | describe ".app_installation_token" do |
19 | | - let(:github_client) { instance_double(Octokit::Client) } |
| 32 | + it "returns a JWT token for for an app" do |
| 33 | + expect(subject).to receive(:app_client) |
| 34 | + .and_return(github_client) |
| 35 | + expect(github_client).to receive(:find_repository_installation) |
| 36 | + .with(repo) |
| 37 | + .and_return(id: installation_id) |
| 38 | + expect(github_client).to receive(:create_app_installation_access_token) |
| 39 | + .with(installation_id) |
| 40 | + .and_return(token: token) |
| 41 | + expect(subject.app_installation_token(repo)).to eq(token) |
| 42 | + end |
20 | 43 |
|
21 | | - let(:installation_id) { "54321" } |
| 44 | + it "outputs a deprecation notice" do |
| 45 | + expect(subject).to receive(:app_client) |
| 46 | + .and_return(github_client) |
| 47 | + expect(github_client).to receive(:find_repository_installation) |
| 48 | + .with(repo) |
| 49 | + .and_return(id: installation_id) |
| 50 | + expect(github_client).to receive(:create_app_installation_access_token) |
| 51 | + .with(installation_id) |
| 52 | + .and_return(token: token) |
| 53 | + expected_output = [ |
| 54 | + "DEPRECATED: app_installation_client will be removed in v0.4.0, use repository_installation_client instead", |
| 55 | + "DEPRECATED: app_installation_token will be removed in v0.4.0, use repository_installation_token instead\n" |
| 56 | + ].join("\n") |
| 57 | + expect { subject.app_installation_client(repo) }.to output(expected_output).to_stdout |
| 58 | + end |
| 59 | + end |
22 | 60 |
|
23 | | - let(:repo) { "test/test-repo" } |
| 61 | + describe ".organization_installation_client" do |
| 62 | + it "returns an Octokit::Client authorized to an organization installation" do |
| 63 | + expect(subject).to receive(:organization_installation_token) |
| 64 | + .with(org, {}) |
| 65 | + .and_return(token) |
| 66 | + expect(subject.organization_installation_client(org)).to be_kind_of(Octokit::Client) |
| 67 | + end |
| 68 | + end |
24 | 69 |
|
| 70 | + describe ".organization_installation_token" do |
25 | 71 | it "returns a JWT token for for an app" do |
26 | 72 | expect(subject).to receive(:app_client) |
27 | | - .and_return(github_client) |
| 73 | + .and_return(github_client) |
| 74 | + expect(github_client).to receive(:find_organization_installation) |
| 75 | + .with(org) |
| 76 | + .and_return(id: installation_id) |
| 77 | + expect(github_client).to receive(:create_app_installation_access_token) |
| 78 | + .with(installation_id) |
| 79 | + .and_return(token: token) |
| 80 | + expect(subject.organization_installation_token(org)).to eq("test-token") |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + describe ".repository_installation_client" do |
| 85 | + it "returns an Octokit::Client authorized to a repo installation" do |
| 86 | + expect(subject).to receive(:repository_installation_token) |
| 87 | + .with(repo, {}) |
| 88 | + .and_return(token) |
| 89 | + expect(subject.repository_installation_client(repo)).to be_kind_of(Octokit::Client) |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + describe ".repository_installation_token" do |
| 94 | + it "returns a JWT token for for a repo" do |
| 95 | + expect(subject).to receive(:app_client) |
| 96 | + .and_return(github_client) |
| 97 | + expect(github_client).to receive(:find_repository_installation) |
| 98 | + .with(repo) |
| 99 | + .and_return(id: installation_id) |
| 100 | + expect(github_client).to receive(:create_app_installation_access_token) |
| 101 | + .with(installation_id) |
| 102 | + .and_return(token: token) |
| 103 | + expect(subject.repository_installation_token(repo)).to eq(token) |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + describe ".user_installation_client" do |
| 108 | + it "returns an Octokit::Client authorized to a user installation" do |
| 109 | + expect(subject).to receive(:user_installation_token) |
| 110 | + .with(user, {}) |
| 111 | + .and_return(token) |
| 112 | + expect(subject.user_installation_client(user)).to be_kind_of(Octokit::Client) |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + describe ".user_installation_token" do |
| 117 | + it "returns a JWT token for for a user" do |
| 118 | + expect(subject).to receive(:app_client) |
| 119 | + .and_return(github_client) |
| 120 | + expect(github_client).to receive(:find_user_installation) |
| 121 | + .with(user) |
| 122 | + .and_return(id: installation_id) |
| 123 | + expect(github_client).to receive(:create_app_installation_access_token) |
| 124 | + .with(installation_id) |
| 125 | + .and_return(token: token) |
| 126 | + expect(subject.user_installation_token(user)).to eq(token) |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + describe ".installation_token" do |
| 131 | + it "raises an error for unsupported installation type" do |
| 132 | + expect(subject).to receive(:app_client) |
| 133 | + .and_return(github_client) |
| 134 | + expect { subject.installation_token("unsupported", {}) }.to raise_error(ArgumentError) |
| 135 | + end |
| 136 | + |
| 137 | + it "raises an error if no installation id is found" do |
| 138 | + expect(subject).to receive(:app_client) |
| 139 | + .and_return(github_client) |
| 140 | + expect(github_client).to receive(:find_repository_installation) |
| 141 | + .with(repo) |
| 142 | + .and_return(token: nil) |
| 143 | + expect { subject.installation_token(:repository, repo, {}) }.to raise_error(GitHub::App::Auth::InstallationError) |
| 144 | + end |
| 145 | + |
| 146 | + it "raises an error if no installation is returned" do |
| 147 | + expect(subject).to receive(:app_client) |
| 148 | + .and_return(github_client) |
| 149 | + expect(github_client).to receive(:find_repository_installation) |
| 150 | + .with(repo) |
| 151 | + .and_return(nil) |
| 152 | + expect { subject.installation_token(:repository, repo, {}) }.to raise_error(GitHub::App::Auth::InstallationError) |
| 153 | + end |
| 154 | + |
| 155 | + it "raises an error if no token is returned" do |
| 156 | + expect(subject).to receive(:app_client) |
| 157 | + .and_return(github_client) |
| 158 | + expect(github_client).to receive(:find_repository_installation) |
| 159 | + .with(repo) |
| 160 | + .and_return(id: installation_id) |
| 161 | + expect(github_client).to receive(:create_app_installation_access_token) |
| 162 | + .with(installation_id) |
| 163 | + .and_return(token: nil) |
| 164 | + expect { subject.installation_token(:repository, repo, {}) }.to raise_error(GitHub::App::Auth::TokenError) |
| 165 | + end |
| 166 | + |
| 167 | + it "raises an error if resp is nil for token creation" do |
| 168 | + expect(subject).to receive(:app_client) |
| 169 | + .and_return(github_client) |
28 | 170 | expect(github_client).to receive(:find_repository_installation) |
29 | | - .with("test/test-repo") |
| 171 | + .with(repo) |
30 | 172 | .and_return(id: installation_id) |
31 | 173 | expect(github_client).to receive(:create_app_installation_access_token) |
32 | 174 | .with(installation_id) |
33 | | - .and_return(token: "test-token") |
34 | | - expect(subject.app_installation_token(repo)).to eq("test-token") |
| 175 | + .and_return(nil) |
| 176 | + expect { subject.installation_token(:repository, repo, {}) }.to raise_error(GitHub::App::Auth::TokenError) |
35 | 177 | end |
36 | 178 | end |
37 | 179 | end |
0 commit comments