1 | var graph = require("../index")
|
2 | , FBConfig = require("./config").facebook
|
3 | , vows = require("vows")
|
4 | , events = require("events")
|
5 | , assert = require("assert");
|
6 |
|
7 |
|
8 | var testUser1 = {}
|
9 | , appAccessToken = FBConfig.appId + "|" + FBConfig.appSecret
|
10 | , testUserParams = {
|
11 | installed: true
|
12 | , name: "Ricky Bobby"
|
13 | , permissions: FBConfig.scope
|
14 | , method: "post"
|
15 | , access_token: appAccessToken
|
16 | };
|
17 |
|
18 |
|
19 | vows.describe("graph.test").addBatch({
|
20 | "Before starting a test suite": {
|
21 | topic: function () {
|
22 | return graph.setAccessToken(null);
|
23 | },
|
24 |
|
25 | "*Access Token* should be null": function (graph) {
|
26 | assert.isNull(graph.getAccessToken());
|
27 | },
|
28 |
|
29 | "should be able to set *request* options": function (graph) {
|
30 | var options = {
|
31 | timeout: 30000
|
32 | , pool: false
|
33 | , headers: { connection: "keep-alive" }
|
34 | };
|
35 |
|
36 | graph.setOptions(options);
|
37 | assert.equal(graph.getOptions(), options);
|
38 |
|
39 |
|
40 | graph.setOptions({});
|
41 | }
|
42 | }
|
43 | }).addBatch({
|
44 | "When accessing the graphApi": {
|
45 | "with no *Access Token** ": {
|
46 | "and searching for public data via username": {
|
47 | topic: function() {
|
48 | graph.get("/btaylor", this.callback);
|
49 | },
|
50 |
|
51 | "should get public data": function (err, res) {
|
52 | assert.include(res, "username");
|
53 | assert.include(res, "name");
|
54 | assert.include(res, "first_name");
|
55 | assert.include(res, "last_name");
|
56 | }
|
57 | },
|
58 |
|
59 | "and requesting an url for a user that does not exist": {
|
60 | topic: function () {
|
61 | graph.get("/thisUserNameShouldNotExist", this.callback);
|
62 | },
|
63 |
|
64 | "should return an error": function (err, res) {
|
65 | assert.include(res, "error");
|
66 | }
|
67 | },
|
68 |
|
69 | "and not using a string as an api url": {
|
70 | topic: function () {
|
71 | graph.get({ you: "shall not pass" }, this.callback);
|
72 | },
|
73 |
|
74 | "should return an api must be a string error": function (err, res) {
|
75 | assert.equal(err.message, "Graph api url must be a string",
|
76 | "Should return an error if api url is not a string");
|
77 | }
|
78 | },
|
79 |
|
80 | "and requesting a public profile picture": {
|
81 | topic: function () {
|
82 | graph.get("/zuck/picture", this.callback);
|
83 | },
|
84 |
|
85 | "should get an image and return a json with its location ": function (err, res) {
|
86 | assert.include(res, "image");
|
87 | assert.include(res, "location");
|
88 | }
|
89 | },
|
90 |
|
91 | "and requesting an api url with a missing slash": {
|
92 | topic: function () {
|
93 | graph.get("zuck/picture", this.callback);
|
94 | },
|
95 |
|
96 | "should be able to get valid data": function (err, res) {
|
97 | assert.include(res, "image");
|
98 | assert.include(res, "location");
|
99 | }
|
100 | },
|
101 |
|
102 | "and requesting an api url with prefixed graphurl": {
|
103 | topic: function() {
|
104 | graph.get(graph.getGraphUrl() + "/zuck/picture", this.callback);
|
105 | },
|
106 |
|
107 | "should be able to get valid data": function (err, res) {
|
108 | assert.include(res, "image");
|
109 | assert.include(res, "location");
|
110 | }
|
111 | },
|
112 |
|
113 | "and trying to access data that requires an access token": {
|
114 | topic: function () {
|
115 | graph.get("/817129783203", this.callback);
|
116 | },
|
117 |
|
118 | "should return an OAuthException error": function (err, res) {
|
119 | assert.include(res, "error");
|
120 | assert.equal(res.error.type, "OAuthException",
|
121 | "Response from facebook should be an OAuthException");
|
122 | }
|
123 | },
|
124 |
|
125 | "and performing a public search ": {
|
126 | topic: function () {
|
127 | graph.search({ q: "watermelon", type: "post" }, this.callback);
|
128 | },
|
129 |
|
130 | "should return valid data": function (err, res) {
|
131 | assert.isNotNull(res);
|
132 | assert.isArray(res.data);
|
133 | assert.ok(res.data.length > 1, "response data should not be empty");
|
134 | }
|
135 | },
|
136 |
|
137 | },
|
138 |
|
139 | "with an *Access Token* ": {
|
140 | topic: function () {
|
141 | var promise = new events.EventEmitter();
|
142 |
|
143 |
|
144 | var testUserUrl = FBConfig.appId + "/accounts/test-users";
|
145 |
|
146 | graph.get(testUserUrl, testUserParams, function(err, res) {
|
147 |
|
148 | if (!res || res.error
|
149 | && ~res.error.message.indexOf("Service temporarily unavailable")) {
|
150 |
|
151 | promise.emit("error", err);
|
152 | console.error("Can't retreive access token from facebook\n" +
|
153 | "Try again in a few minutes");
|
154 | } else {
|
155 |
|
156 | graph.setAccessToken(res.access_token);
|
157 | testUser1 = res;
|
158 | promise.emit("success", res);
|
159 | }
|
160 | });
|
161 |
|
162 | return promise;
|
163 | },
|
164 |
|
165 |
|
166 |
|
167 | "result *keys* should be valid": function(err, res) {
|
168 | assert.isNull(err);
|
169 | assert.include(res, "id");
|
170 | assert.include(res, "access_token");
|
171 | assert.include(res, "login_url");
|
172 | assert.include(res, "email");
|
173 | assert.include(res, "password");
|
174 | },
|
175 |
|
176 | "and getting data from a protected page": {
|
177 | topic: function () {
|
178 | graph.get("/817129783203", this.callback);
|
179 | },
|
180 |
|
181 | "response should be valid": function(err, res) {
|
182 | assert.isNull(err);
|
183 | assert.equal("817129783203", res.id, "response id should be valid");
|
184 | }
|
185 | },
|
186 |
|
187 | "and getting a user permissions": {
|
188 | topic: function () {
|
189 | graph.get("/me/permissions", this.callback);
|
190 | },
|
191 |
|
192 | "test user should have proper permissions": function (err, res) {
|
193 | assert.isNull(err);
|
194 |
|
195 | var permissions = FBConfig.scope
|
196 | .replace(/ /g,"")
|
197 | .split(",");
|
198 |
|
199 | permissions.push("installed");
|
200 |
|
201 | permissions.forEach(function(key) {
|
202 | assert.include(res.data[0], key);
|
203 | });
|
204 | }
|
205 | },
|
206 |
|
207 | "and performing a search": {
|
208 | topic: function () {
|
209 | var searchOptions = {
|
210 | q: "coffee"
|
211 | , type: "place"
|
212 | , center: "37.76,-122.427"
|
213 | , distance: 1000
|
214 | };
|
215 |
|
216 | graph.search(searchOptions, this.callback);
|
217 | },
|
218 |
|
219 | "an *Access Token* required search should return valid data": function (err, res) {
|
220 | assert.isNull(err);
|
221 | assert.ok(res.data.length > 1, "response data should not be empty");
|
222 | }
|
223 | },
|
224 |
|
225 | "and requesting a FQL query": {
|
226 | topic: function () {
|
227 | var query = "SELECT name FROM user WHERE uid = me()";
|
228 |
|
229 | graph.fql(query, this.callback);
|
230 | },
|
231 |
|
232 | "should return valid data": function (err, res) {
|
233 | assert.isNull(err);
|
234 | assert.include(res, 'data');
|
235 | assert.isArray(res.data);
|
236 | assert.equal(res.data[0].name, testUserParams.name);
|
237 | }
|
238 | },
|
239 |
|
240 | "and requesting a FQL multi-query": {
|
241 | topic: function () {
|
242 | var query = {
|
243 | name: "SELECT name FROM user WHERE uid = me()"
|
244 | , permissions: "SELECT " + FBConfig.scope + " FROM permissions WHERE uid = me()"
|
245 | };
|
246 |
|
247 | graph.fql(query, this.callback);
|
248 | },
|
249 |
|
250 | "should return valid data": function (err, res) {
|
251 | assert.isNull(err);
|
252 | assert.include(res, 'data');
|
253 | assert.isArray(res.data);
|
254 |
|
255 | var nameQuery = {}
|
256 | , permsQuery = {};
|
257 |
|
258 | if (res.data[0].name === 'name') {
|
259 | nameQuery = res.data[0];
|
260 | permsQuery = res.data[1];
|
261 | } else {
|
262 | permsQuery = res.data[0];
|
263 | nameQuery = res.data[1];
|
264 | }
|
265 |
|
266 | assert.isArray(nameQuery.fql_result_set);
|
267 | assert.isArray(permsQuery.fql_result_set);
|
268 | assert.equal(nameQuery.fql_result_set[0].name, testUserParams.name);
|
269 |
|
270 | console.dir(permsQuery.fql_result_set);
|
271 | var permissions = permsQuery.fql_result_set[0];
|
272 |
|
273 | testUserParams.permissions.split(', ').forEach(function(permission) {
|
274 | assert.include(permissions, permission);
|
275 | });
|
276 | }
|
277 | }
|
278 | }
|
279 | }
|
280 | }).addBatch({
|
281 | "When tests are over": {
|
282 | topic: function () {
|
283 | graph.del(testUser1.id, this.callback);
|
284 | },
|
285 |
|
286 | "test users should be removed": function(res){
|
287 | assert.equal(res.data, "true");
|
288 | }
|
289 | }
|
290 | }).export(module);
|