-
Notifications
You must be signed in to change notification settings - Fork 0
/
changeorg_test.go
99 lines (93 loc) · 2.56 KB
/
changeorg_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
package gochange
// Imports required packages.
import (
"log"
"testing"
)
// Creates map for application parameters.
var params map[string]string
// Initializes the parameters.
func init() {
params = make(map[string]string)
// sets API key
params["API_KEY"] = "my_api_key"
// sets secret token
params["SECRET"] = "my_secret_token"
// sets url for the petition
params["URL"] = "https://www.change.org/p/sample-for-developers-using-change-org-api-sign-petition-via-change-org-api"
// sets petition id
params["PETITION_ID"] = "2268806"
// sets authorization key for petition with id `2268806`
params["AUTH_KEY"] = "auth_key_for_petition"
}
// TestID tests GetPetitionId function.
func TestID(t *testing.T) {
log.Println("Welcome to gochange!")
change_org := NewChangeOrgClient(params["API_KEY"])
id, err := change_org.GetPetitionId(PetitionIdArgs{
PetitionURL: params["URL"],
})
if err != nil {
t.Fatal(err)
}
log.Println("Petition Id:", *id)
}
// TestAuthKey tests GetAuthKey function.
func TestAuthKey(t *testing.T) {
log.Println("Requesting Auth Key.")
id := params["PETITION_ID"]
// checks if petition id is set.
if len(id) <= 0 {
err := "Petition ID is empty. Use GetPetitionId first."
t.Fatal(err)
}
change_org := NewChangeOrgClient(params["API_KEY"])
authKey, err := change_org.GetAuthKey(AuthKeysArgs{
PetitionID: id,
SourceDesc: "source_description",
Source: "source_that_is_using_the_api",
RequesterEmail: "developer_email",
Callback: "mycallback",
}, params["SECRET"])
if err != nil {
t.Fatal(err)
}
msg := "Auth Key: " + authKey
log.Println(msg)
}
// TestSignature tests SignPetition function.
func TestSignature(t *testing.T) {
log.Println("Go sign a petition via API. Pun intended ;)")
id := params["PETITION_ID"]
// checks if petition id is set.
if len(id) <= 0 {
err := "Petition ID is empty. Use GetPetitionId first."
t.Fatal(err)
}
auth_key := params["AUTH_KEY"]
// checks if auth key is set.
if len(auth_key) <= 0 {
err := "Auth Key is empty. Use GetAuthKey first."
t.Fatal(err)
}
change_org := NewChangeOrgClient(params["API_KEY"])
response, err := change_org.SignPetition(PetitionArgs{
PetitionID: id,
AuthKey: auth_key,
Source: "source_that_is_using_the_api",
Email: "email@example.com",
FirstName: "Grace",
LastName: "Hopper",
Address: "123 Address",
City: "New Haven",
State: "CT",
ZIP: "06520",
Country: "US",
Hidden: "true",
}, params["SECRET"])
if err != nil {
t.Fatal(err)
}
msg := "Response: " + response
log.Println(msg)
}