Skip to content

Commit 778477f

Browse files
committed
tests/util/github: Move public keys mock into github_secret_scanning test module
1 parent f44644f commit 778477f

File tree

2 files changed

+28
-38
lines changed

2 files changed

+28
-38
lines changed

src/tests/github_secret_scanning.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::tests::util::MockRequestExt;
22
use crate::tests::{RequestHelper, TestApp};
33
use crate::util::token::HashedToken;
44
use crate::{models::ApiToken, schema::api_tokens};
5+
use crates_io_github::{GitHubPublicKey, MockGitHubClient};
56
use diesel::prelude::*;
67
use diesel_async::RunQueryDsl;
78
use googletest::prelude::*;
@@ -13,13 +14,34 @@ static URL: &str = "/api/github/secret-scanning/verify";
1314
// Test request and signature from https://docs.github.com/en/developers/overview/secret-scanning-partner-program#create-a-secret-alert-service
1415
static GITHUB_ALERT: &[u8] =
1516
br#"[{"token":"some_token","type":"some_type","url":"some_url","source":"some_source"}]"#;
17+
1618
static GITHUB_PUBLIC_KEY_IDENTIFIER: &str =
1719
"f9525bf080f75b3506ca1ead061add62b8633a346606dc5fe544e29231c6ee0d";
20+
21+
/// Test key from https://docs.github.com/en/developers/overview/secret-scanning-partner-program#create-a-secret-alert-service
22+
static GITHUB_PUBLIC_KEY: &str = "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEsz9ugWDj5jK5ELBK42ynytbo38gP\nHzZFI03Exwz8Lh/tCfL3YxwMdLjB+bMznsanlhK0RwcGP3IDb34kQDIo3Q==\n-----END PUBLIC KEY-----";
23+
1824
static GITHUB_PUBLIC_KEY_SIGNATURE: &str = "MEUCIFLZzeK++IhS+y276SRk2Pe5LfDrfvTXu6iwKKcFGCrvAiEAhHN2kDOhy2I6eGkOFmxNkOJ+L2y8oQ9A2T9GGJo6WJY=";
1925

26+
fn github_mock() -> MockGitHubClient {
27+
let mut mock = MockGitHubClient::new();
28+
29+
mock.expect_public_keys().returning(|_, _| {
30+
let key = GitHubPublicKey {
31+
key_identifier: GITHUB_PUBLIC_KEY_IDENTIFIER.to_string(),
32+
key: GITHUB_PUBLIC_KEY.to_string(),
33+
is_current: true,
34+
};
35+
36+
Ok(vec![key])
37+
});
38+
39+
mock
40+
}
41+
2042
#[tokio::test(flavor = "multi_thread")]
2143
async fn github_secret_alert_revokes_token() {
22-
let (app, anon, user, token) = TestApp::init().with_token();
44+
let (app, anon, user, token) = TestApp::init().with_github(github_mock()).with_token();
2345
let mut conn = app.async_db_conn().await;
2446

2547
// Ensure no emails were sent up to this point
@@ -77,7 +99,7 @@ async fn github_secret_alert_revokes_token() {
7799

78100
#[tokio::test(flavor = "multi_thread")]
79101
async fn github_secret_alert_for_revoked_token() {
80-
let (app, anon, user, token) = TestApp::init().with_token();
102+
let (app, anon, user, token) = TestApp::init().with_github(github_mock()).with_token();
81103
let mut conn = app.async_db_conn().await;
82104

83105
// Ensure no emails were sent up to this point
@@ -138,7 +160,7 @@ async fn github_secret_alert_for_revoked_token() {
138160

139161
#[tokio::test(flavor = "multi_thread")]
140162
async fn github_secret_alert_for_unknown_token() {
141-
let (app, anon, user, token) = TestApp::init().with_token();
163+
let (app, anon, user, token) = TestApp::init().with_github(github_mock()).with_token();
142164
let mut conn = app.async_db_conn().await;
143165

144166
// Ensure no emails were sent up to this point
@@ -180,7 +202,7 @@ async fn github_secret_alert_for_unknown_token() {
180202

181203
#[tokio::test(flavor = "multi_thread")]
182204
async fn github_secret_alert_invalid_signature_fails() {
183-
let (_, anon) = TestApp::init().empty();
205+
let (_, anon) = TestApp::init().with_github(github_mock()).empty();
184206

185207
// No headers or request body
186208
let request = anon.post_request(URL);

src/tests/util/github.rs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::anyhow;
22
use crates_io_github::{
3-
GitHubError, GitHubOrgMembership, GitHubOrganization, GitHubPublicKey, GitHubTeam,
4-
GitHubTeamMembership, GithubUser, MockGitHubClient,
3+
GitHubError, GitHubOrgMembership, GitHubOrganization, GitHubTeam, GitHubTeamMembership,
4+
GithubUser, MockGitHubClient,
55
};
66
use std::sync::atomic::{AtomicUsize, Ordering};
77

@@ -49,14 +49,6 @@ pub(crate) const MOCK_GITHUB_DATA: MockData = MockData {
4949
5050
},
5151
],
52-
// Test key from https://docs.github.com/en/developers/overview/secret-scanning-partner-program#create-a-secret-alert-service
53-
public_keys: &[
54-
MockPublicKey {
55-
key_identifier: "f9525bf080f75b3506ca1ead061add62b8633a346606dc5fe544e29231c6ee0d",
56-
key: "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEsz9ugWDj5jK5ELBK42ynytbo38gP\nHzZFI03Exwz8Lh/tCfL3YxwMdLjB+bMznsanlhK0RwcGP3IDb34kQDIo3Q==\n-----END PUBLIC KEY-----",
57-
is_current: true,
58-
},
59-
],
6052
};
6153

6254
impl MockData {
@@ -80,9 +72,6 @@ impl MockData {
8072
mock.expect_org_membership()
8173
.returning(|org_id, username, _auth| self.org_membership(org_id, username));
8274

83-
mock.expect_public_keys()
84-
.returning(|_username, _password| self.public_keys());
85-
8675
mock
8776
}
8877

@@ -178,10 +167,6 @@ impl MockData {
178167
Err(not_found())
179168
}
180169
}
181-
182-
fn public_keys(&self) -> Result<Vec<GitHubPublicKey>, GitHubError> {
183-
Ok(self.public_keys.iter().map(Into::into).collect())
184-
}
185170
}
186171

187172
fn not_found() -> GitHubError {
@@ -191,7 +176,6 @@ fn not_found() -> GitHubError {
191176
pub(crate) struct MockData {
192177
orgs: &'static [MockOrg],
193178
users: &'static [MockUser],
194-
public_keys: &'static [MockPublicKey],
195179
}
196180

197181
struct MockUser {
@@ -213,19 +197,3 @@ struct MockTeam {
213197
name: &'static str,
214198
members: &'static [&'static str],
215199
}
216-
217-
struct MockPublicKey {
218-
key_identifier: &'static str,
219-
key: &'static str,
220-
is_current: bool,
221-
}
222-
223-
impl From<&'static MockPublicKey> for GitHubPublicKey {
224-
fn from(k: &'static MockPublicKey) -> Self {
225-
Self {
226-
key_identifier: k.key_identifier.to_string(),
227-
key: k.key.to_string(),
228-
is_current: k.is_current,
229-
}
230-
}
231-
}

0 commit comments

Comments
 (0)