fxa_client/state_machine/internal_machines/
uninitialized.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, FxaRustAuthState, FxaState, Result};
7
8pub struct UninitializedStateMachine;
9
10// Save some typing
11use Event::*;
12use State::*;
13
14impl InternalStateMachine for UninitializedStateMachine {
15    fn initial_state(&self, event: FxaEvent) -> Result<State> {
16        match event {
17            FxaEvent::Initialize { .. } => Ok(GetAuthState),
18            e => Err(Error::InvalidStateTransition(format!(
19                "Uninitialized -> {e}"
20            ))),
21        }
22    }
23
24    fn next_state(&self, state: State, event: Event) -> Result<State> {
25        Ok(match (state, event) {
26            (GetAuthState, GetAuthStateSuccess { auth_state }) => match auth_state {
27                FxaRustAuthState::Disconnected => Complete(FxaState::Disconnected),
28                FxaRustAuthState::AuthIssues => {
29                    // FIXME: We should move to `AuthIssues` here, but we don't in order to
30                    // match the current firefox-android behavior
31                    // See https://bugzilla.mozilla.org/show_bug.cgi?id=1794212
32                    EnsureDeviceCapabilities
33                }
34                FxaRustAuthState::Connected => EnsureDeviceCapabilities,
35            },
36            (EnsureDeviceCapabilities, EnsureDeviceCapabilitiesSuccess) => {
37                Complete(FxaState::Connected)
38            }
39            (EnsureDeviceCapabilities, CallError) => Complete(FxaState::Disconnected),
40            (EnsureDeviceCapabilities, EnsureCapabilitiesAuthError) => CheckAuthorizationStatus,
41
42            // FIXME: we should re-run `ensure_capabilities` in this case, but we don't in order to
43            // match the current firefox-android behavior.
44            // See https://bugzilla.mozilla.org/show_bug.cgi?id=1868418
45            (CheckAuthorizationStatus, CheckAuthorizationStatusSuccess { active: true }) => {
46                Complete(FxaState::Connected)
47            }
48            (CheckAuthorizationStatus, CheckAuthorizationStatusSuccess { active: false })
49            | (CheckAuthorizationStatus, CallError) => Complete(FxaState::AuthIssues),
50            (state, event) => return invalid_transition(state, event),
51        })
52    }
53}
54
55#[cfg(test)]
56mod test {
57    use super::super::StateMachineTester;
58    use super::*;
59    use crate::{DeviceConfig, DeviceType};
60
61    #[test]
62    fn test_state_machine() {
63        let mut tester = StateMachineTester::new(
64            UninitializedStateMachine,
65            FxaEvent::Initialize {
66                device_config: DeviceConfig {
67                    name: "test-device".to_owned(),
68                    device_type: DeviceType::Mobile,
69                    capabilities: vec![],
70                },
71            },
72        );
73        assert_eq!(tester.state, GetAuthState);
74        assert_eq!(
75            tester.peek_next_state(GetAuthStateSuccess {
76                auth_state: FxaRustAuthState::Disconnected
77            }),
78            Complete(FxaState::Disconnected)
79        );
80        assert_eq!(
81            tester.peek_next_state(GetAuthStateSuccess {
82                auth_state: FxaRustAuthState::AuthIssues
83            }),
84            // FIXME: https://bugzilla.mozilla.org/show_bug.cgi?id=1794212
85            EnsureDeviceCapabilities,
86        );
87
88        tester.next_state(GetAuthStateSuccess {
89            auth_state: FxaRustAuthState::Connected,
90        });
91        assert_eq!(tester.state, EnsureDeviceCapabilities);
92        assert_eq!(
93            tester.peek_next_state(CallError),
94            Complete(FxaState::Disconnected)
95        );
96        assert_eq!(
97            tester.peek_next_state(EnsureDeviceCapabilitiesSuccess),
98            Complete(FxaState::Connected)
99        );
100
101        tester.next_state(EnsureCapabilitiesAuthError);
102        assert_eq!(tester.state, CheckAuthorizationStatus);
103        assert_eq!(
104            tester.peek_next_state(CallError),
105            Complete(FxaState::AuthIssues)
106        );
107        assert_eq!(
108            tester.peek_next_state(CheckAuthorizationStatusSuccess { active: false }),
109            Complete(FxaState::AuthIssues)
110        );
111        assert_eq!(
112            tester.peek_next_state(CheckAuthorizationStatusSuccess { active: true }),
113            Complete(FxaState::Connected)
114        );
115    }
116}