~thrrgilag/woodstock

woodstock/files_test.go -rw-r--r-- 4.5 KiB
62b10b5cMorgan McMillian Update for new project location and module name 3 months ago
                                                                                
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package woodstock

import (
	"os"
	"testing"
	"time"
)

func TestGetFile(t *testing.T) {
	config, err := GetConfig()
	if err != nil {
		t.Error(err)
	}
	fileID := "15596"
	fileName := "hawkward.jpg"
	client := NewClient(config.ClientID, "")
	// client.SetAccessToken(config.AccessToken)
	file, err := client.GetFile(fileID)
	if err != nil {
		t.Error(err)
	}
	if file.Data.Name != fileName {
		t.Errorf("File appears incorrect, got: %s, want: %s", file.Data.Name, fileName)
	}
	t.Log(file.Meta)
	time.Sleep(Delay)
}

func TestGetFiles(t *testing.T) {
	config, err := GetConfig()
	if err != nil {
		t.Error(err)
	}
	ids := []string{"15589", "15596", "15597"}
	fileName1 := "test-pattern.jpg"
	fileName2 := "hawkward.jpg"
	fileName3 := "walken.jpg"
	client := NewClient(config.ClientID, "")
	// client.SetAccessToken(config.AccessToken)
	files, err := client.GetFiles(ids)
	if err != nil {
		t.Error(err)
	}
	if files.Data[0].Name != fileName1 {
		t.Errorf("File appears incorrect, got: %s, want: %s", files.Data[0].Name, fileName1)
	}
	if files.Data[1].Name != fileName2 {
		t.Errorf("File appears incorrect, got: %s, want: %s", files.Data[1].Name, fileName2)
	}
	if files.Data[2].Name != fileName3 {
		t.Errorf("File appears incorrect, got: %s, want: %s", files.Data[2].Name, fileName3)
	}
	time.Sleep(Delay)
}

func TestGetUserFiles(t *testing.T) {
	config, err := GetConfig()
	if err != nil {
		t.Error(err)
	}
	client := NewClient(config.ClientID, "")
	client.SetAccessToken(config.AccessToken)
	files, err := client.GetUserFiles()
	if err != nil {
		t.Error(err)
	}
	if len(files.Data) < 3 {
		t.Errorf("I recieved fewer files than expected, got: %d, want at least: %d", len(files.Data), 3)
	}
	time.Sleep(Delay)
}

func TestCreateFile(t *testing.T) {
	config, err := GetConfig()
	if err != nil {
		t.Error(err)
	}
	filename := "test-pattern.jpg"
	filereader, err := os.Open(filename)
	if err != nil {
		t.Error(err)
	}
	defer filereader.Close()
	params := map[string]string{
		"type":      "dev.thrrgilag.woodstock",
		"name":      filename,
		"kind":      "image",
		"mimetype":  "image/jpeg",
		"is_public": "true",
	}
	client := NewClient(config.ClientID, "")
	client.SetAccessToken(config.AccessToken)
	file, err := client.CreateFile(params, filereader)
	if err != nil {
		t.Error(err)
	}
	if file.Data.Name != filename {
		t.Errorf("File appears incorrect, got: %s, want: %s", file.Data.Name, filename)
	}
	time.Sleep(Delay)
}

func TestCreateFilePlaceholder(t *testing.T) {
	config, err := GetConfig()
	if err != nil {
		t.Error(err)
	}
	var params File
	params.Type = "dev.thrrgilag.woodstock"
	params.Name = "test-pattern.jpg"
	params.Kind = "image"
	params.MimeType = "image/jpeg"
	params.IsPublic = true
	client := NewClient(config.ClientID, "")
	client.SetAccessToken(config.AccessToken)
	file, err := client.CreateFilePlaceholder(params)
	if err != nil {
		t.Error(err)
	}
	t.Log(file.Meta)
	time.Sleep(Delay)
}

// TODO: is broken
// func TestUpdateFileContent(t *testing.T) {
// 	config, err := GetConfig()
// 	if err != nil {
// 		t.Error(err)
// 	}
// 	filename := "test-pattern.jpg"
// 	filereader, err := os.Open(filename)
// 	if err != nil {
// 		t.Error(err)
// 	}
// 	defer filereader.Close()
// 	client := NewClient(config.ClientID, "")
// 	client.SetAccessToken(config.AccessToken)
// 	file, err := client.UpdateFileContent("15588", filereader)
// 	if err != nil {
// 		t.Error(err)
// 	}
// 	t.Log(file.Meta)
// 	time.Sleep(Delay)
// }

func TestUpdateFileDetails(t *testing.T) {
	config, err := GetConfig()
	if err != nil {
		t.Error(err)
	}
	fileID := "14987"
	// name := "test image"
	var details FileDetails
	details.Name = "test_image"
	client := NewClient(config.ClientID, "")
	client.SetAccessToken(config.AccessToken)
	file, err := client.UpdateFileDetails(fileID, details)
	if err != nil {
		t.Error(err)
	}
	t.Log(file.Meta)
	time.Sleep(Delay)
	// TODO: change filename string to timestamp that can be checked
}

func TestDeleteFile(t *testing.T) {
	config, err := GetConfig()
	if err != nil {
		t.Error(err)
	}
	filename := "test-pattern.jpg"
	filereader, err := os.Open(filename)
	if err != nil {
		t.Error(err)
	}
	defer filereader.Close()
	params := map[string]string{
		"type":      "dev.thrrgilag.woodstock",
		"name":      filename,
		"kind":      "image",
		"mimetype":  "image/jpeg",
		"is_public": "true",
	}
	client := NewClient(config.ClientID, "")
	client.SetAccessToken(config.AccessToken)
	newfile, err := client.CreateFile(params, filereader)
	if err != nil {
		t.Error(err)
	}
	file, err := client.DeleteFile(newfile.Data.ID)
	if err != nil {
		t.Error(err)
	}
	t.Log(file.Meta)
	time.Sleep(Delay)
}