summaryrefslogtreecommitdiff
path: root/anastasis/src/main/java/net/taler/anastasis/viewmodels/FakeReducerViewModel.kt
blob: 3f482e401b6b71e021fab3d3badf0e57b51cfde1 (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
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
/*
 * This file is part of GNU Taler
 * (C) 2023 Taler Systems S.A.
 *
 * GNU Taler is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3, or (at your option) any later version.
 *
 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

package net.taler.anastasis.viewmodels

import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import net.taler.anastasis.backend.TalerErrorInfo
import net.taler.anastasis.backend.Tasks
import net.taler.anastasis.models.AggregatedPolicyMetaInfo
import net.taler.anastasis.models.AuthMethod
import net.taler.anastasis.models.AuthenticationProviderStatus
import net.taler.anastasis.models.BackupStates
import net.taler.anastasis.models.ChallengeFeedback
import net.taler.anastasis.models.ChallengeInfo
import net.taler.anastasis.models.ContinentInfo
import net.taler.anastasis.models.CoreSecret
import net.taler.anastasis.models.CountryInfo
import net.taler.anastasis.models.MethodSpec
import net.taler.anastasis.models.Policy
import net.taler.anastasis.models.RecoveryInformation
import net.taler.anastasis.models.RecoveryInternalData
import net.taler.anastasis.models.RecoveryStates
import net.taler.anastasis.models.ReducerState
import net.taler.anastasis.models.SelectedVersionInfo
import net.taler.anastasis.models.SuccessDetail
import net.taler.anastasis.models.UserAttributeSpec
import net.taler.common.Amount
import net.taler.common.Timestamp

open class FakeReducerViewModel(
    state: ReducerState,
    error: TalerErrorInfo? = null,
): ReducerViewModelI {
    override val reducerManager = null
    private val _reducerState = MutableStateFlow<ReducerState?>(state)
    override val reducerState: StateFlow<ReducerState?> = _reducerState.asStateFlow()
    private val _reducerError = MutableStateFlow(error)
    override val reducerError: StateFlow<TalerErrorInfo?> = _reducerError.asStateFlow()
    private val _loading = MutableStateFlow(Tasks())
    override val tasks = _loading.asStateFlow()

    override fun goBack(): Boolean = false

    override fun goHome() {
        _reducerState.value = null
    }

    override fun cleanError() {
        _reducerError.value = null
    }
}

internal val continents = listOf(
    ContinentInfo(name = "Europe"),
    ContinentInfo(name = "North America"),
)

internal val countries = listOf(
    CountryInfo(
        code = "ch",
        name = "Switzerland",
        continent = "Europe",
    ),
    CountryInfo(
        code = "de",
        name = "Germany",
        continent = "Europe",
    )
)

internal val identityAttributes = mapOf(
    "full_name" to "Max Musterman",
    "birthdate" to "2000-01-01",
)

internal val authenticationProviders = mapOf(
    "http://localhost:8088/" to AuthenticationProviderStatus.Ok(
        httpStatus = 200,
        methods = listOf(
            MethodSpec(type = AuthMethod.Type.Question, usageFee = "EUR:0.001"),
            MethodSpec(type = AuthMethod.Type.Sms, usageFee = "EUR:0.55"),
        ),
        annualFee = "EUR:0.99",
        truthUploadFee = "EUR:3.99",
        liabilityLimit = "EUR:1",
        currency = "EUR",
        storageLimitInMegabytes = 1,
        businessName = "Anastasis 4",
        providerSalt = "CXAPCKSH9D3MYJTS9536RHJHCW",
    ),
    "http://localhost:8089/" to AuthenticationProviderStatus.Ok(
        httpStatus = 200,
        methods = listOf(
            MethodSpec(type = AuthMethod.Type.Question, usageFee = "EUR:0.001"),
            MethodSpec(type = AuthMethod.Type.Sms, usageFee = "EUR:0.55"),
        ),
        annualFee = "EUR:0.99",
        truthUploadFee = "EUR:3.99",
        liabilityLimit = "EUR:1",
        currency = "EUR",
        storageLimitInMegabytes = 1,
        businessName = "Anastasis 2",
        providerSalt = "CXAPCKSH9D3MYJTS9536RHJHCW",
    )
)

internal val requiredAttributes = listOf(
    UserAttributeSpec(
        type = "string",
        name = "full_name",
        label = "Full name",
        widget = "anastasis_gtk_ia_full_name",
        uuid = "9e8f463f-575f-42cb-85f3-759559997331",
    ),
    UserAttributeSpec(
        type = "date",
        name = "birthdate",
        label = "Birthdate",
        widget = "anastasis_gtk_ia_birthdate",
        uuid = "83d655c7-bdb6-484d-904e-80c1058c8854",
    ),
)

internal const val selectedContinent = "Europe"
internal const val selectedCountry = "ch"

internal val coreSecret = CoreSecret(
    value = "EDJP6WK5EG50",
    mime = "text/plain",
)

class FakeBackupViewModel(
    backupState: BackupStates,
): FakeReducerViewModel(
    state = ReducerState.Backup(
        backupState = backupState,
        continents = continents,
        countries = countries,
        identityAttributes = identityAttributes,
        authenticationProviders = authenticationProviders,
        authenticationMethods = listOf(
            AuthMethod(
                type = AuthMethod.Type.Question,
                mimeType = "text/plain",
                challenge = "E1QPPS8A",
                instructions = "What is your favorite GNU package?",
            ),
            AuthMethod(
                type = AuthMethod.Type.Email,
                instructions = "E-mail to user@*le.com",
                challenge = "ENSPAWJ0CNW62VBGDHJJWRVFDM50",
            )
        ),
        requiredAttributes = requiredAttributes,
        selectedContinent = selectedContinent,
        selectedCountry = selectedCountry,
        secretName = "_TALERWALLET_MyPinePhone",
        policies = listOf(
            Policy(
                methods = listOf(
                    Policy.PolicyMethod(
                        authenticationMethod = 0,
                        provider = "http://localhost:8089/",
                    ),
                    Policy.PolicyMethod(
                        authenticationMethod = 1,
                        provider = "http://localhost:8088/",
                    ),
                ),
            ),
            Policy(
                methods = listOf(
                    Policy.PolicyMethod(
                        authenticationMethod = 0,
                        provider = "http://localhost:8089/",
                    ),
                ),
            ),
        ),
        successDetails = mapOf(
            "http://localhost:8088/" to SuccessDetail(
                policyVersion = 1,
                policyExpiration = Timestamp.now(),
            ),
        ),
        coreSecret = coreSecret,
        uploadFees = listOf(
            ReducerState.Backup.UploadFee(
                fee = Amount("KUDOS", 42L, 0),
            ),
        ),
    ),
)

class FakeRecoveryViewModel(
    recoveryState: RecoveryStates,
): FakeReducerViewModel(
    state = ReducerState.Recovery(
        recoveryState = recoveryState,
        continents = continents,
        countries = countries,
        identityAttributes = identityAttributes,
        selectedContinent = selectedContinent,
        selectedCountry = selectedCountry,
        requiredAttributes = requiredAttributes,
        recoveryInformation = RecoveryInformation(
            challenges = listOf(
                ChallengeInfo(
                    instructions = "What is your favorite GNU package?",
                    type = AuthMethod.Type.Question,
                    uuid = "RNB84NQZPCM3MZWF9D5FFMSYYN07J2NAT5N8Q0DBHHT7R3GJ4AA0",
                ),
                ChallengeInfo(
                    instructions = "E-mail to user@*le.com",
                    type = AuthMethod.Type.Email,
                    uuid = "ZA6T35B8XAR0DNKS5H100GK8PDPTA7Q8ST2FPQSYAZ4QRAA9XKK0",
                ),
            ),
            policies = listOf(
                listOf(
                    RecoveryInformation.Policy(uuid = "RNB84NQZPCM3MZWF9D5FFMSYYN07J2NAT5N8Q0DBHHT7R3GJ4AA0"),
                    RecoveryInformation.Policy(uuid = "ZA6T35B8XAR0DNKS5H100GK8PDPTA7Q8ST2FPQSYAZ4QRAA9XKK0")
                ),
            ),
        ),
        recoveryDocument = RecoveryInternalData(
            secretName = "Secret",
            providerUrl = "http://localhost:8089",
            version = 1,
        ),
        selectedChallengeUuid = "RNB84NQZPCM3MZWF9D5FFMSYYN07J2NAT5N8Q0DBHHT7R3GJ4AA0",
        challengeFeedback = mapOf(
            "RNB84NQZPCM3MZWF9D5FFMSYYN07J2NAT5N8Q0DBHHT7R3GJ4AA0" to ChallengeFeedback.IncorrectAnswer,
            "ZA6T35B8XAR0DNKS5H100GK8PDPTA7Q8ST2FPQSYAZ4QRAA9XKK0" to ChallengeFeedback.Solved,
        ),
        coreSecret = coreSecret,
        authenticationProviders = authenticationProviders,
        discoveryState = ReducerState.Recovery.DiscoveryState(
            state = "finished",
            aggregatedPolicies = listOf(
                AggregatedPolicyMetaInfo(
                    attributeMask = 0,
                    policyHash = "000000000000000000000000000000000000000000000000000B28GR6691Y51HR2SAFJZFF0DCMRDZD1YQMS03A55P9NCWHQGEKW8",
                    providers = listOf(
                        SelectedVersionInfo.Provider(
                            url = "https://v1.anastasis.taler.net/",
                            version = 1,
                        ),
                        SelectedVersionInfo.Provider(
                            url = "https://v1.anastasis.codeblau.de/",
                            version = 1,
                        ),
                    ),
                    secretName = "Secret",
                ),
            ),
        ),
    )
)