This repository was archived by the owner on Mar 29, 2025. It is now read-only.
forked from contentful-labs/contentful-go
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy patheditor_interface_test.go
More file actions
142 lines (107 loc) · 4.1 KB
/
Copy patheditor_interface_test.go
File metadata and controls
142 lines (107 loc) · 4.1 KB
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
136
137
138
139
140
141
142
package contentful
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/labd/contentful-go/pkgs/common"
"github.com/labd/contentful-go/pkgs/util"
"github.com/stretchr/testify/assert"
)
func TestEditorInterfacesService_List(t *testing.T) {
var err error
assertions := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertions.Equal(r.Method, "GET")
assertions.Equal(r.URL.Path, "/spaces/"+spaceID+"/environments/master/editor_interface")
checkHeaders(r, assertions)
w.WriteHeader(200)
_, _ = fmt.Fprintln(w, readTestData("editor_interface.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cmaClient client
cmaClient = NewCMA(CMAToken)
cmaClient.BaseURL = server.URL
collection, err := cmaClient.EditorInterfaces.List(spaceID).Next()
assertions.Nil(err)
interfaces := collection.ToEditorInterface()
assertions.Equal(1, len(interfaces))
assertions.Equal("name", interfaces[0].Controls[0].FieldID)
assertions.Equal("extension", interfaces[0].SideBar[0].WidgetNameSpace)
}
func TestEditorInterfacesService_Get(t *testing.T) {
var err error
assertions := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertions.Equal(r.Method, "GET")
assertions.Equal(r.URL.Path, "/spaces/"+spaceID+"/environments/master/content_types/hfM9RCJIk0wIm06WkEOQY/editor_interface")
checkHeaders(r, assertions)
w.WriteHeader(200)
_, _ = fmt.Fprintln(w, readTestData("editor_interface_1.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cmaClient client
cmaClient = NewCMA(CMAToken)
cmaClient.BaseURL = server.URL
editorInterface, err := cmaClient.EditorInterfaces.Get(spaceID, "hfM9RCJIk0wIm06WkEOQY")
assertions.Nil(err)
assertions.Equal("name", editorInterface.Controls[0].FieldID)
assertions.Equal("extension", editorInterface.SideBar[0].WidgetNameSpace)
}
func TestEditorInterfacesService_Get_2(t *testing.T) {
var err error
assertions := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertions.Equal(r.Method, "GET")
assertions.Equal(r.URL.Path, "/spaces/"+spaceID+"/environments/master/content_types/hfM9RCJIk0wIm06WkEOQY/editor_interface")
checkHeaders(r, assertions)
w.WriteHeader(400)
_, _ = fmt.Fprintln(w, readTestData("editor_interface_1.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cmaClient client
cmaClient = NewCMA(CMAToken)
cmaClient.BaseURL = server.URL
_, err = cmaClient.EditorInterfaces.Get(spaceID, "hfM9RCJIk0wIm06WkEOQY")
assertions.NotNil(err)
var notFoundError common.ErrorResponse
errors.As(err, ¬FoundError)
}
func TestEditorInterfacesService_Update(t *testing.T) {
var err error
assertions := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertions.Equal(r.Method, "PUT")
assertions.Equal(r.RequestURI, "/spaces/"+spaceID+"/environments/master/content_types/hfM9RCJIk0wIm06WkEOQY/editor_interface")
checkHeaders(r, assertions)
var payload map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&payload)
assertions.Nil(err)
controls := payload["controls"].([]interface{})
sidebar := payload["sidebar"].([]interface{})
assertions.Equal("changed id", controls[0].(map[string]interface{})["widgetId"].(string))
assertions.Equal("someuiextension", sidebar[0].(map[string]interface{})["widgetId"].(string))
w.WriteHeader(200)
_, _ = fmt.Fprintln(w, readTestData("editor_interface_updated.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cmaClient client
cmaClient = NewCMA(CMAToken)
cmaClient.BaseURL = server.URL
editorInterface, err := editorInterfaceFromTestFile("editor_interface_1.json")
assertions.Nil(err)
editorInterface.Controls[0].WidgetID = util.ToPointer("changed id")
err = cmaClient.EditorInterfaces.Update(spaceID, "hfM9RCJIk0wIm06WkEOQY", editorInterface)
assertions.Nil(err)
assertions.Equal("changed id", *editorInterface.Controls[0].WidgetID)
}