-
Notifications
You must be signed in to change notification settings - Fork 3
Add Login and URL Tests #46
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
native/swift/Sources/wordpress-api/Extensions/OAuthResponseUrl.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Foundation | ||
import wordpress_api_wrapper | ||
|
||
public extension URL { | ||
func asOAuthResponseUrl() -> OAuthResponseUrl { | ||
OAuthResponseUrl(stringValue: self.absoluteString) | ||
} | ||
} | ||
|
||
extension OAuthResponseUrl { | ||
static func new(stringValue: String) -> OAuthResponseUrl { | ||
OAuthResponseUrl(stringValue: stringValue) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
native/swift/Sources/wordpress-api/Extensions/WpRestApiurl.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Foundation | ||
import wordpress_api_wrapper | ||
|
||
enum WPRestAPIError: Error { | ||
case invalidUrl | ||
} | ||
|
||
public extension WpRestApiurl { | ||
func asUrl() throws -> URL { | ||
guard | ||
let url = URL(string: stringValue), | ||
url.scheme != nil, | ||
url.host != nil | ||
else { | ||
throw WPRestAPIError.invalidUrl | ||
} | ||
|
||
return url | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
native/swift/Tests/wordpress-api/Extensions/WPRestAPIUrlTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import XCTest | ||
import wordpress_api | ||
import wordpress_api_wrapper // We need to construct internal types to test them properly | ||
|
||
final class WPRestAPIUrlTests: XCTestCase { | ||
|
||
func testThatValidUrlCanBeParsed() throws { | ||
XCTAssertEqual(URL(string: "http://example.com"), try WpRestApiurl(stringValue: "http://example.com").asUrl()) | ||
} | ||
|
||
func testThatInvalidUrlThrowsError() throws { | ||
XCTAssertThrowsError(try WpRestApiurl(stringValue: "invalid").asUrl()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,52 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
use std::str; | ||
use crate::url::*; | ||
|
||
// After a successful login, the system will receive an OAuth callback with the login details | ||
// embedded as query params. This function parses that URL and extracts the login details as an object. | ||
#[derive(Debug, PartialEq, uniffi::Object)] | ||
pub struct OAuthResponseUrl { | ||
string_value: String, | ||
} | ||
|
||
#[uniffi::export] | ||
pub fn extract_login_details_from_url( | ||
url: WPRestAPIURL, | ||
) -> Option<WPAPIApplicationPasswordDetails> { | ||
if let (Some(site_url), Some(user_login), Some(password)) = | ||
url.as_url() | ||
.query_pairs() | ||
.fold((None, None, None), |accum, (k, v)| { | ||
match k.to_string().as_str() { | ||
"site_url" => (Some(v.to_string()), accum.1, accum.2), | ||
"user_login" => (accum.0, Some(v.to_string()), accum.2), | ||
"password" => (accum.0, accum.1, Some(v.to_string())), | ||
_ => accum, | ||
impl OAuthResponseUrl { | ||
#[uniffi::constructor] | ||
pub fn new(string_value: String) -> Self { | ||
Self { string_value } | ||
} | ||
|
||
pub fn get_password_details( | ||
&self, | ||
) -> Result<WPAPIApplicationPasswordDetails, OAuthResponseUrlError> { | ||
let mut builder = WPAPIApplicationPasswordDetails::builder(); | ||
|
||
let url = | ||
url::Url::parse(&self.string_value).map_err(|err| OAuthResponseUrlError::InvalidUrl)?; | ||
|
||
for pair in url.query_pairs() { | ||
match pair.0.to_string().as_str() { | ||
"site_url" => builder = builder.site_url(pair.1.to_string()), | ||
"user_login" => builder = builder.user_login(pair.1.to_string()), | ||
"password" => builder = builder.password(pair.1.to_string()), | ||
"success" => { | ||
if pair.1 == "false" { | ||
return Err(OAuthResponseUrlError::UnsuccessfulLogin); | ||
} | ||
} | ||
}) | ||
{ | ||
Some(WPAPIApplicationPasswordDetails { | ||
site_url, | ||
user_login, | ||
password, | ||
}) | ||
} else { | ||
None | ||
_ => (), | ||
}; | ||
} | ||
|
||
builder.build() //.map_err(|err| UrlParsingError::InvalidUrl) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, the previous implementation using fold is cleaner. The |
||
} | ||
} | ||
|
||
impl From<&str> for OAuthResponseUrl { | ||
fn from(str: &str) -> Self { | ||
OAuthResponseUrl { | ||
string_value: str.to_string(), | ||
} | ||
} | ||
} | ||
|
||
|
@@ -54,9 +73,173 @@ pub struct WPRestApiAuthenticationEndpoint { | |
pub authorization: String, | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, uniffi::Record)] | ||
#[derive(Debug, PartialEq, Serialize, Deserialize, uniffi::Record)] | ||
pub struct WPAPIApplicationPasswordDetails { | ||
pub site_url: String, | ||
pub user_login: String, | ||
pub password: String, | ||
} | ||
|
||
impl WPAPIApplicationPasswordDetails { | ||
fn builder() -> WPAPIApplicationPasswordDetailsBuilder { | ||
WPAPIApplicationPasswordDetailsBuilder::default() | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
struct WPAPIApplicationPasswordDetailsBuilder { | ||
site_url: Option<String>, | ||
user_login: Option<String>, | ||
password: Option<String>, | ||
} | ||
|
||
impl WPAPIApplicationPasswordDetailsBuilder { | ||
fn site_url(mut self, site_url: String) -> Self { | ||
self.site_url = Some(site_url); | ||
self | ||
} | ||
|
||
fn user_login(mut self, user_login: String) -> Self { | ||
self.user_login = Some(user_login); | ||
self | ||
} | ||
|
||
fn password(mut self, password: String) -> Self { | ||
self.password = Some(password); | ||
self | ||
} | ||
|
||
fn build(self) -> Result<WPAPIApplicationPasswordDetails, OAuthResponseUrlError> { | ||
let site_url = if let Some(site_url) = self.site_url { | ||
site_url | ||
} else { | ||
return Err(OAuthResponseUrlError::MissingSiteUrl); | ||
}; | ||
|
||
let user_login = if let Some(user_login) = self.user_login { | ||
user_login | ||
} else { | ||
return Err(OAuthResponseUrlError::MissingUsername); | ||
}; | ||
|
||
let password = if let Some(password) = self.password { | ||
password | ||
} else { | ||
return Err(OAuthResponseUrlError::MissingPassword); | ||
}; | ||
|
||
Ok(WPAPIApplicationPasswordDetails { | ||
site_url, | ||
user_login, | ||
password, | ||
}) | ||
} | ||
} | ||
|
||
#[derive(Debug, thiserror::Error, uniffi::Error)] | ||
pub enum OAuthResponseUrlError { | ||
#[error("Invalid URL")] | ||
InvalidUrl, | ||
|
||
#[error("The given URL is missing the `site_url` query parameter")] | ||
MissingSiteUrl, | ||
|
||
#[error("The given URL is missing the `username` query parameter")] | ||
MissingUsername, | ||
|
||
#[error("The given URL is missing the `password` query parameter")] | ||
MissingPassword, | ||
|
||
#[error("Unsuccessful Login")] | ||
UnsuccessfulLogin, | ||
} | ||
|
||
#[cfg(test)] | ||
mod oauth_response_url_tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn can_be_initialized() { | ||
assert_eq!(OAuthResponseUrl::new("foo".to_string()), OAuthResponseUrl::from("foo")) | ||
} | ||
|
||
#[test] | ||
fn creates_password_details_for_valid_url() { | ||
let url = OAuthResponseUrl::from( | ||
"exampleauth://login?site_url=http://example.com&user_login=test&password=1234", | ||
); | ||
|
||
assert_eq!( | ||
url.get_password_details().unwrap(), | ||
default_password_details() | ||
); | ||
} | ||
|
||
#[test] | ||
fn ignores_extra_query_params_for_valid_url() { | ||
let url = OAuthResponseUrl::from( | ||
"exampleauth://login?site_url=http://example.com&user_login=test&password=1234&foo=bar", | ||
); | ||
|
||
assert_eq!( | ||
url.get_password_details().unwrap(), | ||
default_password_details() | ||
); | ||
} | ||
|
||
#[test] | ||
fn throws_error_for_missing_site_url() { | ||
let result = OAuthResponseUrl::from("exampleauth://login?user_login=test&password=1234") | ||
.get_password_details(); | ||
assert!(matches!(result, Err(OAuthResponseUrlError::MissingSiteUrl))); | ||
} | ||
|
||
#[test] | ||
fn throws_error_for_missing_user_login() { | ||
let result = | ||
OAuthResponseUrl::from("exampleauth://login?site_url=http://example.com&password=1234") | ||
.get_password_details(); | ||
assert!(matches!( | ||
result, | ||
Err(OAuthResponseUrlError::MissingUsername) | ||
)); | ||
} | ||
|
||
#[test] | ||
fn throws_error_for_missing_password() { | ||
let result = OAuthResponseUrl::from( | ||
"exampleauth://login?site_url=http://example.com&user_login=test", | ||
) | ||
.get_password_details(); | ||
assert!(matches!( | ||
result, | ||
Err(OAuthResponseUrlError::MissingPassword) | ||
)); | ||
} | ||
|
||
#[test] | ||
fn throws_error_for_unsuccessful_login() { | ||
let result = | ||
OAuthResponseUrl::from("exampleauth://login?success=false").get_password_details(); | ||
assert!(matches!( | ||
result, | ||
Err(OAuthResponseUrlError::UnsuccessfulLogin) | ||
)); | ||
} | ||
|
||
#[test] | ||
fn throws_appropriate_error_for_malformed_response() { | ||
let result = | ||
OAuthResponseUrl::from("exampleauth://login?success=true").get_password_details(); | ||
assert!(matches!(result, Err(OAuthResponseUrlError::MissingSiteUrl))); | ||
} | ||
|
||
fn default_password_details() -> WPAPIApplicationPasswordDetails { | ||
WPAPIApplicationPasswordDetails::builder() | ||
.site_url("http://example.com".to_string()) | ||
.user_login("test".to_string()) | ||
.password("1234".to_string()) | ||
.build() | ||
.unwrap() | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't find any caller to this function. Can it be deleted?