summaryrefslogtreecommitdiffstats
path: root/src/apps.rs
blob: d54525a4bbc779c13f530f99c237830549062fea (plain)
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
use std::borrow::Cow;

use serde::Serialize;
use try_from::TryInto;

use crate::{
    errors::{Error, Result},
    scopes::Scopes,
};

/// Represents an application that can be registered with a mastodon instance
#[derive(Clone, Debug, Default, Serialize, PartialEq)]
pub struct App {
    client_name: String,
    redirect_uris: String,
    scopes: Scopes,
    #[serde(skip_serializing_if = "Option::is_none")]
    website: Option<String>,
}

impl App {
    /// Get an AppBuilder object
    ///
    /// # Example
    ///
    /// ```
    /// # extern crate elefren;
    /// use elefren::apps::App;
    ///
    /// let mut builder = App::builder();
    /// ```
    pub fn builder<'a>() -> AppBuilder<'a> {
        AppBuilder::new()
    }

    /// Retrieve the list of scopes that apply to this App
    ///
    /// # Example
    ///
    /// ```
    /// # extern crate elefren;
    /// # use elefren::Error;
    /// use elefren::{apps::App, scopes::Scopes};
    ///
    /// # fn main() -> Result<(), Error> {
    /// let mut builder = App::builder();
    /// builder.client_name("elefren-test");
    /// let app = builder.build()?;
    /// let scopes = app.scopes();
    /// assert_eq!(scopes, &Scopes::read_all());
    /// #   Ok(())
    /// # }
    /// ```
    pub fn scopes(&self) -> &Scopes {
        &self.scopes
    }
}

/// Builder struct for defining your application.
/// ```
/// use elefren::apps::App;
/// use std::error::Error;
///
/// # fn main() -> Result<(), Box<dyn Error>> {
/// let mut builder = App::builder();
/// builder.client_name("elefren_test");
/// let app = builder.build()?;
/// #   Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
pub struct AppBuilder<'a> {
    client_name: Option<Cow<'a, str>>,
    redirect_uris: Option<Cow<'a, str>>,
    scopes: Option<Scopes>,
    website: Option<Cow<'a, str>>,
}

impl<'a> AppBuilder<'a> {
    /// Creates a new AppBuilder object
    pub fn new() -> Self {
        Default::default()
    }

    /// Name of the application. Will be displayed when the user is deciding to
    /// grant permission.
    ///
    /// In order to turn this builder into an App, this needs to be provided
    pub fn client_name<I: Into<Cow<'a, str>>>(&mut self, name: I) -> &mut Self {
        self.client_name = Some(name.into());
        self
    }

    /// Where the user should be redirected after authorization
    ///
    /// If none is specified, the default is `urn:ietf:wg:oauth:2.0:oob`
    pub fn redirect_uris<I: Into<Cow<'a, str>>>(&mut self, uris: I) -> &mut Self {
        self.redirect_uris = Some(uris.into());
        self
    }

    /// Permission scope of the application.
    ///
    /// IF none is specified, the default is Scopes::read_all()
    pub fn scopes(&mut self, scopes: Scopes) -> &mut Self {
        self.scopes = Some(scopes);
        self
    }

    /// URL to the homepage of your application.
    pub fn website<I: Into<Cow<'a, str>>>(&mut self, website: I) -> &mut Self {
        self.website = Some(website.into());
        self
    }

    /// Attempts to convert this build into an `App`
    ///
    /// Will fail if no `client_name` was provided
    pub fn build(self) -> Result<App> {
        Ok(App {
            client_name: self
                .client_name
                .ok_or(Error::MissingField("client_name"))?
                .into(),
            redirect_uris: self
                .redirect_uris
                .unwrap_or_else(|| "urn:ietf:wg:oauth:2.0:oob".into())
                .into(),
            scopes: self.scopes.unwrap_or_else(Scopes::read_all),
            website: self.website.map(|s| s.into()),
        })
    }
}

impl TryInto<App> for App {
    type Err = Error;

    fn try_into(self) -> Result<App> {
        Ok(self)
    }
}

impl<'a> TryInto<App> for AppBuilder<'a> {
    type Err = Error;

    fn try_into(self) -> Result<App> {
        Ok(self.build()?)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_app_builder() {
        let builder = App::builder();
        assert_eq!(builder, AppBuilder::new());
    }

    #[test]
    fn test_app_scopes() {
        let mut builder = App::builder();
        builder.client_name("test").scopes(Scopes::all());
        let app = builder.build().expect("Couldn't build App");
        assert_eq!(app.scopes(), &Scopes::all());
    }

    #[test]
    fn test_app_builder_all_methods() {
        let mut builder = AppBuilder::new();
        builder.client_name("foo-test");
        builder.redirect_uris("http://example.com");
        builder.scopes(Scopes::read_all() | Scopes::write_all());
        builder.website("https://example.com");
        let app = builder.build().expect("Couldn't build App");
        assert_eq!(
            app,
            App {
                client_name: "foo-test".to_string(),
                redirect_uris: "http://example.com".to_string(),
                scopes: Scopes::read_all() | Scopes::write_all(),
                website: Some("https://example.com".to_string()),
            }
        );
    }

    #[test]
    #[should_panic]
    fn test_app_builder_build_fails_if_no_client_name_1() {
        App::builder().build().expect("no client-name");
    }

    #[test]
    #[should_panic]
    fn test_app_builder_build_fails_if_no_client_name_2() {
        let mut builder = App::builder();
        builder
            .website("https://example.com")
            .redirect_uris("https://example.com")
            .scopes(Scopes::all());
        builder.build().expect("no client-name");
    }

    #[test]
    fn test_app_try_into_app() {
        let app = App {
            client_name: "foo-test".to_string(),
            redirect_uris: "http://example.com".to_string(),
            scopes: Scopes::all(),
            website: None,
        };
        let expected = app.clone();
        let result = app.try_into().expect("Couldn't make App into App");
        assert_eq!(expected, result);
    }

    #[test]
    fn test_app_builder_try_into_app() {
        let mut builder = App::builder();
        builder
            .client_name("foo-test")
            .redirect_uris("http://example.com")
            .scopes(Scopes::all());
        let expected = App {
            client_name: "foo-test".to_string(),
            redirect_uris: "http://example.com".to_string(),
            scopes: Scopes::all(),
            website: None,
        };
        let result = builder
            .try_into()
            .expect("Couldn't make AppBuilder into App");
        assert_eq!(expected, result);
    }
}