@@ -15,60 +15,210 @@ struct TestHarnessAPIDecoder {
15
15
static func decode(
16
16
specification: FeatureSpecification ) -> AmplifyAPI {
17
17
18
- guard let expectedAmplifyResponseValidation = specification. validations. first ( where: { validation in
18
+ let expectedAmplifyResponseValidation = specification. validations. first ( where: { validation in
19
19
validation. value ( at: " type " ) == . string( " amplify " )
20
- } ) else {
21
- fatalError ( " Expected Amplify response not found " )
22
- }
20
+ } )
23
21
24
- guard case . string( let responseType) = expectedAmplifyResponseValidation [ " responseType " ] else {
25
- fatalError ( " Expected Amplify response type not found " )
22
+ var responseType : String ? = nil
23
+ if case . string( let responseTypeUnwrapped) = expectedAmplifyResponseValidation ? [ " responseType " ] {
24
+ responseType = responseTypeUnwrapped
26
25
}
27
26
28
- guard case . object( let response) = expectedAmplifyResponseValidation [ " response " ] else {
29
- fatalError ( " Expected Amplify response not found " )
27
+ var data : Data ? = nil
28
+ if case . object( let response) = expectedAmplifyResponseValidation ? [ " response " ] {
29
+ data = try ? JSONEncoder ( ) . encode ( response)
30
30
}
31
31
32
- let data = try ! JSONEncoder ( ) . encode ( response)
33
-
34
32
switch specification. api. name {
35
33
case . resetPassword:
36
34
return resetPasswordAPI (
37
35
params: specification. api. params,
38
36
responseType: responseType,
39
37
data: data
40
38
)
39
+ case . signIn:
40
+ return signInAPI (
41
+ params: specification. api. params,
42
+ responseType: responseType,
43
+ data: data)
44
+ case . signUp:
45
+ return signUpAPI (
46
+ params: specification. api. params,
47
+ responseType: responseType,
48
+ data: data)
49
+ case . deleteUser:
50
+ return deleteUserAPI (
51
+ params: specification. api. params,
52
+ responseType: responseType,
53
+ data: data)
54
+ case . confirmSignIn:
55
+ return confirmSignInAPI (
56
+ params: specification. api. params,
57
+ responseType: responseType,
58
+ data: data)
59
+ case . fetchAuthSession:
60
+ return fetchAuthSession (
61
+ params: specification. api. params,
62
+ responseType: responseType,
63
+ data: data)
64
+ case . signOut:
65
+ return signOutApi (
66
+ options: specification. api. options,
67
+ responseType: responseType,
68
+ data: data)
41
69
default :
42
70
fatalError ( )
43
71
}
44
72
}
45
73
74
+ private static func signInAPI(
75
+ params: JSONValue ,
76
+ responseType: String ? ,
77
+ data: Data ?
78
+ ) -> AmplifyAPI {
79
+ guard case . string( let username) = params [ " username " ] else {
80
+ fatalError ( " missing username parameter " )
81
+ }
82
+ var inputPassword : String ?
83
+ if case . string( let password) = params [ " password " ] {
84
+ inputPassword = password
85
+ }
86
+ return . signIn(
87
+ input: . init(
88
+ username: username,
89
+ password: inputPassword, options: . init( ) ) ,
90
+ expectedOutput: generateResult ( responseType: responseType, data: data) )
91
+ }
92
+
93
+ private static func signUpAPI(
94
+ params: JSONValue ,
95
+ responseType: String ? ,
96
+ data: Data ?
97
+ ) -> AmplifyAPI {
98
+ guard case . string( let username) = params [ " username " ] else {
99
+ fatalError ( " missing username parameter " )
100
+ }
101
+ var inputPassword : String ?
102
+ if case . string( let password) = params [ " password " ] {
103
+ inputPassword = password
104
+ }
105
+ return . signUp(
106
+ input: . init(
107
+ username: username,
108
+ password: inputPassword, options: . init( ) ) ,
109
+ expectedOutput: generateResult ( responseType: responseType, data: data) )
110
+ }
111
+
46
112
private static func resetPasswordAPI(
47
113
params: JSONValue ,
48
- responseType: String ,
49
- data: Data
114
+ responseType: String ? ,
115
+ data: Data ?
50
116
) -> AmplifyAPI {
51
117
guard case . string( let username) = params [ " username " ] else {
52
118
fatalError ( " missing username parameter " )
53
119
}
120
+ return . resetPassword(
121
+ input: . init( username: username,
122
+ options: . init( ) ) ,
123
+ expectedOutput: generateResult ( responseType: responseType, data: data) )
124
+ }
125
+
126
+ private static func confirmSignInAPI(
127
+ params: JSONValue ,
128
+ responseType: String ? ,
129
+ data: Data ?
130
+ ) -> AmplifyAPI {
131
+ guard case . string( let challengeResponse) = params [ " challengeResponse " ] else {
132
+ fatalError ( " missing username parameter " )
133
+ }
134
+ return . confirmSignIn(
135
+ input: . init( challengeResponse: challengeResponse, options: . init( ) ) ,
136
+ expectedOutput: generateResult ( responseType: responseType, data: data) )
137
+ }
138
+
139
+ private static func fetchAuthSession(
140
+ params: JSONValue ,
141
+ responseType: String ? ,
142
+ data: Data ?
143
+ ) -> AmplifyAPI {
144
+
145
+ let result : Result < AWSAuthCognitoSession , AuthError > ? = generateResult (
146
+ responseType: responseType, data: data)
147
+
148
+ return . fetchAuthSession(
149
+ input: . init( options: . init( ) ) ,
150
+ expectedOutput: result)
151
+ }
152
+
153
+ private static func signOutApi(
154
+ options: JSONValue ,
155
+ responseType: String ? ,
156
+ data: Data ?
157
+ ) -> AmplifyAPI {
158
+
159
+ var globalSignOut = false
160
+ if case . boolean( let globalSignOutVal) = options [ " globalSignOut " ] {
161
+ globalSignOut = globalSignOutVal
162
+ }
163
+
164
+ let result : Result < AWSCognitoSignOutResult , AuthError > ? = generateResult (
165
+ responseType: responseType, data: data)
54
166
55
- let result : Result < AuthResetPasswordResult , AuthError >
167
+ return . signOut(
168
+ input: . init( options: . init( globalSignOut: globalSignOut) ) ,
169
+ expectedOutput: result)
170
+ }
171
+
172
+ private static func deleteUserAPI(
173
+ params: JSONValue ,
174
+ responseType: String ? ,
175
+ data: Data ?
176
+ ) -> AmplifyAPI {
177
+
178
+ guard let responseType = responseType, let data = data else {
179
+ return . deleteUser(
180
+ input: ( ) ,
181
+ expectedOutput: nil )
182
+ }
183
+
184
+ let result : Result < Void , AuthError >
56
185
57
186
switch responseType {
58
187
case " failure " :
59
188
let authError = try ! JSONDecoder ( ) . decode (
60
189
AuthError . self, from: data)
61
190
result = . failure( authError)
62
191
case " success " :
63
- let resetPasswordResult = try ! JSONDecoder ( ) . decode (
64
- AuthResetPasswordResult . self, from: data)
65
- result = . success( resetPasswordResult)
192
+ result = . success( ( ) )
66
193
default :
67
194
fatalError ( " invalid response type " )
68
195
}
69
- return . resetPassword(
70
- input: . init( username: username,
71
- options: . init( ) ) ,
196
+ return . deleteUser(
197
+ input: ( ) ,
72
198
expectedOutput: result)
73
199
}
200
+
201
+ private static func generateResult< Output: Decodable > (
202
+ responseType: String ? , data: Data ? ) -> Result < Output , AuthError > ? {
203
+
204
+ guard let responseType = responseType, let data = data else {
205
+ return nil
206
+ }
207
+
208
+ let result : Result < Output , AuthError >
209
+
210
+ switch responseType {
211
+ case " failure " :
212
+ let authError = try ! JSONDecoder ( ) . decode (
213
+ AuthError . self, from: data)
214
+ result = . failure( authError)
215
+ case " success " :
216
+ let output = try ! JSONDecoder ( ) . decode (
217
+ Output . self, from: data)
218
+ result = . success( output)
219
+ default :
220
+ fatalError ( " invalid response type " )
221
+ }
222
+ return result
223
+ }
74
224
}
0 commit comments