aboutsummaryrefslogtreecommitdiff
path: root/js/tests/ElggSecurityTest.js
blob: 107c0adbd0edaddc1b9aac7816c949587a9aa39c (plain)
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
ElggSecurityTest = TestCase("ElggSecurityTest");

ElggSecurityTest.prototype.setUp = function() {
	//fill with fake, but reasonable, values for testing
	this.ts = elgg.security.token.__elgg_ts = 12345;
	this.token = elgg.security.token.__elgg_token = 'abcdef';
};

ElggSecurityTest.prototype.testAddTokenAcceptsUndefined = function() {
	var input,
		expected = {
			__elgg_ts: this.ts,
			__elgg_token: this.token
		};
	
	assertEquals(expected, elgg.security.addToken(input));
};

ElggSecurityTest.prototype.testAddTokenAcceptsObject = function() {
	var input = {},
		expected = {
			__elgg_ts: this.ts,
			__elgg_token: this.token
		};
	
	assertEquals(expected, elgg.security.addToken(input));
};

ElggSecurityTest.prototype.testAddTokenAcceptsRelativeUrl = function() {
	var input,
		str = "__elgg_ts=" + this.ts + "&__elgg_token=" + this.token;

	input = "test";
	assertEquals(input + '?' + str, elgg.security.addToken(input));
};

ElggSecurityTest.prototype.testAddTokenAcceptsFullUrl = function() {
	var input,
		str = "__elgg_ts=" + this.ts + "&__elgg_token=" + this.token;

	input = "http://elgg.org/";
	assertEquals(input + '?' + str, elgg.security.addToken(input));
};

ElggSecurityTest.prototype.testAddTokenAcceptsQueryString = function() {
	var input,
		str = "__elgg_ts=" + this.ts + "&__elgg_token=" + this.token;

	input = "?data=sofar";
	assertEquals(input + '&' + str, elgg.security.addToken(input));

	input = "test?data=sofar";
	assertEquals(input + '&' + str, elgg.security.addToken(input));

	input = "http://elgg.org/?data=sofar";
	assertEquals(input + '&' + str, elgg.security.addToken(input));
};

ElggSecurityTest.prototype.testAddTokenAlreadyAdded = function() {
	var input,
		str = "__elgg_ts=" + this.ts + "&__elgg_token=" + this.token;

	input = "http://elgg.org/?" + str + "&data=sofar";
	assertEquals(input, elgg.security.addToken(input));
};

ElggSecurityTest.prototype.testSetTokenSetsElggSecurityToken = function() {
	var json = {
		__elgg_ts: 4567,
		__elgg_token: 'abcdef'
	};
	
	elgg.security.setToken(json);
	assertEquals(json, elgg.security.token);
};