/
public_test.go
126 lines (105 loc) · 3.25 KB
/
public_test.go
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
package twitter
import (
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/sensorbee/sensorbee.v0/data"
"io/ioutil"
"os"
"testing"
)
func TestKeyParameters(t *testing.T) {
Convey("Given api key parameters", t, func() {
params, err := data.NewMap(map[string]interface{}{
"consumer_key": "abc",
"consumer_secret": "def",
"access_token": "ghi",
"access_token_secret": "jkl",
})
So(err, ShouldBeNil)
Convey("when creating apiKey", func() {
keys, err := getKeyParameters(params)
So(err, ShouldBeNil)
Convey("consumer_key should have the correct value", func() {
So(keys.ConsumerKey, ShouldEqual, "abc")
})
Convey("consumer_secret should have the correct value", func() {
So(keys.ConsumerSecret, ShouldEqual, "def")
})
Convey("access_token should have the correct value", func() {
So(keys.AccessToken, ShouldEqual, "ghi")
})
Convey("access_token_secret should have the correct value", func() {
So(keys.AccessTokenSecret, ShouldEqual, "jkl")
})
})
Convey("when creating a source from it", func() {
// kind of a white-box testing; ctx and ioParams aren't used in func
_, err := CreatePublicStreamSource(nil, nil, params)
Convey("it should succeed", func() {
So(err, ShouldBeNil)
})
})
})
}
func TestKeyFile(t *testing.T) {
tempPath := func() string {
f, err := ioutil.TempFile("", "twitter-plugin-test-key-file")
if err != nil {
t.Fatal("Cannot create a temp file:", err)
}
defer f.Close()
_, err = f.WriteString(`consumer_key: abc
consumer_secret: def
access_token: ghi
access_token_secret: jkl`)
if err != nil {
t.Fatal("Cannot write a key information to the temp file:", err)
}
return f.Name()
}()
defer os.Remove(tempPath)
Convey("Given an api key file", t, func() {
params := data.Map{
"key_file": data.String(tempPath),
}
Convey("when creating apiKey", func() {
keys, err := getKeyParameters(params)
So(err, ShouldBeNil)
Convey("consumer_key should have the correct value", func() {
So(keys.ConsumerKey, ShouldEqual, "abc")
})
Convey("consumer_secret should have the correct value", func() {
So(keys.ConsumerSecret, ShouldEqual, "def")
})
Convey("access_token should have the correct value", func() {
So(keys.AccessToken, ShouldEqual, "ghi")
})
Convey("access_token_secret should have the correct value", func() {
So(keys.AccessTokenSecret, ShouldEqual, "jkl")
})
})
Convey("when creating a source from it", func() {
_, err := CreatePublicStreamSource(nil, nil, params)
Convey("it should succeed", func() {
So(err, ShouldBeNil)
})
})
Convey("when creating apiKey with other key parameters", func() {
params, err := data.NewMap(map[string]interface{}{
"key_file": tempPath,
"consumer_key": "_abc",
"consumer_secret": "_def",
"access_token": "_ghi",
"access_token_secret": "_jkl",
})
So(err, ShouldBeNil)
keys, err := getKeyParameters(params)
So(err, ShouldBeNil)
Convey("key_file parameter should be preferred", func() {
So(keys.ConsumerKey, ShouldEqual, "abc")
So(keys.ConsumerSecret, ShouldEqual, "def")
So(keys.AccessToken, ShouldEqual, "ghi")
So(keys.AccessTokenSecret, ShouldEqual, "jkl")
})
})
})
}