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
276
277
278
279
280
281
282
283
284
285
286
//! A partially implemented wrapper for the Reddit API, as declared here: https://www.reddit.com/dev/api

pub mod api;
pub mod pants;

#[cfg(test)]
mod tests {
    use std::env;

    use crate::api::generated::request::links_and_comments;
    use crate::pants::Pants;

    const USER_AGENT: &str = "Microsoft Windows 10 Home:ca.technicallyrural.testapp:0.0.1 (by /u/ample_bird)";
    const SUBREDDIT: &str = "testingground4bots";

    #[allow(dead_code)]
    fn setup_logger() -> Result<(), fern::InitError> {
        fern::Dispatch::new()
            .format(|out, message, record| {
                out.finish(format_args!(
                    "{}[{}][{}] {}",
                    chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
                    record.target(),
                    record.level(),
                    message
                ))
            })
            .level(log::LevelFilter::Info)
            .level_for("mr_splashy_pants", log::LevelFilter::Trace)
            .chain(std::io::stdout())
            .chain(fern::log_file("output.log")?)
            .apply()?;
        Ok(())
    }

    fn build_pants() -> Pants {
        dotenv::dotenv().ok();

        Pants::new(
            USER_AGENT,
            env::var("ACCESS_TOKEN").unwrap(),
            &env::var("REFRESH_TOKEN").unwrap(),
            &env::var("CLIENT_ID").unwrap(),
            &env::var("CLIENT_SECRET").unwrap(),
        )
    }

    // Accounts
    #[test]
    fn me() {
        let pants = build_pants();

        match tokio_test::block_on(pants.me()) {
            Ok(response) => println!("Successfully got response on first invocation: {:#?}", response),
            Err(e) => panic!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn me_karma() {
        let pants = build_pants();

        match tokio_test::block_on(pants.me_karma()) {
            Ok(response) => println!("Response to me_karma is: {:#?}", response),
            Err(e) => panic!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn me_prefs() {
        let pants = build_pants();

        match tokio_test::block_on(pants.me_prefs()) {
            Ok(response) => println!("Response to me_prefs is: {:#?}", response),
            Err(e) => panic!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn me_trophies() {
        let pants = build_pants();

        match tokio_test::block_on(pants.me_trophies()) {
            Ok(response) => println!("Response to me_trophies is: {:#?}", response),
            Err(e) => panic!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn prefs_friends() {
        let pants = build_pants();

        match tokio_test::block_on(pants.prefs_friends()) {
            Ok(response) => println!("Response to prefs_friends is: {:#?}", response),
            Err(e) => panic!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn prefs_blocked() {
        let pants = build_pants();

        match tokio_test::block_on(pants.prefs_blocked()) {
            Ok(response) => println!("Response to prefs_blocked is: {:#?}", response),
            Err(e) => panic!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn prefs_messaging() {
        let pants = build_pants();

        match tokio_test::block_on(pants.prefs_messaging()) {
            Ok(response) => println!("Response to prefs_messaging is: {:#?}", response),
            Err(e) => panic!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn prefs_trusted() {
        let pants = build_pants();

        match tokio_test::block_on(pants.prefs_trusted()) {
            Ok(response) => println!("Response to prefs_trusted is: {:#?}", response),
            Err(e) => panic!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn me_friends() {
        let pants = build_pants();

        match tokio_test::block_on(pants.me_friends()) {
            Ok(response) => println!("Response to me_friends is: {:#?}", response),
            Err(e) => panic!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn me_blocked() {
        let pants = build_pants();

        match tokio_test::block_on(pants.me_blocked()) {
            Ok(response) => println!("Response to me_blocked is: {:#?}", response),
            Err(e) => panic!("An error ocurred: {}", e),
        };
    }

    // Listings

    #[test]
    fn trending_subreddits() {
        dotenv::dotenv().ok();

        let pants = build_pants();

        match tokio_test::block_on(pants.trending_subreddits()) {
            Ok(response) => println!("Response to trending_subreddits is: {:#?}", response),
            Err(e) => panic!("An error ocurred: {}", e),
        };
    }

    // fn by_id_names() {
    //     let mut pants = build_pants();
    //     // TODO: Figure this out
    //     match tokio_test::block_on(pants.by_id_names(fullnames: Vec<String>)) {
    //         Ok(response) => println!("Response to best is: {:#?}", response),
    //         Err(e) => panic!("An error ocurred: {}", e),
    //     };
    // }

    // fn comments_article() {
    //     let mut pants = build_pants();
    //     // TODO: Figure this out
    //     match tokio_test::block_on(pants.get_comments_article(article: String)() {
    //         Ok(response) => println!("Response to best is: {:#?}", response),
    //         Err(e) => panic!("An error ocurred: {}", e),
    //     };
    // }

    // fn duplicates_article() {
    //     let mut pants = build_pants();
    //     // TODO: Figure this out
    //     match tokio_test::block_on(pants.duplicates_article(article: String)) {
    //         Ok(response) => println!("Response to best is: {:#?}", response),
    //         Err(e) => panic!("An error ocurred: {}", e),
    //     };
    // }

    #[test]
    fn submit() {
        let pants = build_pants();

        let request_body = links_and_comments::ApiSubmit {
            url: "".to_string(),
            video_poster_url: "".to_string(),
            sendreplies: "".to_string(),
            collection_id: "".to_string(),
            resubmit: "".to_string(),
            richtext_json: "".to_string(),
            title: "Self Test title".to_string(),
            ad: "".to_string(),
            flair_text: "".to_string(),
            g_recaptcha_response: "".to_string(),
            extension: "".to_string(),
            nsfw: "".to_string(),
            api_type: "json".to_string(),
            kind: "self".to_string(),
            event_end: "".to_string(),
            event_start: "".to_string(),
            app: "".to_string(),
            flair_id: "".to_string(),
            event_tz: "".to_string(),
            sr: SUBREDDIT.to_string(),
            spoiler: "".to_string(),
            text: "Sample text".to_string(),
        };

        let response = match tokio_test::block_on(pants.submit(request_body)) {
            Ok(response) => response,
            Err(e) => panic!("An error ocurred: {}", e),
        };

        println!(
            "Response to submit is: {}",
            serde_json::to_string_pretty(&response).unwrap()
        );

        let submission_name = response.json.data.name;
        println!("The name of the submission is '{}'", submission_name);

        let delete_request_body = links_and_comments::ApiDel { id: submission_name };

        match tokio_test::block_on(pants.del(delete_request_body)) {
            Ok(response) => println!(
                "Response to submit is: {}",
                serde_json::to_string_pretty(&response).unwrap()
            ),
            Err(e) => panic!("An error ocurred: {}", e),
        };
    }

    #[test]
    fn crosspost() {
        let pants = build_pants();

        let request_body = links_and_comments::ApiSubmitCrosspost {
            api_type: "json".to_string(),
            crosspost_fullname: "t3_j5hste".to_string(),
            kind: "crosspost".to_string(),
            nsfw: "false".to_string(),
            original_content: "false".to_string(),
            post_to_twitter: "false".to_string(),
            sendreplies: "true".to_string(),
            show_error_list: "true".to_string(),
            spoiler: "false".to_string(),
            sr: "testingground4bots".to_string(),
            submit_type: "subreddit".to_string(),
            title: "MK3S now, or Mini with the intention of upgrading to MK4 / XL in a couple years?".to_string(),
            validate_on_submit: "true".to_string(),
        };

        let response = match tokio_test::block_on(pants.crosspost(request_body)) {
            Ok(response) => response,
            Err(e) => panic!("An error ocurred: {}", e),
        };

        println!(
            "Response to submit is: {}",
            serde_json::to_string_pretty(&response).unwrap()
        );

        let submission_name = response.json.data.name;
        println!("The name of the submission is '{}'", submission_name);

        let delete_request_body = links_and_comments::ApiDel { id: submission_name };

        match tokio_test::block_on(pants.del(delete_request_body)) {
            Ok(response) => println!(
                "Response to del is: {}",
                serde_json::to_string_pretty(&response).unwrap()
            ),
            Err(e) => panic!("An error ocurred: {}", e),
        };
    }
}