fxa_client/state_machine/internal_machines/
auth_issues.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use super::{invalid_transition, Event, InternalStateMachine, State};
6use crate::{Error, FxaEvent, FxaState, Result};
7
8pub struct AuthIssuesStateMachine;
9
10// Save some typing
11use Event::*;
12use State::*;
13
14impl InternalStateMachine for AuthIssuesStateMachine {
15    fn initial_state(&self, event: FxaEvent) -> Result<State> {
16        match event {
17            FxaEvent::BeginOAuthFlow { scopes, entrypoint } => Ok(BeginOAuthFlow {
18                scopes: scopes.clone(),
19                entrypoint: entrypoint.clone(),
20            }),
21            FxaEvent::Disconnect => Ok(Complete(FxaState::Disconnected)),
22            e => Err(Error::InvalidStateTransition(format!("AuthIssues -> {e}"))),
23        }
24    }
25
26    fn next_state(&self, state: State, event: Event) -> Result<State> {
27        Ok(match (state, event) {
28            (BeginOAuthFlow { .. }, BeginOAuthFlowSuccess { oauth_url }) => {
29                Complete(FxaState::Authenticating { oauth_url })
30            }
31            (BeginOAuthFlow { .. }, CallError) => Cancel,
32            (state, event) => return invalid_transition(state, event),
33        })
34    }
35}
36
37#[cfg(test)]
38mod test {
39    use super::super::StateMachineTester;
40    use super::*;
41
42    #[test]
43    fn test_reauthenticate() {
44        let tester = StateMachineTester::new(
45            AuthIssuesStateMachine,
46            FxaEvent::BeginOAuthFlow {
47                scopes: vec!["profile".to_owned()],
48                entrypoint: "test-entrypoint".to_owned(),
49            },
50        );
51
52        assert_eq!(
53            tester.state,
54            BeginOAuthFlow {
55                scopes: vec!["profile".to_owned()],
56                entrypoint: "test-entrypoint".to_owned()
57            }
58        );
59        assert_eq!(tester.peek_next_state(CallError), Cancel);
60        assert_eq!(
61            tester.peek_next_state(BeginOAuthFlowSuccess {
62                oauth_url: "http://example.com/oauth-start".to_owned()
63            }),
64            Complete(FxaState::Authenticating {
65                oauth_url: "http://example.com/oauth-start".to_owned(),
66            })
67        );
68    }
69
70    #[test]
71    fn test_disconnect() {
72        let tester = StateMachineTester::new(AuthIssuesStateMachine, FxaEvent::Disconnect);
73        assert_eq!(tester.state, Complete(FxaState::Disconnected));
74    }
75}