forked from ggordan/go-onedrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site_test.go
120 lines (101 loc) · 3.85 KB
/
site_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
package onedrive
import (
"context"
"net/http"
"net/url"
"testing"
"time"
"gotest.tools/v3/assert"
)
func TestSiteService_Search(t *testing.T) {
t.Run("successfully returns a collection of sites", func(t *testing.T) {
setup()
defer teardown()
srv := siteService{client: oneDrive}
mux.HandleFunc("/sites", func(w http.ResponseWriter, req *http.Request) {
assert.Equal(t, req.URL.Query().Get("search"), "the filter")
fileWrapperHandler("fixtures/site.search.valid.json", 200)(w, req)
})
got, err := srv.Search(context.Background(), "the filter")
assert.NilError(t, err)
want := &Sites{
Collection: []Site{
{
ID: "geckotest.sharepoint.com,ec9e6431-91ed-42d6-89a1-7f64d6f31b02,55dc3204-2dfe-4c08-a4fc-65593905518e",
Name: "geckotest.sharepoint.com",
DisplayName: "Geckoboard Team Site",
CreatedAt: time.Date(2014, 12, 16, 1, 15, 35, 0, time.UTC),
WebURL: "https://geckotest.sharepoint.com",
Extra: SiteData{
Hostname: "geckotest.sharepoint.com",
},
},
{
ID: "geckotest.sharepoint.com,6f7d1f84-7138-49b5-82ff-cde0bb23edbb,7953cb13-fe36-444f-9410-e663d3f1597f",
Name: "TeamOrgSite",
DisplayName: "TeamOrgSite",
Description: "TeamOrgSite",
CreatedAt: time.Date(2022, 6, 8, 18, 11, 21, 0, time.UTC),
WebURL: "https://geckotest.sharepoint.com/sites/TeamOrgSite",
Extra: SiteData{
Hostname: "geckotest.sharepoint.com",
},
},
{
ID: "geckotest.sharepoint.com,6c32fce6-0b8b-452b-b30b-30037c735af4,0668ad2f-eca4-4006-bcf7-273340b18a90",
Name: "contentTypeHub",
DisplayName: "Team Site",
CreatedAt: time.Date(2015, 11, 6, 10, 51, 40, 0, time.UTC),
WebURL: "https://geckotest.sharepoint.com/sites/contentTypeHub",
Extra: SiteData{
Hostname: "geckotest.sharepoint.com",
},
},
},
}
assert.DeepEqual(t, got, want)
})
t.Run("errors when building request fails", func(t *testing.T) {
srv := siteService{client: &OneDrive{BaseURL: string([]byte{0x7f})}}
_, err := srv.Search(context.Background(), "")
assert.ErrorType(t, err, &url.Error{})
})
t.Run("errors when bad request response", func(t *testing.T) {
setup()
defer teardown()
srv := siteService{client: oneDrive}
mux.HandleFunc("/sites", fileWrapperHandler("fixtures/site.search.invalid.json", 400))
_, err := srv.Search(context.Background(), "thefilter")
assert.ErrorContains(t, err, "Search criteria must be provided for sites search")
})
}
func TestSiteService_RootSite(t *testing.T) {
t.Run("successfully returns the root site details when the requests succeeds", func(t *testing.T) {
setup()
defer teardown()
srv := siteService{client: oneDrive}
mux.HandleFunc("/sites/root", fileWrapperHandler("fixtures/site.root.valid.json", 200))
got, err := srv.RootSite(context.Background())
assert.NilError(t, err)
assert.DeepEqual(t, got, &Site{
ID: "geckotest.sharepoint.com,d1accbe0-839d-11ee-b148-dfeac5dd9ada,dbea6860-839d-11ee-8830-a3eeb150c1aa",
DisplayName: "Geckoboard Test Site",
CreatedAt: time.Date(2014, 12, 16, 1, 15, 35, 467000000, time.UTC),
WebURL: "https://geckotest.sharepoint.com",
Extra: SiteData{Hostname: "geckotest.sharepoint.com"},
})
})
t.Run("returns an error when building the request fails", func(t *testing.T) {
srv := siteService{client: &OneDrive{BaseURL: string([]byte{0x7f})}}
_, err := srv.RootSite(context.Background())
assert.ErrorType(t, err, &url.Error{})
})
t.Run("returns an error when we get a bad response", func(t *testing.T) {
setup()
defer teardown()
srv := siteService{client: oneDrive}
mux.HandleFunc("/sites/root", fileWrapperHandler("fixtures/site.root.invalid.json", 400))
_, err := srv.RootSite(context.Background())
assert.ErrorContains(t, err, "Invalid hostname for this tenancy")
})
}