This repository has been archived by the owner on Sep 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
spec_twitter.js
154 lines (151 loc) · 5.52 KB
/
spec_twitter.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var assert = require('assert'),
fs = require('fs'),
EventEmitter = require('events').EventEmitter,
util = require('util');
var vows = require('vows');
var Twitter = require('../lib/twitter').Twitter;
var auth = JSON.parse(fs.readFileSync(__dirname + '/auth.json'));
var MockStreamServer = require('./mock/stream').MockStreamServer;
function ts(){
return ((new Date()).getTime());
}
vows.describe('node-twbot/twitter api test')
.addBatch({
'Twitter Client' : {
'topic' : function(){
var client = new Twitter(auth.consumerKey, auth.consumerSecret,
{streamUrl: 'http://localhost:10080'});
client.accessKey = auth.accessKey;
client.accessSecret = auth.accessSecret;
return client;
},
'when updates a text.' : {
'topic': function(client){
var text = ts() + ': test tweet';
client.update(text, this.callback);
},
'should returns a tweet object' : function(err, data, response){
assert.isNull(err);
assert.isObject(data);
assert.isObject(data.user);
assert.ok(data.text);
}
},
'when updates a text over 140 chars' : {
'topic' : function(client){
var text = ts().toString();
var padding = 140 - text.length;
for(var i=0; i<padding; i++){
text = text + '*';
}
text += "this is truncated.";
client.update(text, this.callback);
},
'should truncate the text' : function(err, data, response){
assert.isNull(err);
assert.isObject(data);
assert.isObject(data.user);
assert.ok(data.text);
assert.equal(data.text.length, 140);
assert.equal(data.text.substr(137), '...');
}
},
'when gets timeline' : {
'without parameters' : {
'topic': function(client){
client.getTimeline(this.callback);
},
'should returns home timeline' : function(err, data, response){
// TODO: how to test the data is 'home' or not.
assert.isNull(err);
assert.isArray(data);
}
},
'with string parameters' : {
'topic': function(client){
client.getTimeline('public', this.callback);
},
'should returns the specified timeline' : function(err, data, response){
// TODO: how to test the data is specified timeline or not.
assert.isNull(err);
assert.isArray(data);
}
},
'with parameters' : {
'topic': function(client){
client.getTimeline({
type: 'user',
user: 'yssk22'
}, this.callback);
},
'should returns the specified timeline' : function(err, data, response){
// TODO: how to test the data is specified timeline or not.
assert.isNull(err);
assert.isArray(data);
}
},
'with wrong parameter' : {
'topic': function(client){
client.getTimeline({
type: 'user',
user: 'nonexistent'
}, this.callback);
},
'should returns an error' : function(err, data, response){
assert.isObject(err);
assert.isUndefined(data);
}
}
}
},
'UserStream' : {
'topic': function(){
var server = new MockStreamServer(
[{friends: [123456]}]
);;
server.listen(10080);
var client = new Twitter(auth.consumerKey, auth.consumerSecret,
{streamUrl: 'http://localhost:10080'});
client.accessKey = auth.accessKey;
client.accessSecret = auth.accessSecret;
return {
client: client,
stream: client.openUserStream(),
server: server
};
},
'events' : {
'topic': function(topic){
var client = topic.client;
var server = topic.server;
var stream = topic.stream;
var self = this;
var result = {};
stream.on('data', function(data){
if( !result.received ){
result.received = [];
}
result.received.push(data);
});
stream.on('error', function(err){
self.callback(err);
});
stream.on('end', function(){
result.end = true;
self.callback(null, result);
});
},
'should have end event': function(result){
assert.ok(result.end);
},
'should receive streamed data' : function(result){
assert.isArray(result.received);
assert.length(result.received, 1);
},
'teardown': function(_, topic){
topic.stream.end();
topic.server.close();
}
}
}
}).export(module);