agora-token 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ /**
2
+ * run this test with command:
3
+ * nodeunit test/FpaTokenBuilderTest.js
4
+ * see https://github.com/caolan/nodeunit
5
+ */
6
+ const FpaTokenBuilder = require('../src/FpaTokenBuilder').FpaTokenBuilder
7
+ const {AccessToken2, ServiceFpa, kFpaServiceType} = require('../src/AccessToken2')
8
+
9
+ const appId = "970CA35de60c44645bbae8a215061b33"
10
+ const appCertificate = "5CFd2fd1755d40ecb72977518be15d3b"
11
+ const expire = 24 * 3600
12
+
13
+ exports.buildToken_Test = function (test) {
14
+ let token = FpaTokenBuilder.buildToken(appId, appCertificate)
15
+ let accessToken = new AccessToken2('', '', 0, 0)
16
+ accessToken.from_string(token)
17
+
18
+ test.equal(appId, accessToken.appId)
19
+ test.equal(expire, accessToken.expire)
20
+ test.equal(0, accessToken.services[kFpaServiceType].__privileges[ServiceFpa.kPrivilegeLogin])
21
+ test.done()
22
+ }
@@ -0,0 +1,140 @@
1
+ /**
2
+ * run this test with command:
3
+ * nodeunit test/RtcTokenBuilder2Test.js
4
+ * see https://github.com/caolan/nodeunit
5
+ */
6
+ const RtcTokenBuilder = require("../src/RtcTokenBuilder2").RtcTokenBuilder;
7
+ const Role = require("../src/RtcTokenBuilder2").Role;
8
+ const { AccessToken2, ServiceRtc, kRtcServiceType } = require("../src/AccessToken2");
9
+
10
+ const appId = "970CA35de60c44645bbae8a215061b33";
11
+ const appCertificate = "5CFd2fd1755d40ecb72977518be15d3b";
12
+ const channelName = "7d72365eb983485397e3e3f9d460bdda";
13
+ const uid = 2882341273;
14
+ const uidStr = "2882341273";
15
+ const expire = 600;
16
+
17
+ const tokenExpirationInSecond = 600;
18
+ const privilegeExpirationInSecond = 600;
19
+
20
+ exports.buildTokenWithUid_SUBSCRIBER_Test = function (test) {
21
+ let token = RtcTokenBuilder.buildTokenWithUid(
22
+ appId,
23
+ appCertificate,
24
+ channelName,
25
+ uid,
26
+ Role.SUBSCRIBER,
27
+ tokenExpirationInSecond,
28
+ privilegeExpirationInSecond
29
+ );
30
+ let accessToken = new AccessToken2("", "", 0, 0);
31
+ accessToken.from_string(token);
32
+
33
+ test.equal(appId, accessToken.appId);
34
+ test.equal(expire, accessToken.expire);
35
+ test.equal(channelName, accessToken.services[kRtcServiceType].__channel_name);
36
+ test.equal(uidStr, accessToken.services[kRtcServiceType].__uid);
37
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegeJoinChannel]);
38
+ test.done();
39
+ };
40
+
41
+ exports.buildTokenWithUid_PUBLISHER_Test = function (test) {
42
+ let token = RtcTokenBuilder.buildTokenWithUid(
43
+ appId,
44
+ appCertificate,
45
+ channelName,
46
+ uid,
47
+ Role.PUBLISHER,
48
+ tokenExpirationInSecond,
49
+ privilegeExpirationInSecond
50
+ );
51
+ let accessToken = new AccessToken2("", "", 0, 0);
52
+ accessToken.from_string(token);
53
+
54
+ test.equal(appId, accessToken.appId);
55
+ test.equal(expire, accessToken.expire);
56
+ test.equal(channelName, accessToken.services[kRtcServiceType].__channel_name);
57
+ test.equal(uidStr, accessToken.services[kRtcServiceType].__uid);
58
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegeJoinChannel]);
59
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegePublishAudioStream]);
60
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegePublishVideoStream]);
61
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegePublishDataStream]);
62
+ test.done();
63
+ };
64
+
65
+ exports.buildTokenWithUserAccount_SUBSCRIBER_Test = function (test) {
66
+ let token = RtcTokenBuilder.buildTokenWithUserAccount(
67
+ appId,
68
+ appCertificate,
69
+ channelName,
70
+ uidStr,
71
+ Role.SUBSCRIBER,
72
+ tokenExpirationInSecond,
73
+ privilegeExpirationInSecond
74
+ );
75
+ let accessToken = new AccessToken2("", "", 0, 0);
76
+ accessToken.from_string(token);
77
+
78
+ test.equal(appId, accessToken.appId);
79
+ test.equal(expire, accessToken.expire);
80
+ test.equal(channelName, accessToken.services[kRtcServiceType].__channel_name);
81
+ test.equal(uidStr, accessToken.services[kRtcServiceType].__uid);
82
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegeJoinChannel]);
83
+ test.done();
84
+ };
85
+
86
+ exports.buildTokenWithUserAccount_PUBLISHER_Test = function (test) {
87
+ let token = RtcTokenBuilder.buildTokenWithUserAccount(
88
+ appId,
89
+ appCertificate,
90
+ channelName,
91
+ uid,
92
+ Role.PUBLISHER,
93
+ tokenExpirationInSecond,
94
+ privilegeExpirationInSecond
95
+ );
96
+ let accessToken = new AccessToken2("", "", 0, 0);
97
+ accessToken.from_string(token);
98
+
99
+ test.equal(appId, accessToken.appId);
100
+ test.equal(expire, accessToken.expire);
101
+ test.equal(channelName, accessToken.services[kRtcServiceType].__channel_name);
102
+ test.equal(uidStr, accessToken.services[kRtcServiceType].__uid);
103
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegeJoinChannel]);
104
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegePublishAudioStream]);
105
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegePublishVideoStream]);
106
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegePublishDataStream]);
107
+ test.done();
108
+ };
109
+
110
+ exports.buildTokenWithUidAndPrivilege_Test = function (test) {
111
+ let token = RtcTokenBuilder.buildTokenWithUidAndPrivilege(appId, appCertificate, channelName, uid, expire, expire, expire, expire, expire);
112
+ let accessToken = new AccessToken2("", "", 0, 0);
113
+ accessToken.from_string(token);
114
+
115
+ test.equal(appId, accessToken.appId);
116
+ test.equal(expire, accessToken.expire);
117
+ test.equal(channelName, accessToken.services[kRtcServiceType].__channel_name);
118
+ test.equal(uidStr, accessToken.services[kRtcServiceType].__uid);
119
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegeJoinChannel]);
120
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegePublishAudioStream]);
121
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegePublishVideoStream]);
122
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegePublishDataStream]);
123
+ test.done();
124
+ };
125
+
126
+ exports.BuildTokenWithUserAccountAndPrivilege_Test = function (test) {
127
+ let token = RtcTokenBuilder.BuildTokenWithUserAccountAndPrivilege(appId, appCertificate, channelName, uidStr, expire, expire, expire, expire, expire);
128
+ let accessToken = new AccessToken2("", "", 0, 0);
129
+ accessToken.from_string(token);
130
+
131
+ test.equal(appId, accessToken.appId);
132
+ test.equal(expire, accessToken.expire);
133
+ test.equal(channelName, accessToken.services[kRtcServiceType].__channel_name);
134
+ test.equal(uidStr, accessToken.services[kRtcServiceType].__uid);
135
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegeJoinChannel]);
136
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegePublishAudioStream]);
137
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegePublishVideoStream]);
138
+ test.equal(expire, accessToken.services[kRtcServiceType].__privileges[ServiceRtc.kPrivilegePublishDataStream]);
139
+ test.done();
140
+ };
@@ -0,0 +1,24 @@
1
+ /**
2
+ * run this test with command:
3
+ * nodeunit test/RtmTokenBuilder2Test.js
4
+ * see https://github.com/caolan/nodeunit
5
+ */
6
+ const RtmTokenBuilder = require("../src/RtmTokenBuilder2").RtmTokenBuilder;
7
+ const { AccessToken2, ServiceRtm, kRtmServiceType } = require("../src/AccessToken2");
8
+
9
+ const appId = "970CA35de60c44645bbae8a215061b33";
10
+ const appCertificate = "5CFd2fd1755d40ecb72977518be15d3b";
11
+ const userId = "test_user";
12
+ const expire = 600;
13
+
14
+ exports.buildToken = function (test) {
15
+ let token = RtmTokenBuilder.buildToken(appId, appCertificate, userId, expire);
16
+ let accessToken = new AccessToken2("", "", 0, 0);
17
+ accessToken.from_string(token);
18
+
19
+ test.equal(appId, accessToken.appId);
20
+ test.equal(expire, accessToken.expire);
21
+ test.equal(userId, accessToken.services[kRtmServiceType].__user_id);
22
+ test.equal(expire, accessToken.services[kRtmServiceType].__privileges[ServiceRtm.kPrivilegeLogin]);
23
+ test.done();
24
+ };
@@ -0,0 +1,24 @@
1
+ /**
2
+ * run this test with command:
3
+ * nodeunit AccessTokenTest.js
4
+ * see https://github.com/caolan/nodeunit
5
+ */
6
+ const AccessToken = require("../src/AccessToken").AccessToken;
7
+ const RtmTokenBuilder = require("../src/RtmTokenBuilder").RtmTokenBuilder;
8
+ const Role = require("../src/RtmTokenBuilder").Role;
9
+ const Priviledges = require("../src/AccessToken").priviledges;
10
+
11
+ const appID = "970CA35de60c44645bbae8a215061b33";
12
+ const appCertificate = "5CFd2fd1755d40ecb72977518be15d3b";
13
+ const account = "test_user";
14
+ const expireTimestamp = 1446455471;
15
+
16
+ exports.RtmToken_Test = function (test) {
17
+ const token = RtmTokenBuilder.buildToken(appID, appCertificate, account, Role.Rtm_User, expireTimestamp);
18
+ let accessToken = new AccessToken("", "", 0, 0);
19
+ accessToken.fromString(token);
20
+
21
+ test.equal(appID, accessToken.appID);
22
+ test.equal(expireTimestamp, accessToken.messages[Priviledges.kRtmLogin]);
23
+ test.done();
24
+ };