forked from sogko/go-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 2
/
media_test.go
155 lines (132 loc) · 3.61 KB
/
media_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package wordpress_test
import (
"github.com/sogko/go-wordpress"
"io/ioutil"
"net/http"
"os"
"testing"
)
func factoryMediaFileUpload(t *testing.T) *wordpress.MediaUploadOptions {
// assuming current-working directory `{GO_WORKSPACE_PATH}/src/github.com/sogko/go-wordpress`
path := "./test-data/test-media.jpg"
// prepare file to upload
file, err := os.Open(path)
defer file.Close()
if err != nil {
t.Fatalf("Failed to open test media file to upload: %v", err.Error())
}
fileContents, err := ioutil.ReadAll(file)
if err != nil {
t.Fatalf("Failed to read test media file to upload: %v", err.Error())
}
// create / upload media
media := wordpress.MediaUploadOptions{
Filename: "test-media.jpg",
ContentType: "image/jpeg",
Data: fileContents,
}
return &media
}
func getAnyOneMedia(t *testing.T, wp *wordpress.Client) *wordpress.Media {
media, resp, _, _ := wp.Media().List(nil)
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if len(media) < 1 {
t.Fatalf("Should not return empty comments")
}
mediaID := media[0].ID
m, resp, _, _ := wp.Media().Get(mediaID, "context=edit")
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if m == nil {
t.Fatalf("Media should not be nil")
}
return m
}
func cleanUpMedia(t *testing.T, wp *wordpress.Client, mediaID int) {
deletedMedia, resp, body, err := wp.Media().Delete(mediaID, "force=true")
if err != nil {
t.Errorf("Failed to clean up new media: %v", err.Error())
}
if body == nil {
t.Errorf("body should not be nil")
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 StatusOK, got %v", resp.Status)
}
if deletedMedia.ID != mediaID {
t.Errorf("Deleted comment ID should be the same as newly created comment: %v != %v", deletedMedia.ID, mediaID)
}
}
func TestMediaList(t *testing.T) {
wp := initTestClient()
media, resp, body, err := wp.Media().List(nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if body == nil {
t.Errorf("Should not return nil body")
}
if media == nil {
t.Errorf("Should not return nil media")
}
if len(media) == 0 {
t.Errorf("Should not return empty media")
}
}
func TestMediaGet_Exists(t *testing.T) {
wp := initTestClient()
m := getAnyOneMedia(t, wp)
media, resp, body, err := wp.Media().Get(m.ID, nil)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected 200 OK, got %v", resp.Status)
}
if body == nil {
t.Errorf("Should not return nil body")
}
if media == nil {
t.Errorf("Should not return nil media")
}
}
func TestMediaGet_DoesNotExists(t *testing.T) {
wp := initTestClient()
media, resp, body, err := wp.Media().Get(-1, nil)
if err == nil {
t.Errorf("Should return error")
}
if resp.StatusCode != http.StatusNotFound {
t.Errorf("Expected 404 Not Found, got %v", resp.Status)
}
if body == nil {
t.Errorf("Should not return nil body")
}
if media == nil {
t.Errorf("Should not return nil media")
}
}
func TestMediaCreate(t *testing.T) {
wp := initTestClient()
media := factoryMediaFileUpload(t)
newMedia, resp, body, err := wp.Media().Create(media)
if err != nil {
t.Errorf("Should not return error: %v", err.Error())
}
if resp.StatusCode != http.StatusCreated {
t.Errorf("Expected 201 Created, got %v", resp.Status)
}
if body == nil {
t.Errorf("Should not return nil body")
}
if newMedia == nil {
t.Errorf("Should not return nil newMedia")
}
cleanUpMedia(t, wp, newMedia.ID)
}