forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseSession-test.js
154 lines (139 loc) · 4.65 KB
/
ParseSession-test.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
jest.dontMock('../CoreManager');
jest.dontMock('../decode');
jest.dontMock('../encode');
jest.dontMock('../isRevocableSession');
jest.dontMock('../ObjectStateMutations');
jest.dontMock('../parseDate');
jest.dontMock('../ParseError');
jest.dontMock('../ParseObject');
jest.dontMock('../ParseOp');
jest.dontMock('../ParseSession');
jest.dontMock('../RESTController');
jest.dontMock('../Storage');
jest.dontMock('../StorageController.default');
jest.dontMock('../TaskQueue');
jest.dontMock('../unique');
jest.dontMock('../UniqueInstanceStateController');
jest.dontMock('./test_helpers/mockXHR');
const mockUser = function (token) {
this.token = token;
};
mockUser.prototype.getSessionToken = function () {
return this.token;
};
mockUser.current = function () {
return null;
};
jest.setMock('../ParseUser', mockUser);
const CoreManager = require('../CoreManager');
const ParseObject = require('../ParseObject').default;
const ParseSession = require('../ParseSession').default;
CoreManager.set('APPLICATION_ID', 'A');
CoreManager.set('JAVASCRIPT_KEY', 'B');
describe('ParseSession', () => {
it('can be initialized', () => {
let session = new ParseSession();
session.set('someField', 'someValue');
expect(session.getSessionToken()).toBe('');
expect(session.get('someField')).toBe('someValue');
session = new ParseSession({
someField: 'someValue',
});
expect(session.get('someField')).toBe('someValue');
});
it('cannot create schema with invalid fields', () => {
expect(() => {
new ParseSession({ 'invalid#name': 'foo' });
}).toThrow("Can't create an invalid Session");
});
it('cannot write to readonly fields', () => {
const session = new ParseSession();
expect(session.set.bind(session, 'createdWith', 'facebook')).toThrow(
'Cannot modify readonly attribute: createdWith'
);
expect(session.set.bind(session, 'expiresAt', new Date())).toThrow(
'Cannot modify readonly attribute: expiresAt'
);
expect(session.set.bind(session, 'installationId', 'iid')).toThrow(
'Cannot modify readonly attribute: installationId'
);
expect(session.set.bind(session, 'restricted', true)).toThrow(
'Cannot modify readonly attribute: restricted'
);
expect(session.set.bind(session, 'sessionToken', 'st')).toThrow(
'Cannot modify readonly attribute: sessionToken'
);
expect(session.set.bind(session, 'user', null)).toThrow(
'Cannot modify readonly attribute: user'
);
});
it('exposes the token through a getter', () => {
const session = new ParseSession();
session._finishFetch({
id: 'session1',
sessionToken: 'abc123',
});
expect(session.getSessionToken()).toBe('abc123');
});
it('checks the current user for a revocable token', () => {
expect(ParseSession.isCurrentSessionRevocable()).toBe(false);
mockUser.current = function () {
return new mockUser('r:abc123');
};
expect(ParseSession.isCurrentSessionRevocable()).toBe(true);
mockUser.current = function () {
return new mockUser('abc123');
};
expect(ParseSession.isCurrentSessionRevocable()).toBe(false);
mockUser.current = function () {
return new mockUser(null);
};
expect(ParseSession.isCurrentSessionRevocable()).toBe(false);
});
it('can fetch the full session for the current token', done => {
CoreManager.setRESTController({
request(method, path, _body, options) {
expect(method).toBe('GET');
expect(path).toBe('sessions/me');
expect(options).toEqual({
sessionToken: 'abc123',
useMasterKey: true,
});
return Promise.resolve({
objectId: 'session1',
sessionToken: 'abc123',
});
},
ajax() {},
});
mockUser.currentAsync = function () {
return Promise.resolve(new mockUser('abc123'));
};
ParseSession.current({ useMasterKey: true }).then(session => {
expect(session instanceof ParseSession).toBe(true);
expect(session.id).toBe('session1');
expect(session.getSessionToken()).toBe('abc123');
done();
});
});
it('cannot get current session without current user', done => {
mockUser.currentAsync = function () {
return Promise.resolve(null);
};
ParseSession.current().catch(error => {
expect(error).toBe('There is no current user.');
done();
});
});
it('can be cloned', () => {
const s = ParseObject.fromJSON({
className: '_Session',
sessionToken: '123abc',
foo: 12,
});
const clone = s.clone();
expect(clone.className).toBe('_Session');
expect(clone.get('foo')).toBe(12);
expect(clone.get('sessionToken')).toBe(undefined);
});
});