-
Notifications
You must be signed in to change notification settings - Fork 87
/
phone_tests_setup.js
161 lines (136 loc) · 5.16 KB
/
phone_tests_setup.js
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
// Phone test vars setup
Accounts._options.verificationWaitTime = 1;
Accounts._options.verificationRetriesWaitTime = 1;
Accounts._options.verificationMaxRetries = 100;
Accounts.validateNewUser(function (user) {
if (user.profile && user.profile.invalidAndThrowException) {
throw new Meteor.Error(403, "An exception thrown within Accounts.validateNewUser");
}
return !(user.profile && user.profile.invalid);
});
Accounts.onCreateUser(function (options, user) {
if (options.testOnCreateUserHook) {
user.profile = user.profile || {};
user.profile.touchedByOnCreateUser = true;
return user;
} else {
return 'TEST DEFAULT HOOK';
}
});
// connection id -> action
var invalidateLogins = {};
Meteor.methods({
testInvalidateLogins: function (action) {
if (action)
invalidateLogins[this.connection.id] = action;
else
delete invalidateLogins[this.connection.id];
}
});
Accounts.validateLoginAttempt(function (attempt) {
var action =
attempt &&
attempt.connection &&
invalidateLogins[attempt.connection.id];
if (! action)
return true;
else if (action === 'fail')
return false;
else if (action === 'hide')
throw new Meteor.Error(403, 'hide actual error');
else
throw new Error('unknown action: ' + action);
});
// connection id -> [{successful: boolean, attempt: object}]
var capturedLogins = {};
Meteor.methods({
testCaptureLogins: function () {
capturedLogins[this.connection.id] = [];
},
testFetchCapturedLogins: function () {
if (capturedLogins[this.connection.id]) {
var logins = capturedLogins[this.connection.id];
delete capturedLogins[this.connection.id];
return logins;
}
else
return [];
}
});
Accounts.onLogin(function (attempt) {
if (capturedLogins[attempt.connection.id])
capturedLogins[attempt.connection.id].push({
successful: true,
attempt: _.omit(attempt, 'connection')
});
});
Accounts.onLoginFailure(function (attempt) {
if (capturedLogins[attempt.connection.id]) {
capturedLogins[attempt.connection.id].push({
successful: false,
attempt: _.omit(attempt, 'connection')
});
}
});
// Because this is global state that affects every client, we can't turn
// it on and off during the tests. Doing so would mean two simultaneous
// test runs could collide with each other.
//
// We should probably have some sort of server-isolation between
// multiple test runs. Perhaps a separate server instance per run. This
// problem isn't unique to this test, there are other places in the code
// where we do various hacky things to work around the lack of
// server-side isolation.
//
// For now, we just test the one configuration state. You can comment
// out each configuration option and see that the tests fail.
Accounts.config({
});
// Set send phone verification to true
Accounts._options.sendPhoneVerificationCode = true;
Meteor.methods({
testMeteorUser: function () { return Meteor.user(); },
clearPhonsAndProfile: function () {
if (!this.userId)
throw new Error("Not logged in!");
Meteor.users.update(this.userId,
{$unset: {profile: 1, phone: 1}});
},
expireTokens: function () {
Accounts._expireTokens(new Date(), this.userId);
},
removeUser: function (phone) {
Meteor.users.remove({ "phone.number": phone });
}
});
// Create a user that had previously logged in with SRP.
Meteor.methods({
testCreateSRPUser: function () {
var phone = '+97254580'+ (Math.abs(Math.floor(Math.random() * 1000 - 1000)) + 1000);
Meteor.users.remove({'phone.number': phone});
var userId = Accounts.createUserWithPhone({'phone.number': phone});
Meteor.users.update(
userId,
{ '$set': { 'services.phone.srp': {
"identity" : "iPNrshUEcpOSO5fRDu7o4RRDc9OJBCGGljYpcXCuyg9",
"salt" : "Dk3lFggdEtcHU3aKm6Odx7sdcaIrMskQxBbqtBtFzt6",
"verifier" : "2e8bce266b1357edf6952cc56d979db19f699ced97edfb2854b95972f820b0c7006c1a18e98aad40edf3fe111b87c52ef7dd06b320ce452d01376df2d560fdc4d8e74f7a97bca1f67b3cfaef34dee34dd6c76571c247d762624dc166dab5499da06bc9358528efa75bf74e2e7f5a80d09e60acf8856069ae5cfb080f2239ee76"
} } }
);
return username;
},
testSRPUpgrade: function (phone) {
var user = Meteor.users.findOne({'phone.number': phone});
if (user.services && user.services.phone && user.services.phone.srp)
throw new Error("srp wasn't removed");
if (!(user.services && user.services.phone && user.services.phone.bcrypt))
throw new Error("bcrypt wasn't added");
},
testNoSRPUpgrade: function (phone) {
var user = Meteor.users.findOne({'phone.number': phone});
if (user.services && user.services.phone && user.services.phone.bcrypt)
throw new Error("bcrypt was added");
if (user.services && user.services.phone && ! user.services.phone.srp)
throw new Error("srp was removed");
}
});