Skip to content

Commit 0355b5c

Browse files
committed
add tests for generateToken connection data #11
1 parent bf5b37c commit 0355b5c

File tree

2 files changed

+39
-66
lines changed

2 files changed

+39
-66
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ repositories {
2828
dependencies {
2929
testCompile group: 'junit', name: 'junit', version: '4.+'
3030
testCompile group: 'com.github.tomakehurst', name: 'wiremock', version: '1.45'
31+
testCompile group: 'commons-lang', name: 'commons-lang', version: '2.6'
3132
compile group: 'com.ning', name: 'async-http-client', version: '1.6.1'
3233
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.13'
3334
compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.13'

src/test/java/com/opentok/test/OpenTokTest.java

Lines changed: 38 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
import java.security.SignatureException;
1313
import java.util.ArrayList;
1414
import java.util.Map;
15+
import org.apache.commons.lang.StringUtils;
1516

1617
import com.opentok.TokenOptions;
17-
1818
import com.opentok.OpenTok;
1919
import com.opentok.Session;
2020
import com.opentok.constants.Version;
@@ -254,71 +254,43 @@ public void testTokenExpireTime() throws
254254

255255
}
256256

257-
// @Test
258-
// public void testRolePublisher() throws OpenTokException {
259-
// Session s= sdk.createSession();
260-
// String t = s.generateToken(Role.PUBLISHER);
261-
// TokBoxXML xml = get_token_info(t);
262-
//
263-
// String expectedRole = "publisher";
264-
// String actualRole = xml.getElementValue("role", "token").trim();
265-
// Assert.assertEquals("Java SDK tests: role not publisher", expectedRole, actualRole);
266-
//
267-
// // Permissions are set as an empty node in the xml
268-
// // Verify that the expected permission node is there
269-
// // Verify nodes for permissions not granted to the role are not there
270-
// Assert.assertTrue("Java SDK tests: publisher role does not have subscriber permissions", xml.hasElement("subscribe", "permissions"));
271-
// Assert.assertTrue("Java SDK tests: publisher role does not have publisher permissions", xml.hasElement("publish", "permissions"));
272-
// Assert.assertTrue("Java SDK tests: publisher role does not have signal permissions", xml.hasElement("signal", "permissions"));
273-
// Assert.assertFalse("Java SDK tests: publisher role should not have forceunpublish permissions", xml.hasElement("forceunpublish", "permissions"));
274-
// Assert.assertFalse("Java SDK tests: publisher role should not have forcedisconnect permissions", xml.hasElement("forcedisconnect", "permissions"));
275-
// Assert.assertFalse("Java SDK tests: publisher role should not have record permissions", xml.hasElement("record", "permissions"));
276-
// Assert.assertFalse("Java SDK tests: publisher role should not have playback permissions", xml.hasElement("playback", "permissions"));
277-
// }
278-
//
279-
// @Test
280-
// public void testRoleSubscriber() throws OpenTokException {
281-
// Session s= sdk.createSession();
282-
// String t = s.generateToken(Role.SUBSCRIBER);
283-
// TokBoxXML xml = get_token_info(t);
284-
//
285-
// String expectedRole = "subscriber";
286-
// String actualRole = xml.getElementValue("role", "token").trim();
287-
// Assert.assertEquals("Java SDK tests: role not subscriber", expectedRole, actualRole);
288-
//
289-
// // Permissions are set as an empty node in the xml
290-
// // Verify that the expected permission node is there
291-
// // Verify nodes for permissions not granted to the role are not there
292-
// Assert.assertTrue("Java SDK tests: subscriber role does not have subscriber permissions", xml.hasElement("subscribe", "permissions"));
293-
// Assert.assertFalse("Java SDK tests: subscriber role should not have publisher permissions", xml.hasElement("publish", "permissions"));
294-
// Assert.assertFalse("Java SDK tests: subscriber role should not have signal permissions", xml.hasElement("signal", "permissions"));
295-
// Assert.assertFalse("Java SDK tests: subscriber role should not have forceunpublish permissions", xml.hasElement("forceunpublish", "permissions"));
296-
// Assert.assertFalse("Java SDK tests: subscriber role should not have forcedisconnect permissions", xml.hasElement("forcedisconnect", "permissions"));
297-
// Assert.assertFalse("Java SDK tests: subscriber role should not have record permissions", xml.hasElement("record", "permissions"));
298-
// Assert.assertFalse("Java SDK tests: subscriber role should not have playback permissions", xml.hasElement("playback", "permissions"));
299-
// }
300-
//
301-
// @Test
302-
// public void testRoleModerator() throws OpenTokException {
303-
// Session s= sdk.createSession();
304-
// String t = s.generateToken(Role.MODERATOR);
305-
// TokBoxXML xml = get_token_info(t);
306-
//
307-
// String expectedRole = "moderator";
308-
// String actualRole = xml.getElementValue("role", "token").trim();
309-
// Assert.assertEquals("Java SDK tests: role not moderator", expectedRole, actualRole);
310-
//
311-
// // Permissions are set as an empty node in the xml
312-
// // Verify that the expected permission node is there
313-
// // Verify nodes for permissions not granted to the role are not there
314-
// Assert.assertTrue("Java SDK tests: moderator role does not have subscriber permissions", xml.hasElement("subscribe", "permissions"));
315-
// Assert.assertTrue("Java SDK tests: moderator role does not have publisher permissions", xml.hasElement("publish", "permissions"));
316-
// Assert.assertTrue("Java SDK tests: moderator role does not have signal permissions", xml.hasElement("signal", "permissions"));
317-
// Assert.assertTrue("Java SDK tests: moderator role does not have forceunpublish permissions", xml.hasElement("forceunpublish", "permissions"));
318-
// Assert.assertTrue("Java SDK tests: moderator role does not have forcedisconnect permissions", xml.hasElement("forcedisconnect", "permissions"));
319-
// Assert.assertTrue("Java SDK tests: moderator role does not have record permissions", xml.hasElement("record", "permissions"));
320-
// Assert.assertTrue("Java SDK tests: moderator role does not have playback permissions", xml.hasElement("playback", "permissions"));
321-
// }
257+
@Test
258+
public void testTokenConnectionData() throws
259+
OpenTokException, SignatureException, NoSuchAlgorithmException, InvalidKeyException,
260+
UnsupportedEncodingException {
261+
262+
int apiKey = 123456;
263+
String apiSecret = "1234567890abcdef1234567890abcdef1234567890";
264+
String sessionId = "1_MX4xMjM0NTZ-flNhdCBNYXIgMTUgMTQ6NDI6MjMgUERUIDIwMTR-MC40OTAxMzAyNX4";
265+
OpenTok opentok = new OpenTok(apiKey, apiSecret);
266+
// purposely contains some exotic characters
267+
String actualData = "{\"name\":\"%foo ç &\"}";
268+
Exception tooLongException = null;
269+
270+
String defaultToken = opentok.generateToken(sessionId);
271+
String dataBearingToken = opentok.generateToken(sessionId, new TokenOptions.Builder()
272+
.data(actualData)
273+
.build());
274+
try {
275+
String dataTooLongToken = opentok.generateToken(sessionId, new TokenOptions.Builder()
276+
.data(StringUtils.repeat("x", 1001))
277+
.build());
278+
} catch (InvalidArgumentException e) {
279+
tooLongException = e;
280+
}
281+
282+
assertNotNull(defaultToken);
283+
assertNotNull(dataBearingToken);
284+
assertTrue(Helpers.verifyTokenSignature(defaultToken, apiSecret));
285+
assertTrue(Helpers.verifyTokenSignature(dataBearingToken, apiSecret));
286+
287+
Map<String, String> defaultTokenData = Helpers.decodeToken(defaultToken);
288+
assertNull(defaultTokenData.get("connection_data"));
289+
Map<String, String> dataBearingTokenData = Helpers.decodeToken(dataBearingToken);
290+
assertEquals(actualData, dataBearingTokenData.get("connection_data"));
291+
assertEquals(InvalidArgumentException.class, tooLongException.getClass());
292+
}
293+
322294
//
323295
// @Test
324296
// public void testRoleGarbageInput() {

0 commit comments

Comments
 (0)