-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathifog_test.go
122 lines (116 loc) · 4.18 KB
/
ifog_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
package ifog
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"github.com/petitout/ifog/fmip"
"github.com/petitout/ifog/login"
"github.com/petitout/ifog/models"
)
func TestSession_Login(t *testing.T) {
s := NewSession()
response := new(login.ResponseBody)
response.User.AppleId = "myAppleId"
response.Webservices = make(map[string]models.WebService)
response.Webservices[fmip.URLKey] = models.WebService{Url: "http://falseFmipURL.com"}
var requestHeader http.Header
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestHeader = r.Header
w.Header().Set("Content-Type", "application/json")
responseToSend, _ := json.Marshal(*response)
w.Write(responseToSend)
}))
s.LoginURL = ts.URL
defer ts.Close()
err := s.Login(login.RequestBody{AppleId: "myAppleId", Password: "mySuperPassword"})
if err != nil {
t.Errorf("Session.Login() error = %v, wantErr %v", err, nil)
}
if !reflect.DeepEqual(response.User, s.User) {
t.Errorf("Session.Login() error = %v, wantErr %v", s.User, response.User)
}
if !reflect.DeepEqual(response.Webservices, s.Webservices) {
t.Errorf("Session.Login() error = %v, wantErr %v", s.User, response.User)
}
if !checkRequestHeader(requestHeader) {
t.Errorf("Session.Login() error = Bad request header")
}
}
func TestSession_PopulateDevices(t *testing.T) {
response := new(fmip.ResponseBody)
response.Devices = []models.Device{
models.Device{Location: models.Location{}, DeviceClass: "iPhone", Id: "thisIsADeviceId"},
}
var requestHeader http.Header
var requestURL url.URL
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestHeader = r.Header
requestURL = *r.URL
w.Header().Set("Content-Type", "application/json")
responseToSend, _ := json.Marshal(*response)
w.Write(responseToSend)
}))
defer ts.Close()
s := NewSession()
s.client = new(http.Client)
s.Webservices = make(map[string]models.WebService)
s.Webservices[fmip.URLKey] = models.WebService{Url: ts.URL}
err := s.PopulateDevices()
if err != nil {
t.Errorf("Session.Login() error = %v, wantErr %v", err, nil)
}
if requestURL.Path != fmip.RefreshClientURL {
t.Errorf("Session.Login() error = %v, wantErr %v", requestURL.Path, fmip.RefreshClientURL)
}
if !reflect.DeepEqual(response.Devices, s.Devices) {
t.Errorf("Session.Login() error = %v, wantErr %v", s.Devices, response.Devices)
}
if !checkRequestHeader(requestHeader) {
t.Errorf("Session.Login() error = Bad request header")
}
}
func TestSession_SendMessage(t *testing.T) {
response := new(fmip.ResponseBody)
var requestHeader http.Header
var requestURL url.URL
expectedBody := fmip.SendMessageRequestBody{RequestCommonBody: fmip.RequestCommonBody{Dsid: "Dsid", ClientId: "whatever"}, Device: "deviceID", Subject: "subject", Sound: false, UserText: true, Text: "this is a message"}
var requestBody fmip.SendMessageRequestBody
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestHeader = r.Header
requestURL = *r.URL
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&requestBody)
if err != nil {
panic(err)
}
defer r.Body.Close()
w.Header().Set("Content-Type", "application/json")
responseToSend, _ := json.Marshal(*response)
w.Write(responseToSend)
}))
defer ts.Close()
s := NewSession()
s.client = new(http.Client)
s.Webservices = make(map[string]models.WebService)
s.Webservices[fmip.URLKey] = models.WebService{Url: ts.URL}
s.User.Dsid = expectedBody.Dsid
err := s.SendMessage(expectedBody.Text, expectedBody.Device, expectedBody.Subject)
if err != nil {
t.Errorf("Session.Login() error = %v, wantErr %v", err, nil)
}
if requestURL.Path != fmip.SendMessageURL {
t.Errorf("Session.Login() error = %v, wantErr %v", requestURL.Path, fmip.SendMessageURL)
}
if !reflect.DeepEqual(requestBody, expectedBody) {
t.Errorf("Session.Login() error = %v, wantErr %v", requestBody, expectedBody)
}
if !checkRequestHeader(requestHeader) {
t.Errorf("Session.Login() error = Bad request header")
}
}
func checkRequestHeader(h http.Header) bool {
return h.Get("Content-Type") == "application/json" && h.Get("Origin") == "https://www.icloud.com"
}