42
42
from wire import api
43
43
from wire .context import Context
44
44
import csv
45
+ import time # for `time.sleep`. FUTUREWORK: you can probably tweak the
46
+ # sleeps we've injected a lot, or remove all of them,
47
+ # without doing any harm. just test it on staging first!
45
48
import argparse
46
49
47
50
### configure this section
48
51
49
52
scim_token = "..."
50
53
filename = './test.csv'
51
- ctx = Context ( domain = "localhost" , version = "4" , service_map = { 'spar' : 8081 , 'brig' : 8082 })
54
+ ctx = None
52
55
53
56
### brig, spar api
54
57
@@ -72,61 +75,96 @@ def confirm_new_email(ctx, user_id, key, code):
72
75
url = ctx .mkurl ("brig" , f"/activate" )
73
76
return ctx .request ('GET' , url , headers = ({'Z-User' : user_id }), params = ({'key' : key , 'code' : code }))
74
77
78
+ def assert_resp (resp , status_want ):
79
+ if resp .response .status_code == status_want :
80
+ return
81
+ else :
82
+ print (resp .text );
83
+ # FUTUREWORK: it's not necessary to fail here; we could also
84
+ # printf this and continue with the next row of the input;
85
+ # later collecting all the failing cases into a list to be
86
+ # handed back to the team admin for further analysis.
87
+ assert False
88
+
75
89
### idioms
76
90
77
91
def confirm_email (user_id , email ):
78
92
r = get_activation_code (ctx , user_id , email )
79
- assert r . response . status_code == 200
93
+ assert_resp ( r , 200 )
80
94
r2 = confirm_new_email (ctx , user_id , r .json ()['key' ], r .json ()['code' ])
81
- assert r2 . response . status_code == 200
95
+ assert_resp ( r2 , 200 )
82
96
return r2
83
97
98
+ # update from one email address to another. this is called twice,
99
+ # first for "to old address" to make the db state consistent, and then
100
+ # for "to new address". it's possible that this should be two
101
+ # functions, but so far they have no difference to suggest that.
84
102
def update (user_id , email ):
85
103
r = get_scim_user (ctx , user_id )
86
- assert r . response . status_code == 200
104
+ assert_resp ( r , 200 )
87
105
body = dict (r .json ())
88
106
if body ['externalId' ] != email :
89
107
body ['externalId' ] = email
90
108
r2 = put_scim_user (ctx , user_id , body )
91
- assert r2 .response .status_code == 200
109
+ body2 = r2 .json ()
110
+ # FUTUREWORK: there are some legitimate ways to fail in the
111
+ # following line, if the email address is already the old one.
112
+ # add code here to your liking in order to handle these more
113
+ # gracefully. you may want to distinguish between "change to
114
+ # old address to make everything consistent" (where it's ok if
115
+ # you find that it's already consistent, and you don't need to
116
+ # do anything) and "change to new email address" (where you
117
+ # certainly want a response that says "yes, it worked").
118
+ assert_resp (r2 , 200 )
92
119
return True
93
120
else :
94
121
report_state ('old=new' , user_id )
95
122
return False
96
123
97
124
def report_state (msg , user_id ):
98
125
r = get_scim_user (ctx , user_id )
99
- assert r . response . status_code == 200
126
+ assert_resp ( r , 200 )
100
127
r2 = get_brig_user (ctx , user_id )
101
- assert r . response . status_code == 200
128
+ assert_resp ( r2 , 200 )
102
129
print (f"[{ msg } ] uid={ user_id } ; scim_email={ r .json ()['externalId' ]} ; brig_email={ r2 .json ()['email' ]} " )
130
+ return r .json ()['externalId' ], r2 .json ()['email' ]
131
+
103
132
104
133
### process one item
105
134
106
135
def update_back_and_forth (user_id , old_email , new_email ):
107
136
assert old_email != new_email
108
137
109
138
# nothing has happened yet
110
- report_state ('before' , user_id )
139
+ email_scim , email_brig = report_state ('before' , user_id )
111
140
112
141
# change to old email address to unblock pending confirmation
142
+ # if brig already has the old email, setting to old email won't yield an activation code, so we don't.
113
143
if update (user_id , old_email ):
114
- confirm_email (user_id , old_email )
144
+ if email_brig != old_email :
145
+ time .sleep (4 )
146
+ confirm_email (user_id , old_email )
115
147
report_state ('between' , user_id )
116
148
117
149
# change back to new email address
118
- assert update (user_id , new_email )
119
- confirm_email (user_id , new_email )
150
+ if update (user_id , new_email ):
151
+ confirm_email (user_id , new_email )
152
+ else :
153
+ # nothing to confirm; logging happens in update_to_new
154
+ pass
120
155
report_state ('after' , user_id )
121
156
122
157
### process csv file
123
158
124
159
def main ():
160
+ global ctx
125
161
with open (filename , newline = '' ) as csvfile :
126
162
rows = csv .reader (csvfile , delimiter = ',' )
127
163
for row in rows :
164
+ ctx = Context (domain = "localhost" , version = "4" , service_map = {'spar' : 8081 , 'brig' : 8082 })
128
165
[old_email , user_id , new_email ] = row
129
166
update_back_and_forth (user_id , old_email , new_email )
167
+ time .sleep (5 )
130
168
131
169
if __name__ == '__main__' :
132
170
main ()
0 commit comments