PageRenderTime 28ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/test/user-stream-api-read-e2e-test.js

https://gitlab.com/pump.io/pump.io
JavaScript | 292 lines | 262 code | 12 blank | 18 comment | 4 complexity | a81b7a18164b172bd3ebb010c998ef8a MD5 | raw file
  1. // user-stream-api-e2e-test.js
  2. //
  3. // Test user streams API
  4. //
  5. // Copyright 2012, E14N https://e14n.com/
  6. //
  7. // Licensed under the Apache License, Version 2.0 (the "License");
  8. // you may not use this file except in compliance with the License.
  9. // You may obtain a copy of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS,
  15. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. // See the License for the specific language governing permissions and
  17. // limitations under the License.
  18. "use strict";
  19. var assert = require("assert"),
  20. vows = require("vows"),
  21. Step = require("step"),
  22. _ = require("lodash"),
  23. querystring = require("querystring"),
  24. http = require("http"),
  25. OAuth = require("oauth-evanp").OAuth,
  26. Browser = require("zombie"),
  27. httputil = require("./lib/http"),
  28. oauthutil = require("./lib/oauth"),
  29. apputil = require("./lib/app"),
  30. withAppSetup = apputil.withAppSetup,
  31. register = oauthutil.register,
  32. newCredentials = oauthutil.newCredentials;
  33. var ignore = function(err) {};
  34. var suite = vows.describe("User stream API test");
  35. var sizeFeed = function(endpoint, size) {
  36. return {
  37. topic: function(cred) {
  38. var full = "http://localhost:4815" + endpoint,
  39. callback = this.callback;
  40. httputil.getJSON(full, cred, callback);
  41. },
  42. "it works": function(err, feed, resp) {
  43. assert.ifError(err);
  44. },
  45. "it looks like a feed": function(err, feed, resp) {
  46. assert.ifError(err);
  47. assert.isObject(feed);
  48. assert.include(feed, "totalItems");
  49. assert.include(feed, "items");
  50. },
  51. "it is empty": function(err, feed, resp) {
  52. assert.ifError(err);
  53. assert.isObject(feed);
  54. assert.include(feed, "totalItems");
  55. assert.equal(feed.totalItems, size);
  56. assert.include(feed, "items");
  57. assert.isArray(feed.items);
  58. assert.equal(feed.items.length, size);
  59. }
  60. };
  61. };
  62. var emptyFeed = function(endpoint) {
  63. return {
  64. topic: function(cred) {
  65. var full = "http://localhost:4815" + endpoint,
  66. callback = this.callback;
  67. httputil.getJSON(full, cred, callback);
  68. },
  69. "it works": function(err, feed, resp) {
  70. assert.ifError(err);
  71. },
  72. "it looks like a feed": function(err, feed, resp) {
  73. assert.ifError(err);
  74. assert.isObject(feed);
  75. assert.include(feed, "totalItems");
  76. assert.include(feed, "items");
  77. },
  78. "it is empty": function(err, feed, resp) {
  79. assert.ifError(err);
  80. assert.isObject(feed);
  81. assert.include(feed, "totalItems");
  82. assert.equal(feed.totalItems, 0);
  83. assert.include(feed, "items");
  84. assert.isEmpty(feed.items);
  85. }
  86. };
  87. };
  88. // A batch for testing the read access to the API
  89. suite.addBatch(
  90. withAppSetup({
  91. "and we get new credentials": {
  92. topic: function() {
  93. newCredentials("bigredchicken", "bokbokbok!", this.callback);
  94. },
  95. "it works": function(err, cred) {
  96. assert.ifError(err);
  97. assert.isObject(cred);
  98. assert.isString(cred.consumer_key);
  99. assert.isString(cred.consumer_secret);
  100. assert.isString(cred.token);
  101. assert.isString(cred.token_secret);
  102. },
  103. "and we check the feed endpoint":
  104. httputil.endpoint("/api/user/bigredchicken/feed", ["GET", "POST"]),
  105. "and we check the minor feed endpoint":
  106. httputil.endpoint("/api/user/bigredchicken/feed/minor", ["GET"]),
  107. "and we check the major feed endpoint":
  108. httputil.endpoint("/api/user/bigredchicken/feed/major", ["GET"]),
  109. "and we check the inbox endpoint":
  110. httputil.endpoint("/api/user/bigredchicken/inbox", ["GET", "POST"]),
  111. "and we check the minor inbox endpoint":
  112. httputil.endpoint("/api/user/bigredchicken/inbox/minor", ["GET"]),
  113. "and we check the major inbox endpoint":
  114. httputil.endpoint("/api/user/bigredchicken/inbox/major", ["GET"]),
  115. "and we check the direct inbox endpoint":
  116. httputil.endpoint("/api/user/bigredchicken/inbox/direct", ["GET"]),
  117. "and we check the direct minor inbox endpoint":
  118. httputil.endpoint("/api/user/bigredchicken/inbox/direct/minor", ["GET"]),
  119. "and we check the direct major inbox endpoint":
  120. httputil.endpoint("/api/user/bigredchicken/inbox/direct/major", ["GET"]),
  121. "and we get the feed of a new user":
  122. sizeFeed("/api/user/bigredchicken/feed", 5),
  123. "and we get the minor feed of a new user":
  124. sizeFeed("/api/user/bigredchicken/feed/minor", 5),
  125. "and we get the major feed of a new user":
  126. emptyFeed("/api/user/bigredchicken/feed/major"),
  127. "and we get the inbox of a new user":
  128. sizeFeed("/api/user/bigredchicken/inbox", 6),
  129. "and we get the minor inbox of a new user":
  130. sizeFeed("/api/user/bigredchicken/inbox/minor", 5),
  131. "and we get the major inbox of a new user":
  132. sizeFeed("/api/user/bigredchicken/inbox/major", 1),
  133. "and we get the direct inbox of a new user":
  134. sizeFeed("/api/user/bigredchicken/inbox/direct", 1),
  135. "and we get the direct minor inbox of a new user":
  136. emptyFeed("/api/user/bigredchicken/inbox/direct/minor"),
  137. "and we get the direct major inbox of a new user":
  138. sizeFeed("/api/user/bigredchicken/inbox/direct/major", 1)
  139. },
  140. "and we get more new credentials": {
  141. topic: function() {
  142. newCredentials("dora", "v4m0nos!", this.callback);
  143. },
  144. "it works": function(err, cred) {
  145. assert.ifError(err);
  146. assert.isObject(cred);
  147. assert.isString(cred.consumer_key);
  148. assert.isString(cred.consumer_secret);
  149. assert.isString(cred.token);
  150. assert.isString(cred.token_secret);
  151. },
  152. "and we post a new activity": {
  153. topic: function(cred) {
  154. var cb = this.callback,
  155. act = {
  156. verb: "post",
  157. object: {
  158. objectType: "note",
  159. content: "Hello, world!"
  160. }
  161. };
  162. httputil.postJSON("http://localhost:4815/api/user/dora/feed", cred, act, function(err, act, result) {
  163. cb(err, act);
  164. });
  165. },
  166. "it works": function(err, act) {
  167. assert.ifError(err);
  168. },
  169. "results look right": function(err, act) {
  170. assert.isObject(act);
  171. assert.include(act, "id");
  172. assert.isString(act.id);
  173. assert.include(act, "actor");
  174. assert.isObject(act.actor);
  175. assert.include(act.actor, "id");
  176. assert.isString(act.actor.id);
  177. assert.include(act, "verb");
  178. assert.isString(act.verb);
  179. assert.include(act, "object");
  180. assert.isObject(act.object);
  181. assert.include(act.object, "id");
  182. assert.isString(act.object.id);
  183. assert.include(act, "published");
  184. assert.isString(act.published);
  185. assert.include(act, "updated");
  186. assert.isString(act.updated);
  187. },
  188. "and we read the feed": {
  189. topic: function(act, cred) {
  190. var cb = this.callback;
  191. httputil.getJSON("http://localhost:4815/api/user/dora/feed", cred, function(err, newf) {
  192. if (err) {
  193. cb(err);
  194. } else {
  195. cb(null, {act: act, feed: newf});
  196. }
  197. });
  198. },
  199. "it works": function(err, res) {
  200. assert.ifError(err);
  201. },
  202. "it has the right members": function(err, res) {
  203. assert.isObject(res);
  204. assert.include(res, "feed");
  205. var feed = res.feed;
  206. assert.include(feed, "author");
  207. assert.include(feed.author, "id");
  208. assert.include(feed.author, "displayName");
  209. assert.include(feed.author, "objectType");
  210. assert.include(feed, "totalItems");
  211. assert.include(feed, "items");
  212. assert.include(feed, "displayName");
  213. assert.include(feed, "url");
  214. assert.include(feed, "objectTypes");
  215. assert.include(feed.objectTypes, "activity");
  216. },
  217. "it has six objects": function(err, res) {
  218. assert.isObject(res);
  219. assert.include(res, "feed");
  220. var feed = res.feed;
  221. assert.equal(feed.totalItems, 6);
  222. assert.equal(feed.items.length, 6);
  223. },
  224. "it has our activity": function(err, res) {
  225. assert.isObject(res);
  226. assert.include(res, "feed");
  227. assert.include(res, "act");
  228. var feed = res.feed, act = res.act;
  229. assert.equal(feed.items[0].id, act.id);
  230. }
  231. },
  232. "and we read the inbox": {
  233. topic: function(act, cred) {
  234. var cb = this.callback;
  235. httputil.getJSON("http://localhost:4815/api/user/dora/inbox", cred, function(err, newb) {
  236. if (err) {
  237. cb(err);
  238. } else {
  239. cb(null, {act: act, inbox: newb});
  240. }
  241. });
  242. },
  243. "it works": function(err, res) {
  244. assert.ifError(err);
  245. },
  246. "it has the right members": function(err, res) {
  247. assert.isObject(res);
  248. assert.include(res, "inbox");
  249. var inbox = res.inbox;
  250. assert.include(inbox, "author");
  251. assert.include(inbox.author, "id");
  252. assert.include(inbox.author, "displayName");
  253. assert.include(inbox.author, "objectType");
  254. assert.include(inbox, "totalItems");
  255. assert.include(inbox, "items");
  256. assert.include(inbox, "displayName");
  257. assert.include(inbox, "url");
  258. assert.include(inbox, "objectTypes");
  259. assert.include(inbox.objectTypes, "activity");
  260. },
  261. "it has seven items": function(err, res) {
  262. assert.isObject(res);
  263. assert.include(res, "inbox");
  264. var inbox = res.inbox;
  265. assert.equal(inbox.totalItems, 7);
  266. assert.equal(inbox.items.length, 7);
  267. },
  268. "it has our activity": function(err, res) {
  269. assert.isObject(res);
  270. assert.include(res, "inbox");
  271. assert.include(res, "act");
  272. var inbox = res.inbox, act = res.act;
  273. assert.equal(inbox.items[0].id, act.id);
  274. }
  275. }
  276. }
  277. }
  278. })
  279. );
  280. suite["export"](module);