Skip to content

Commit 109600f

Browse files
committed
refactor: Add more tests and traits
1 parent 22f7b63 commit 109600f

File tree

4 files changed

+93
-9
lines changed

4 files changed

+93
-9
lines changed

src/database/wrappers/account_links/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ impl UserMarker for RobloxMarker {
169169
}
170170

171171
/// Wrapper for account link user IDs.
172+
#[derive(Debug)]
172173
pub(crate) struct AccountLinkUserId<Marker: UserMarker> {
173174
/// Marker for the user type.
174175
marker: PhantomData<Marker>,

src/oauth/types/discord.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ use std::fmt::{Display, Formatter, Result as FmtResult};
88
///
99
/// The internal API should parse these scope sets into individual scopes
1010
/// that will be requested from the OAuth2 provider
11+
#[derive(PartialEq, Debug)]
1112
pub(crate) enum DiscordOAuthScopeSet {
1213
/// The scopes required for verifying a user's Discord account
1314
Verification,
1415
}
1516

1617
/// A set of scopes that will be requested from the OAuth2 provider.
18+
#[derive(PartialEq, Debug)]
1719
pub(crate) struct DiscordOAuthScopes(pub(crate) Vec<DiscordOAuthScope>);
1820

1921
/// A scope that can be requested from the OAuth2 provider.
2022
///
2123
/// [Reference](https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes)
24+
#[derive(PartialEq, Debug)]
2225
pub(crate) enum DiscordOAuthScope {
2326
Identify,
2427
Guilds,
@@ -316,3 +319,44 @@ impl<'a> DiscordRefreshTokenBody<'a> {
316319
])
317320
}
318321
}
322+
323+
#[cfg(test)]
324+
mod tests {
325+
use super::{DiscordOAuthScope, DiscordOAuthScopeSet, DiscordOAuthScopes};
326+
327+
#[test]
328+
fn test_discord_oauth_scopes_display() {
329+
let scopes = DiscordOAuthScopes(vec![
330+
DiscordOAuthScope::Identify,
331+
DiscordOAuthScope::GuildsMembersRead,
332+
DiscordOAuthScope::Guilds,
333+
DiscordOAuthScope::Connections,
334+
]);
335+
336+
assert_eq!(
337+
format!("{}", scopes),
338+
"identify+guilds.members.read+guilds+connections"
339+
);
340+
}
341+
342+
#[test]
343+
fn test_discord_oauth_scope_set_try_from() {
344+
assert_eq!(
345+
DiscordOAuthScopeSet::try_from("verification").unwrap(),
346+
DiscordOAuthScopeSet::Verification
347+
);
348+
}
349+
350+
#[test]
351+
fn test_discord_oauth_scopes_from() {
352+
assert_eq!(
353+
DiscordOAuthScopes::from(DiscordOAuthScopeSet::Verification),
354+
DiscordOAuthScopes(vec![
355+
DiscordOAuthScope::Identify,
356+
DiscordOAuthScope::GuildsMembersRead,
357+
DiscordOAuthScope::Guilds,
358+
DiscordOAuthScope::Connections,
359+
])
360+
);
361+
}
362+
}

src/oauth/types/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub(super) struct OAuthCallback {
1111
}
1212

1313
/// Result of the [`generate_session`](crate::oauth::utils::generate_session) function.
14+
#[derive(Debug)]
1415
pub(super) struct GeneratedSession<'a> {
1516
/// The session cookie.
1617
pub(super) cookie: Cookie<'a>,

src/oauth/types/roblox.rs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ use std::fmt::{Display, Formatter, Result as FmtResult};
77
///
88
/// The internal API should parse these scope sets into individual scopes
99
/// that will be requested from the OAuth2 provider
10+
#[derive(PartialEq, Debug)]
1011
pub(crate) enum RobloxOAuthScopeSet {
1112
Verification,
1213
}
1314

1415
/// A set of scopes that will be requested from the OAuth2 provider.
16+
#[derive(PartialEq, Debug)]
1517
pub(crate) struct RobloxOAuthScopes(pub(crate) Vec<RobloxOAuthScope>);
1618

1719
/// A scope that will be requested from the OAuth2 provider.
20+
#[derive(PartialEq, Debug)]
1821
pub(crate) enum RobloxOAuthScope {
1922
OpenID,
2023
Profile,
@@ -66,7 +69,7 @@ pub(crate) struct RobloxAuthorizedCodeRequestBody<'a> {
6669
}
6770

6871
/// The response body for the Roblox OAuth token endpoint.
69-
#[derive(Debug, Deserialize)]
72+
#[derive(Deserialize)]
7073
#[serde(crate = "rocket::serde")]
7174
pub(crate) struct RobloxAccessTokenResponse {
7275
/// The access token for the user.
@@ -176,14 +179,6 @@ impl TryFrom<&str> for RobloxOAuthScopeSet {
176179
}
177180
}
178181

179-
// impl From<&RobloxOAuthScopeSet> for String {
180-
// fn from(value: &RobloxOAuthScopeSet) -> Self {
181-
// let scopes: RobloxOAuthScopes = value.into();
182-
//
183-
// scopes.to_string()
184-
// }
185-
// }
186-
187182
impl<'a> RobloxAuthorizedCodeRequestBody<'a> {
188183
/// Creates a new [`RobloxAuthorizedCodeRequestBody`] with the given code, code verifier, and configuration.
189184
pub(crate) fn new(code: &'a str, code_verifier: &'a str, cfg: &'a Config) -> Self {
@@ -208,3 +203,46 @@ impl<'a> RobloxAuthorizedCodeRequestBody<'a> {
208203
])
209204
}
210205
}
206+
207+
#[cfg(test)]
208+
mod tests {
209+
use super::{RobloxOAuthScope, RobloxOAuthScopeSet, RobloxOAuthScopes};
210+
211+
#[test]
212+
fn test_roblox_oauth_scopes_display() {
213+
let scopes = RobloxOAuthScopes(vec![
214+
RobloxOAuthScope::OpenID,
215+
RobloxOAuthScope::Profile,
216+
RobloxOAuthScope::GroupRead,
217+
RobloxOAuthScope::UserInventoryItemRead,
218+
RobloxOAuthScope::UserAdvancedRead,
219+
]);
220+
221+
assert_eq!(
222+
format!("{}", scopes),
223+
"openid%20profile%20group:read%20user.inventory-item:read%20user.advanced:read"
224+
);
225+
}
226+
227+
#[test]
228+
fn test_roblox_oauth_scope_set_try_from() {
229+
assert_eq!(
230+
RobloxOAuthScopeSet::try_from("verification").unwrap(),
231+
RobloxOAuthScopeSet::Verification
232+
);
233+
}
234+
235+
#[test]
236+
fn test_roblox_oauth_scopes_from() {
237+
assert_eq!(
238+
RobloxOAuthScopes::from(RobloxOAuthScopeSet::Verification),
239+
RobloxOAuthScopes(vec![
240+
RobloxOAuthScope::OpenID,
241+
RobloxOAuthScope::Profile,
242+
RobloxOAuthScope::GroupRead,
243+
RobloxOAuthScope::UserInventoryItemRead,
244+
RobloxOAuthScope::UserAdvancedRead,
245+
])
246+
);
247+
}
248+
}

0 commit comments

Comments
 (0)