forked from ggordan/go-onedrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_test.go
135 lines (117 loc) · 4.04 KB
/
search_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
127
128
129
130
131
132
133
134
135
package onedrive
import (
"context"
"encoding/json"
"net/http"
"net/url"
"testing"
"time"
"gotest.tools/v3/assert"
)
func TestSearchService_Search(t *testing.T) {
ctx := context.Background()
t.Run("returns a search response with matching entries when the request is succcesful", func(t *testing.T) {
setup()
defer teardown()
srv := searchService{client: oneDrive}
mux.HandleFunc("/search/query", func(w http.ResponseWriter, req *http.Request) {
assert.Equal(t, req.Method, http.MethodPost)
assert.Equal(t, req.Header.Get("Content-Type"), "application/json")
var body map[string]interface{}
assert.NilError(t, json.NewDecoder(req.Body).Decode(&body))
defer req.Body.Close()
assert.DeepEqual(t, body, map[string]interface{}{
"requests": []interface{}{
map[string]interface{}{
"entityTypes": []interface{}{"driveItem"},
"from": 0.0,
"query": map[string]interface{}{
"queryString": `filetype:xlsx path:"http://sharepointsite.example.com" filename:"metrics"`,
},
"size": 50.0,
},
},
})
fileWrapperHandler("fixtures/search.sharepoint.xlsx.files.json", 200)(w, req)
})
res, err := srv.Search(ctx, SearchRequest{
EntityTypes: []EntityType{DriveItem},
Query: SearchOptions{
FileType: "xlsx",
Filename: "metrics",
Path: "http://sharepointsite.example.com",
},
From: 0,
Size: 50,
})
assert.NilError(t, err)
assert.DeepEqual(t, res, &SearchResponse{
Entries: []ResponseValue{
{
RecordContainers: []HitRecord{
{
Records: []Resource{
{
Detail: ResourceDetail{
Name: "TestSpreadsheet.xlsx",
LastModifiedDateTime: "2023-09-15T15:21:32Z",
ParentReference: ParentReference{
SiteID: "geckotest.sharepoint.com,6c85e504-83c4-11ee-9a22-3b8cdacf20b0,82a42d96-83c4-11ee-bf72-23b70da57b80",
DriveID: "b!kgQ8GaPYWUq1lFr0iFCk-PLCDocV3VFPgi9xnUXR-5Qr3Wx1CpLoR5-T4tJ",
Identifiers: Identifiers{
ListID: "3d334d4a-83c5-11ee-bbee-378bbb9bb93d",
ListItemID: "eb05bb6a-0cd1-4b14-b4e5-42ee32c78fb9",
},
},
},
},
{
Detail: ResourceDetail{
Name: "TeamA Shared File.xlsx",
LastModifiedDateTime: "2023-10-13T11:38:30Z",
ParentReference: ParentReference{
SiteID: "geckotest.sharepoint.com,6663ae22-83c4-11ee-9df7-a7207635eb99,870ec2f2-dd15-4f51-822f-719d45d1fb94",
DriveID: "b!kgQ8GaPYWUq1lFr0iFCk-PLCDocV3VFPgi9xnUXR-5Q1aV1N0LTSQ7BJ972r",
Identifiers: Identifiers{
ListID: "7cb14d5a-83c5-11ee-b6fb-6bc49ef4866d",
ListItemID: "376e3002-b63e-40eb-a8c8-513a8f65fae9",
},
},
},
},
},
},
},
},
},
})
})
t.Run("returns an error when the response errors", func(t *testing.T) {
setup()
defer teardown()
srv := searchService{client: oneDrive}
mux.HandleFunc("/search/query", func(w http.ResponseWriter, req *http.Request) {
fileWrapperHandler("fixtures/search.error.response.json", 400)(w, req)
})
_, err := srv.Search(ctx, SearchRequest{})
assert.ErrorContains(t, err, "The call failed, please try again.")
})
t.Run("returns an error when the context deadline reached during request", func(t *testing.T) {
setup()
defer teardown()
srv := searchService{client: oneDrive}
mux.HandleFunc("/search/query", func(w http.ResponseWriter, req *http.Request) {
time.Sleep(100 * time.Millisecond)
fileWrapperHandler("fixtures/search.sharepoint.xlsx.files.json", 200)(w, req)
})
ctx, cancelFn := context.WithTimeout(ctx, 10*time.Millisecond)
defer cancelFn()
_, err := srv.Search(ctx, SearchRequest{})
assert.ErrorContains(t, err, "context deadline exceeded")
})
t.Run("returns an error when building the request fails", func(t *testing.T) {
srv := searchService{client: &OneDrive{BaseURL: string([]byte{0x7f})}}
_, err := srv.Search(ctx, SearchRequest{})
assert.ErrorType(t, err, &url.Error{})
})
}