Kibou is a federated social networking server.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

actor.rs 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. use actor;
  2. use base64;
  3. use database;
  4. use openssl::hash::MessageDigest;
  5. use openssl::pkey::PKey;
  6. use openssl::rsa::Rsa;
  7. use openssl::sign::Verifier;
  8. use pem::Pem;
  9. use serde_json;
  10. use tests::utils::create_local_test_actor;
  11. use tests::utils::create_remote_test_actor;
  12. use tests::utils::delete_test_actor;
  13. #[test]
  14. fn generate_new_keys()
  15. {
  16. let mut test_actor = create_local_test_actor("852f47fe-9271-4f96-ae81-610ae40ca164");
  17. let test_actor_pkey = test_actor.get_private_key();
  18. delete_test_actor(test_actor);
  19. match pem::parse(test_actor_pkey)
  20. {
  21. Ok(_) => assert!(true),
  22. Err(_) => assert!(false, "Invalid private key was generated")
  23. }
  24. }
  25. #[test]
  26. fn sign()
  27. {
  28. let mut test_actor = create_local_test_actor("92edfc1f-4f9d-40f3-83a0-e4deb5163c15");
  29. let test_string: String = String::from("Test");
  30. let test_actor_pkey = test_actor.get_private_key();
  31. let signed_string = test_actor.sign(test_string.clone());
  32. delete_test_actor(test_actor);
  33. let pem_decoded = pem::parse(test_actor_pkey).unwrap();
  34. let pkey = PKey::from_rsa(openssl::rsa::Rsa::private_key_from_der(&pem_decoded.contents).unwrap()).unwrap();
  35. let mut verifier = Verifier::new(MessageDigest::sha256(), &pkey).unwrap();
  36. verifier.update(&test_string.into_bytes()).unwrap();
  37. assert_eq!(verifier.verify(&base64::decode(&signed_string.into_bytes()).unwrap()).unwrap(), true);
  38. }
  39. #[test]
  40. fn update_local_keys()
  41. {
  42. let mut test_actor = create_local_test_actor("d0fd62bc-a991-42e5-9f39-758bafab06e6");
  43. let test_actor_pkey = test_actor.get_private_key();
  44. test_actor.update_local_keys();
  45. let test_actor_new_pkey = test_actor.get_private_key();
  46. delete_test_actor(test_actor);
  47. assert_ne!(test_actor_pkey, test_actor_new_pkey);
  48. }
  49. #[test]
  50. fn get_actor_by_acct()
  51. {
  52. let database = database::establish_connection();
  53. let mut test_actor = create_local_test_actor("6ca9fede-04e0-4175-b7af-29ff760b80ba");
  54. let test_actor_id = test_actor.id;
  55. let acct = test_actor.get_acct();
  56. match actor::get_actor_by_acct(&database, acct)
  57. {
  58. Ok(actor) =>
  59. {
  60. delete_test_actor(test_actor);
  61. assert_eq!(test_actor_id, actor.id);
  62. },
  63. Err(_) =>
  64. {
  65. delete_test_actor(test_actor);
  66. assert!(false, "Actor could not be queried by `acct`");
  67. }
  68. }
  69. }
  70. #[test]
  71. fn get_actor_by_uri()
  72. {
  73. let database = database::establish_connection();
  74. let mut test_actor = create_local_test_actor("11c116b1-818c-4c07-aac7-26debc450d0b");
  75. let test_actor_id = test_actor.id;
  76. match actor::get_actor_by_uri(&database, &test_actor.actor_uri)
  77. {
  78. Ok(actor) => {
  79. delete_test_actor(test_actor);
  80. assert_eq!(test_actor_id, actor.id);
  81. },
  82. Err(_) =>
  83. {
  84. delete_test_actor(test_actor);
  85. assert!(false, "Actor could not be queried by `uri`");
  86. }
  87. }
  88. }
  89. #[test]
  90. fn get_local_actor_by_preferred_username()
  91. {
  92. let database = database::establish_connection();
  93. let mut test_actor = create_local_test_actor("dd4f078d-d5d8-4f84-a99f-7c13841fa962");
  94. match actor::get_local_actor_by_preferred_username(&database, test_actor.preferred_username.clone())
  95. {
  96. Ok(_) => {
  97. delete_test_actor(test_actor);
  98. assert!(true);
  99. },
  100. Err(_) => {
  101. delete_test_actor(test_actor);
  102. assert!(false, "Local user was not found");
  103. }
  104. }
  105. }
  106. #[test]
  107. fn create_remote_actor()
  108. {
  109. let database = database::establish_connection();
  110. let mut test_actor = create_remote_test_actor("6c85897a-d420-4e23-b5c8-7d6eb4ea834f");
  111. let email = test_actor.email.clone();
  112. let password = test_actor.password.clone();
  113. let actor_uri = test_actor.actor_uri.clone();
  114. let username = test_actor.username.clone();
  115. let preferred_username = test_actor.preferred_username.clone();
  116. let summary = test_actor.summary.clone();
  117. let local = test_actor.local.clone();
  118. match actor::get_actor_by_uri(&database, &test_actor.actor_uri)
  119. {
  120. Ok(db_actor) =>
  121. {
  122. delete_test_actor(test_actor);
  123. assert_eq!(db_actor.email, email);
  124. assert_eq!(db_actor.password, password);
  125. assert_eq!(db_actor.actor_uri, actor_uri);
  126. assert_eq!(db_actor.username, username);
  127. assert_eq!(db_actor.preferred_username, preferred_username.clone());
  128. assert_eq!(db_actor.summary, summary);
  129. assert_eq!(db_actor.local, local);
  130. },
  131. Err(_) =>
  132. {
  133. delete_test_actor(test_actor);
  134. assert!(false, "Actor was not found in database");
  135. }
  136. }
  137. }
  138. #[test]
  139. fn delete_remote_actor()
  140. {
  141. let database = database::establish_connection();
  142. let mut test_actor = create_remote_test_actor("3e1218d3-f735-4a50-b610-52c6f64445ee");
  143. let actor_uri = test_actor.actor_uri.clone();
  144. delete_test_actor(test_actor);
  145. match actor::get_actor_by_uri(&database, &actor_uri)
  146. {
  147. Ok(_) => assert!(false, "Actor was not deleted!"),
  148. Err(_) => assert!(true)
  149. }
  150. }
  151. /// Creates an actor with all nullable values being filled with null
  152. ///
  153. /// # Note
  154. /// This is neither a valid local actor, nor a valid remote actor. It's purpose is to test
  155. /// the creation of actors with optional values in general.
  156. #[test]
  157. fn create_actor_with_optional_values()
  158. {
  159. let database = database::establish_connection();
  160. let test_actor_username = String::from("e40e4712-ad48-4b5b-9bd6-1459b399b85c");
  161. let mut test_actor = actor::Actor {
  162. id: 0,
  163. email: None,
  164. password: None,
  165. actor_uri: String::from("https://example.tld/e40e4712-ad48-4b5b-9bd6-1459b399b85c"),
  166. username: None,
  167. preferred_username: String::from("e40e4712-ad48-4b5b-9bd6-1459b399b85c"),
  168. summary: None,
  169. inbox: None,
  170. icon: None,
  171. keys: serde_json::json!({}),
  172. local: false
  173. };
  174. let email = test_actor.email.clone();
  175. let password = test_actor.password.clone();
  176. let actor_uri = test_actor.actor_uri.clone();
  177. let username = test_actor.username.clone();
  178. let preferred_username = test_actor.preferred_username.clone();
  179. let summary = test_actor.summary.clone();
  180. let inbox = test_actor.inbox.clone();
  181. let icon = test_actor.icon.clone();
  182. let keys = test_actor.keys.clone();
  183. let local = test_actor.local.clone();
  184. actor::create_actor(&database, &mut test_actor);
  185. match actor::get_actor_by_uri(&database, &actor_uri)
  186. {
  187. Ok(db_actor) =>
  188. {
  189. delete_test_actor(test_actor);
  190. assert_eq!(db_actor.email, email);
  191. assert_eq!(db_actor.password, password);
  192. assert_eq!(db_actor.actor_uri, actor_uri);
  193. assert_eq!(db_actor.username, username);
  194. assert_eq!(db_actor.preferred_username, preferred_username);
  195. assert_eq!(db_actor.summary, summary);
  196. assert_eq!(db_actor.inbox, inbox);
  197. assert_eq!(db_actor.icon, icon);
  198. assert_eq!(db_actor.keys, keys);
  199. assert_eq!(db_actor.local, local);
  200. },
  201. Err(_) =>
  202. {
  203. delete_test_actor(test_actor);
  204. assert!(false, "Actor was not found in database");
  205. }
  206. }
  207. }
  208. #[test]
  209. fn create_local_actor()
  210. {
  211. let database = database::establish_connection();
  212. let mut test_actor = create_local_test_actor("efdd9c02-2887-47a6-a2a7-97e8cb533a7c");
  213. let email = test_actor.email.clone();
  214. let password = test_actor.password.clone();
  215. let actor_uri = test_actor.actor_uri.clone();
  216. let username = test_actor.username.clone();
  217. let preferred_username = test_actor.preferred_username.clone();
  218. let summary = test_actor.summary.clone();
  219. let local = test_actor.local.clone();
  220. match actor::get_local_actor_by_preferred_username(&database, test_actor.preferred_username.clone())
  221. {
  222. Ok(db_actor) =>
  223. {
  224. delete_test_actor(test_actor);
  225. assert_eq!(db_actor.email, email);
  226. assert_eq!(db_actor.password, password);
  227. assert_eq!(db_actor.actor_uri, actor_uri);
  228. assert_eq!(db_actor.username, username);
  229. assert_eq!(db_actor.preferred_username, preferred_username.clone());
  230. assert_eq!(db_actor.summary, summary);
  231. assert_eq!(db_actor.local, local);
  232. },
  233. Err(_) =>
  234. {
  235. delete_test_actor(test_actor);
  236. assert!(false, "Actor was not found in database");
  237. }
  238. }
  239. }
  240. #[test]
  241. fn delete_local_actor()
  242. {
  243. let database = database::establish_connection();
  244. let test_actor: actor::Actor = create_local_test_actor("e649852f-61aa-4999-9865-165a35c2618d");
  245. let preferred_username = test_actor.preferred_username.clone();
  246. delete_test_actor(test_actor);
  247. match actor::get_local_actor_by_preferred_username(&database, preferred_username)
  248. {
  249. Ok(_) => assert!(false, "Actor was not deleted!"),
  250. Err(_) => assert!(true)
  251. }
  252. }