Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Auth): impl apple token auth #788

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/types/api/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ impl FetchRequestParams<APIRequest> for APIRequest {
APIRequest::Auth(AuthRequest::Login { .. }) => "login".to_owned(),
APIRequest::Auth(AuthRequest::LoginWithToken { .. }) => "loginWithToken".to_owned(),
APIRequest::Auth(AuthRequest::Facebook { .. }) => "authWithFacebook".to_owned(),
APIRequest::Auth(AuthRequest::Apple { .. }) => "authWithApple".to_owned(),
APIRequest::Auth(AuthRequest::Register { .. }) => "register".to_owned(),
APIRequest::Logout { .. } => "logout".to_owned(),
APIRequest::DeleteAccount { .. } => "deleteUser".to_owned(),
Expand Down Expand Up @@ -192,6 +193,8 @@ pub enum AuthRequest {
password: String,
#[serde(default)]
facebook: bool,
#[serde(default)]
apple: bool,
},
Register {
email: String,
Expand All @@ -201,6 +204,9 @@ pub enum AuthRequest {
Facebook {
token: String,
},
Apple {
token: String,
},
LoginWithToken {
token: String,
},
Expand All @@ -213,11 +219,13 @@ impl fmt::Debug for AuthRequest {
email,
password: _,
facebook,
apple,
} => f
.debug_struct("Login")
.field("email", email)
.field("password", &"<SENSITIVE>")
.field("facebook", facebook)
.field("apple", apple)
.finish(),
Self::Register {
email,
Expand All @@ -233,6 +241,10 @@ impl fmt::Debug for AuthRequest {
.debug_struct("Facebook")
.field("token", &"<SENSITIVE>")
.finish(),
Self::Apple { token: _ } => f
.debug_struct("Apple")
.field("token", &"<SENSITIVE>")
.finish(),
Self::LoginWithToken { token: _ } => f
.debug_struct("LoginWithToken")
.field("token", &"<SENSITIVE>")
Expand Down
3 changes: 3 additions & 0 deletions src/types/profile/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ pub struct User {
pub fb_id: Option<String>,
#[serde(default)]
#[serde_as(deserialize_as = "DefaultOnNull<NoneAsEmptyString>")]
pub apple_id: Option<String>,
#[serde(default)]
#[serde_as(deserialize_as = "DefaultOnNull<NoneAsEmptyString>")]
pub avatar: Option<String>,
#[cfg_attr(test, derivative(Default(value = "Utc.timestamp_opt(0, 0).unwrap()")))]
pub last_modified: DateTime<Utc>,
Expand Down
1 change: 1 addition & 0 deletions src/unit_tests/ctx/add_to_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ fn actionctx_addtolibrary() {
id: "user_id".to_owned(),
email: "user_email".to_owned(),
fb_id: None,
apple_id: None,
avatar: None,
last_modified: TestEnv::now(),
date_registered: TestEnv::now(),
Expand Down
146 changes: 144 additions & 2 deletions src/unit_tests/ctx/authenticate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ fn user_fixture() -> User {
id: "user_id".to_owned(),
email: "user_email".to_owned(),
fb_id: None,
apple_id: None,
avatar: None,
last_modified: TestEnv::now(),
date_registered: TestEnv::now(),
Expand Down Expand Up @@ -72,7 +73,7 @@ fn actionctx_authenticate_login() {
url, method, body, ..
} if url == "https://api.strem.io/api/login"
&& method == "POST"
&& body == "{\"type\":\"Auth\",\"type\":\"Login\",\"email\":\"user_email\",\"password\":\"user_password\",\"facebook\":false}" =>
&& body == "{\"type\":\"Auth\",\"type\":\"Login\",\"email\":\"user_email\",\"password\":\"user_password\",\"facebook\":false,\"apple\":false}" =>
{
future::ok(Box::new(APIResult::Ok(auth_response_fixture())) as Box<dyn Any + Send>).boxed_env()
}
Expand Down Expand Up @@ -121,6 +122,7 @@ fn actionctx_authenticate_login() {
email: "user_email".into(),
password: "user_password".into(),
facebook: false,
apple: false,
})),
})
});
Expand Down Expand Up @@ -173,7 +175,7 @@ fn actionctx_authenticate_login() {
Request {
url: "https://api.strem.io/api/login".to_owned(),
method: "POST".to_owned(),
body: "{\"type\":\"Auth\",\"type\":\"Login\",\"email\":\"user_email\",\"password\":\"user_password\",\"facebook\":false}".to_owned(),
body: "{\"type\":\"Auth\",\"type\":\"Login\",\"email\":\"user_email\",\"password\":\"user_password\",\"facebook\":false,\"apple\":false}".to_owned(),
..Default::default()
},
"Login request has been sent"
Expand Down Expand Up @@ -484,6 +486,146 @@ fn actionctx_authenticate_facebook() {
);
}

#[test]
fn actionctx_authenticate_apple() {
#[derive(Model, Clone, Default)]
#[model(TestEnv)]
struct TestModel {
ctx: Ctx,
}
fn fetch_handler(request: Request) -> TryEnvFuture<Box<dyn Any + Send>> {
match request {
Request {
url, method, body, ..
} if url == "https://api.strem.io/api/authWithApple"
&& method == "POST"
&& body == "{\"type\":\"Auth\",\"type\":\"Apple\",\"token\":\"access_token\"}" =>
{
future::ok(Box::new(APIResult::Ok(auth_response_fixture())) as Box<dyn Any + Send>).boxed_env()
}
Request {
url, method, body, ..
} if url == "https://api.strem.io/api/addonCollectionGet"
&& method == "POST"
&& body == "{\"type\":\"AddonCollectionGet\",\"authKey\":\"auth_key\",\"update\":true}" =>
{
future::ok(Box::new(APIResult::Ok(
CollectionResponse {
addons: vec![],
last_modified: TestEnv::now(),
},)
) as Box<dyn Any + Send>).boxed_env()
}
Request {
url, method, body, ..
} if url == "https://api.strem.io/api/datastoreGet"
&& method == "POST"
&& body == "{\"authKey\":\"auth_key\",\"collection\":\"libraryItem\",\"ids\":[],\"all\":true}" =>
{
future::ok(Box::new(APIResult::Ok(LibraryItemsResponse::new(),)) as Box<dyn Any + Send>).boxed_env()
}
_ => default_fetch_handler(request),
}
}
let _env_mutex = TestEnv::reset().expect("Should have exclusive lock to TestEnv");
*FETCH_HANDLER.write().unwrap() = Box::new(fetch_handler);
let ctx = Ctx::new(
Profile::default(),
LibraryBucket::default(),
StreamsBucket::default(),
ServerUrlsBucket::new::<TestEnv>(None),
NotificationsBucket::new::<TestEnv>(None, vec![]),
SearchHistoryBucket::default(),
DismissedEventsBucket::default(),
);
let (runtime, _rx) = Runtime::<TestEnv, _>::new(TestModel { ctx }, vec![], 1000);
TestEnv::run(|| {
runtime.dispatch(RuntimeAction {
field: None,
action: Action::Ctx(ActionCtx::Authenticate(AuthRequest::Apple {
token: "access_token".into(),
})),
})
});
assert_eq!(
runtime.model().unwrap().ctx.profile,
profile_fixture(),
"profile updated successfully in memory"
);
assert_eq!(
runtime.model().unwrap().ctx.library,
LibraryBucket {
uid: Some("user_id".to_string()),
..Default::default()
},
"library updated successfully in memory"
);
assert_eq!(
serde_json::from_str::<Profile>(STORAGE.read().unwrap().get(PROFILE_STORAGE_KEY).unwrap())
.unwrap(),
profile_fixture(),
"profile updated successfully in storage"
);
assert_eq!(
serde_json::from_str::<LibraryBucket>(
STORAGE
.read()
.unwrap()
.get(LIBRARY_RECENT_STORAGE_KEY)
.unwrap()
)
.unwrap(),
LibraryBucket::new(Some("user_id".to_owned()), vec![]),
"recent library updated successfully in storage"
);
assert_eq!(
serde_json::from_str::<LibraryBucket>(
STORAGE.read().unwrap().get(LIBRARY_STORAGE_KEY).unwrap()
)
.unwrap(),
LibraryBucket::new(Some("user_id".to_owned()), vec![]),
"library updated successfully in storage"
);
assert_eq!(
REQUESTS.read().unwrap().len(),
3,
"Three requests have been sent"
);
assert_eq!(
REQUESTS.read().unwrap().first().unwrap().to_owned(),
Request {
url: "https://api.strem.io/api/authWithApple".to_owned(),
method: "POST".to_owned(),
body: "{\"type\":\"Auth\",\"type\":\"Apple\",\"token\":\"access_token\"}".to_owned(),
..Default::default()
},
"Login request has been sent"
);
assert_eq!(
REQUESTS.read().unwrap().get(1).unwrap().to_owned(),
Request {
url: "https://api.strem.io/api/addonCollectionGet".to_owned(),
method: "POST".to_owned(),
body: "{\"type\":\"AddonCollectionGet\",\"authKey\":\"auth_key\",\"update\":true}"
.to_owned(),
..Default::default()
},
"AddonCollectionGet request has been sent"
);
assert_eq!(
REQUESTS.read().unwrap().get(2).unwrap().to_owned(),
Request {
url: "https://api.strem.io/api/datastoreGet".to_owned(),
method: "POST".to_owned(),
body:
"{\"authKey\":\"auth_key\",\"collection\":\"libraryItem\",\"ids\":[],\"all\":true}"
.to_owned(),
..Default::default()
},
"DatastoreGet request has been sent"
);
}

#[test]
fn actionctx_authenticate_register() {
#[derive(Model, Clone, Default)]
Expand Down
1 change: 1 addition & 0 deletions src/unit_tests/ctx/delete_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn actionctx_delete_account() {
id: "user_id".to_owned(),
email: "user_email".to_owned(),
fb_id: None,
apple_id: None,
avatar: None,
last_modified: TestEnv::now(),
date_registered: TestEnv::now(),
Expand Down
1 change: 1 addition & 0 deletions src/unit_tests/ctx/install_addon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ fn actionctx_installaddon_install_with_user() {
id: "user_id".to_owned(),
email: "user_email".to_owned(),
fb_id: None,
apple_id: None,
avatar: None,
last_modified: TestEnv::now(),
date_registered: TestEnv::now(),
Expand Down
1 change: 1 addition & 0 deletions src/unit_tests/ctx/logout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn actionctx_logout() {
id: "user_id".to_owned(),
email: "user_email".to_owned(),
fb_id: None,
apple_id: None,
avatar: None,
last_modified: TestEnv::now(),
date_registered: TestEnv::now(),
Expand Down
1 change: 1 addition & 0 deletions src/unit_tests/ctx/pull_addons_from_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ fn actionctx_pulladdonsfromapi_with_user() {
id: "user_id".to_owned(),
email: "user_email".to_owned(),
fb_id: None,
apple_id: None,
avatar: None,
last_modified: TestEnv::now(),
date_registered: TestEnv::now(),
Expand Down
1 change: 1 addition & 0 deletions src/unit_tests/ctx/push_addons_to_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ fn actionctx_pushaddonstoapi_with_user() {
id: "user_id".to_owned(),
email: "user_email".to_owned(),
fb_id: None,
apple_id: None,
avatar: None,
last_modified: TestEnv::now(),
date_registered: TestEnv::now(),
Expand Down
1 change: 1 addition & 0 deletions src/unit_tests/ctx/remove_from_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fn actionctx_removefromlibrary() {
id: "user_id".to_owned(),
email: "user_email".to_owned(),
fb_id: None,
apple_id: None,
avatar: None,
last_modified: TestEnv::now(),
date_registered: TestEnv::now(),
Expand Down
1 change: 1 addition & 0 deletions src/unit_tests/ctx/rewind_library_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fn actionctx_rewindlibraryitem() {
id: "user_id".to_owned(),
email: "user_email".to_owned(),
fb_id: None,
apple_id: None,
avatar: None,
last_modified: TestEnv::now(),
date_registered: TestEnv::now(),
Expand Down
2 changes: 2 additions & 0 deletions src/unit_tests/ctx/sync_library_with_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ fn actionctx_synclibrarywithapi_with_user() {
id: "user_id".to_owned(),
email: "user_email".to_owned(),
fb_id: None,
apple_id: None,
avatar: None,
last_modified: TestEnv::now(),
date_registered: TestEnv::now(),
Expand Down Expand Up @@ -414,6 +415,7 @@ fn actionctx_synclibrarywithapi_with_user_empty_library() {
id: "user_id".to_owned(),
email: "user_email".to_owned(),
fb_id: None,
apple_id: None,
avatar: None,
last_modified: TestEnv::now(),
date_registered: TestEnv::now(),
Expand Down
1 change: 1 addition & 0 deletions src/unit_tests/ctx/uninstall_addon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ fn actionctx_uninstalladdon_with_user() {
id: "user_id".to_owned(),
email: "user_email".to_owned(),
fb_id: None,
apple_id: None,
avatar: None,
last_modified: TestEnv::now(),
date_registered: TestEnv::now(),
Expand Down
19 changes: 17 additions & 2 deletions src/unit_tests/serde/auth_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ fn auth_request() {
email: "email".to_owned(),
password: "password".to_owned(),
facebook: false,
apple: false,
},
AuthRequest::LoginWithToken {
token: "token".to_owned(),
},
AuthRequest::Facebook {
token: "token".to_owned(),
},
AuthRequest::Apple {
token: "token".to_owned(),
},
AuthRequest::Register {
email: "email".to_owned(),
password: "password".to_owned(),
Expand All @@ -26,10 +30,10 @@ fn auth_request() {
],
&[
vec![
Token::Seq { len: Some(4) },
Token::Seq { len: Some(5) },
Token::Struct {
name: "AuthRequest",
len: 4,
len: 5,
},
Token::Str("type"),
Token::Str("Login"),
Expand All @@ -39,6 +43,8 @@ fn auth_request() {
Token::Str("password"),
Token::Str("facebook"),
Token::Bool(false),
Token::Str("apple"),
Token::Bool(false),
Token::StructEnd,
Token::Struct {
name: "AuthRequest",
Expand All @@ -58,6 +64,15 @@ fn auth_request() {
Token::Str("token"),
Token::Str("token"),
Token::StructEnd,
Token::Struct {
name: "AuthRequest",
len: 2,
},
Token::Str("type"),
Token::Str("Apple"),
Token::Str("token"),
Token::Str("token"),
Token::StructEnd,
Token::Struct {
name: "AuthRequest",
len: 4,
Expand Down
Loading