1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
use forgejo_api::structs::*;
mod common;
#[tokio::test]
async fn myself() {
let api = common::login();
let myself = api.user_get_current().await.unwrap();
assert!(myself.is_admin.unwrap(), "user should be admin");
assert_eq!(
myself.login.as_ref().unwrap(),
"TestingAdmin",
"user should be named \"TestingAdmin\""
);
let myself_indirect = api.user_get("TestingAdmin").await.unwrap();
assert_eq!(
myself, myself_indirect,
"result of `myself` does not match result of `get_user`"
);
}
#[tokio::test]
async fn myself_custom_type() {
let api = common::login();
#[derive(serde::Deserialize, PartialEq, Eq, Debug)]
struct CustomUser {
is_admin: bool,
login: String,
}
forgejo_api::impl_from_response!(CustomUser);
let myself = api
.user_get_current()
.response_type::<CustomUser>()
.await
.unwrap();
assert!(myself.is_admin, "user should be admin");
assert_eq!(
myself.login, "TestingAdmin",
"user should be named \"TestingAdmin\""
);
let myself_indirect = api
.user_get("TestingAdmin")
.response_type::<CustomUser>()
.await
.unwrap();
assert_eq!(
myself, myself_indirect,
"result of `myself` does not match result of `get_user`"
);
}
#[cfg(feature = "sync")]
#[test]
fn myself_sync() {
let api = common::sync_login();
let myself = api.user_get_current().send().unwrap();
assert!(myself.is_admin.unwrap(), "user should be admin");
assert_eq!(
myself.login.as_ref().unwrap(),
"TestingAdmin",
"user should be named \"TestingAdmin\""
);
let myself_indirect = api.user_get("TestingAdmin").send().unwrap();
assert_eq!(
myself, myself_indirect,
"result of `myself` does not match result of `get_user`"
);
}
#[tokio::test]
async fn follow() {
let api = common::login();
let (_, following) = api.user_list_following("TestingAdmin").await.unwrap();
assert!(following.is_empty(), "following list not empty");
let (_, followers) = api.user_list_followers("TestingAdmin").await.unwrap();
assert!(followers.is_empty(), "follower list not empty");
let option = CreateUserOption {
created_at: None,
email: "follower@no-reply.example.org".into(),
full_name: None,
login_name: None,
must_change_password: Some(false),
password: Some("password".into()),
restricted: None,
send_notify: None,
source_id: None,
username: "Follower".into(),
visibility: None,
};
let _ = api.admin_create_user(option).await.unwrap();
let new_user = common::login_pass("Follower", "password");
new_user
.user_current_put_follow("TestingAdmin")
.await
.unwrap();
api.user_current_put_follow("Follower").await.unwrap();
let (_, following) = api.user_list_following("TestingAdmin").await.unwrap();
assert!(!following.is_empty(), "following list empty");
let (_, followers) = api.user_list_followers("TestingAdmin").await.unwrap();
assert!(!followers.is_empty(), "follower list empty");
}
#[tokio::test]
async fn password_login() {
let api = common::login();
let password_api = common::login_pass("TestingAdmin", "password");
assert!(
api.user_get_current().await.unwrap() == password_api.user_get_current().await.unwrap(),
"users not equal comparing token-auth and pass-auth"
);
}
#[tokio::test]
async fn oauth2_login() {
let api = common::login();
let opt = forgejo_api::structs::CreateOAuth2ApplicationOptions {
confidential_client: Some(true),
name: Some("Test Application".into()),
redirect_uris: Some(vec!["http://127.0.0.1:48879/".into()]),
};
let app = api.user_create_oauth2_application(opt).await.unwrap();
let client_id = app.client_id.unwrap();
let client_secret = app.client_secret.unwrap();
let base_url = &std::env::var("FORGEJO_API_CI_INSTANCE_URL").unwrap();
let client = reqwest::Client::builder()
.cookie_store(true)
.redirect(reqwest::redirect::Policy::none())
.build()
.unwrap();
// Log in via the web interface
let _ = client
.post(&format!("{base_url}user/login"))
.form(&[("user_name", "TestingAdmin"), ("password", "password")])
.send()
.await
.unwrap()
.error_for_status()
.unwrap();
// Load the authorization page
let response = client
.get(&format!(
"{base_url}login/oauth/authorize\
?client_id={client_id}\
&redirect_uri=http%3A%2F%2F127.0.0.1%3A48879%2F\
&response_type=code\
&state=theyve"
))
.send()
.await
.unwrap()
.error_for_status()
.unwrap();
let csrf = response.cookies().find(|x| x.name() == "_csrf").unwrap();
// Authorize the new application via the web interface
let response = client
.post(&format!("{base_url}login/oauth/grant"))
.form(&[
("_csrf", csrf.value()),
("client_id", &client_id),
("state", "theyve"),
("scope", ""),
("nonce", ""),
("redirect_uri", "http://127.0.0.1:48879/"),
("granted", "true"),
])
.send()
.await
.unwrap()
.error_for_status()
.unwrap();
// Extract the code from the redirect url
let location = response.headers().get(reqwest::header::LOCATION).unwrap();
let location = url::Url::parse(dbg!(location.to_str().unwrap())).unwrap();
let mut code = None;
for (key, value) in location.query_pairs() {
if key == "code" {
code = Some(value.into_owned());
} else if key == "error_description" {
panic!("{value}");
}
}
let code = code.unwrap();
// Redeem the code and check it works
let url = url::Url::parse(base_url).unwrap();
let api = forgejo_api::Forgejo::new(forgejo_api::Auth::None, url.clone()).unwrap();
let request = forgejo_api::structs::OAuthTokenRequest::Confidential {
client_id: &client_id,
client_secret: &client_secret,
code: &code,
redirect_uri: url::Url::parse("http://127.0.0.1:48879/").unwrap(),
};
let token = api.oauth_get_access_token(request).await.unwrap();
let token_api =
forgejo_api::Forgejo::new(forgejo_api::Auth::OAuth2(&token.access_token), url.clone())
.unwrap();
let myself = token_api.user_get_current().await.unwrap();
assert_eq!(myself.login.as_deref(), Some("TestingAdmin"));
let request = forgejo_api::structs::OAuthTokenRequest::Refresh {
refresh_token: &token.refresh_token,
client_id: &client_id,
client_secret: &client_secret,
};
let token = token_api.oauth_get_access_token(request).await.unwrap();
let token_api =
forgejo_api::Forgejo::new(forgejo_api::Auth::OAuth2(&token.access_token), url).unwrap();
let myself = token_api.user_get_current().await.unwrap();
assert_eq!(myself.login.as_deref(), Some("TestingAdmin"));
}
#[tokio::test]
async fn user_vars() {
let api = common::login();
let (_, var_list) = api
.get_user_variables_list()
.await
.expect("failed to list user vars");
assert!(var_list.is_empty());
let opt = CreateVariableOption {
value: "false".into(),
};
api.create_user_variable("likes_dogs", opt)
.await
.expect("failed to create user var");
let new_var = api
.get_user_variable("likes_dogs")
.await
.expect("failed to get user var");
assert_eq!(new_var.data.as_deref(), Some("false"));
// what??? totally wrong. I love dogs!
let opt = UpdateVariableOption {
name: Some("loves_dogs".into()),
value: "true".into(),
};
api.update_user_variable("likes_dogs", opt)
.await
.expect("failed to update user variable");
let new_var = api
.get_user_variable("loves_dogs")
.await
.expect("failed to get user var");
assert_eq!(new_var.data.as_deref(), Some("true"));
api.delete_user_variable("loves_dogs")
.await
.expect("failed to delete user var");
}
|