-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathdelete_account.rs
140 lines (129 loc) · 4.18 KB
/
delete_account.rs
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
use crate::{
constants::PROFILE_STORAGE_KEY,
models::ctx::Ctx,
runtime::{
msg::{Action, ActionCtx},
Env, EnvFutureExt, Runtime, RuntimeAction, TryEnvFuture,
},
types::{
api::{APIResult, SuccessResponse},
events::DismissedEventsBucket,
library::LibraryBucket,
notifications::NotificationsBucket,
profile::{Auth, AuthKey, GDPRConsent, Password, Profile, User},
search_history::SearchHistoryBucket,
server_urls::ServerUrlsBucket,
streams::StreamsBucket,
True,
},
unit_tests::{default_fetch_handler, Request, TestEnv, FETCH_HANDLER, REQUESTS, STORAGE},
};
use futures::future;
use std::any::Any;
use stremio_derive::Model;
#[test]
fn actionctx_delete_account() {
#[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/deleteUser"
&& method == "POST"
&& body == "{\"type\":\"DeleteAccount\",\"authKey\":\"auth_key\",\"password\":\"password\"}" =>
{
future::ok(
Box::new(APIResult::Ok(SuccessResponse { success: True {} }))
as Box<dyn Any + Send>,
)
.boxed_env()
}
_ => default_fetch_handler(request),
}
}
let profile = Profile {
auth: Some(Auth {
key: AuthKey("auth_key".to_owned()),
user: 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(),
trakt: None,
premium_expire: None,
gdpr_consent: GDPRConsent {
tos: true,
privacy: true,
marketing: true,
from: Some("tests".to_owned()),
},
},
}),
..Default::default()
};
let _env_mutex = TestEnv::reset().expect("Should have exclusive lock to TestEnv");
*FETCH_HANDLER.write().unwrap() = Box::new(fetch_handler);
STORAGE.write().unwrap().insert(
PROFILE_STORAGE_KEY.to_owned(),
serde_json::to_string(&profile).unwrap(),
);
let (runtime, _rx) = Runtime::<TestEnv, _>::new(
TestModel {
ctx: Ctx::new(
profile,
LibraryBucket::default(),
StreamsBucket::default(),
ServerUrlsBucket::new::<TestEnv>(None),
NotificationsBucket::new::<TestEnv>(None, vec![]),
SearchHistoryBucket::default(),
DismissedEventsBucket::default(),
),
},
vec![],
1000,
);
TestEnv::run(|| {
runtime.dispatch(RuntimeAction {
field: None,
action: Action::Ctx(ActionCtx::DeleteAccount(Password("password".to_owned()))),
})
});
assert_eq!(
runtime.model().unwrap().ctx.profile,
Default::default(),
"profile updated successfully in memory"
);
assert!(
STORAGE
.read()
.unwrap()
.get(PROFILE_STORAGE_KEY)
.map_or(false, |data| {
serde_json::from_str::<Profile>(data).unwrap() == Default::default()
}),
"profile updated successfully in storage"
);
assert_eq!(
REQUESTS.read().unwrap().len(),
1,
"One request have been sent"
);
assert_eq!(
REQUESTS.read().unwrap().get(0).unwrap().to_owned(),
Request {
url: "https://api.strem.io/api/deleteUser".to_owned(),
method: "POST".to_owned(),
body: "{\"type\":\"DeleteAccount\",\"authKey\":\"auth_key\",\"password\":\"password\"}"
.to_owned(),
..Default::default()
},
"Delete account request has been sent"
);
}