forked from artwells/meteor-accounts-guest
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccounts-anonymous-server-tests.js
87 lines (80 loc) · 2.54 KB
/
accounts-anonymous-server-tests.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
"use strict";
/* globals AccountsMultiple, AccountsAnonymous */
Tinytest.add(
'AccountsAnonymous - onAbandoned',
function (test) {
AccountsMultiple._unregisterAll();
AccountsAnonymous._init();
AccountsAnonymous.onAbandoned(function (user) {
actualCalls++;
actualId = user._id;
});
var actualCalls = 0;
var expectedCalls = 0;
var expectedId;
var actualId;
var connection = DDP.connect(Meteor.absoluteUrl());
var anonId, pwId;
try {
// User not logged in.
anonId = connection.call('login', { anonymous: true }).id;
test.equal(actualCalls, expectedCalls);
// Anonymous user logged in.
expectedId = anonId;
pwId = connection.call('createUser',
{ email: '[email protected]', password: 'password' }
).id;
Meteor.users.remove(pwId);
expectedCalls++;
test.equal(actualCalls, expectedCalls);
test.equal(actualId, anonId);
} finally {
connection.disconnect();
if (anonId) { Meteor.users.remove(anonId); }
if (pwId) { Meteor.users.remove(pwId); }
}
}
);
Tinytest.add(
'AccountsAnonymous - onAbandoned - user already removed',
function (test) {
AccountsMultiple._unregisterAll();
AccountsAnonymous._init();
var onAbandonedCalls = 0;
AccountsAnonymous.onAbandoned(function (/* user (unused) */) {
onAbandonedCalls++;
});
var debugCalls = 0;
var origDebug = Meteor._debug;
Meteor._debug = function(/*arguments*/) {
debugCalls++;
origDebug.apply(this, arguments);
};
var anonId, pwId;
var connection = DDP.connect(Meteor.absoluteUrl());
try {
// Create an anonymous user
anonId = connection.call('login', { anonymous: true }).id;
// Add a one-off validateLoginAttempt handler that removes it.
var validateLoginStopper = Accounts.validateLoginAttempt(function() {
validateLoginStopper.stop();
Meteor.users.remove(anonId);
return true;
});
// When we login as a new user, the above handler will remove the
// attempting user. onAbandoned will still fire but no errors should
// occur.
pwId = connection.call('createUser',
{ email: '[email protected]', password: 'password' }
).id;
Meteor.users.remove(pwId);
test.equal(onAbandonedCalls, 1);
test.equal(debugCalls, 0);
} finally {
connection.disconnect();
Meteor._debug = origDebug;
if (anonId) { Meteor.users.remove(anonId); }
if (pwId) { Meteor.users.remove(pwId); }
}
}
);