~evanj/cms

ref: 45d809610bddec619ed3ca6371d520d0473b4c72 cms/pkg/e3/e3_test.go -rw-r--r-- 1.5 KiB
45d80961Evan M Jones Fix(main.css): Large ref buttons overflow. 10 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
package e3_test

import (
	"context"
	"io/ioutil"
	"net/http"
	"os"
	"testing"

	"git.sr.ht/~evanj/cms/pkg/e3"
	"github.com/go-playground/assert/v2"
)

var objectstorage = e3.New(
	os.Getenv("TEST_E3_USER"),
	os.Getenv("TEST_E3_PASS"),
	e3.DefaultURL,
)

func TestPublic(t *testing.T) {
	t.Parallel()

	// Upload public file.
	file, err := os.Open("testfiles/zoidberg.png")
	assert.Equal(t, nil, err)
	url, err := objectstorage.Upload(context.Background(), true, file.Name(), file)
	assert.Equal(t, nil, err)
	// Retrieve the same file.
	req, err := http.NewRequest("GET", url, nil)
	assert.Equal(t, nil, err)
	resp, err := http.DefaultClient.Do(req)
	assert.Equal(t, nil, err)
	defer resp.Body.Close()
	_, err = ioutil.ReadAll(resp.Body)
	assert.Equal(t, nil, err)
	assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestPrivate(t *testing.T) {
	t.Parallel()

	// Upload public file.
	file, err := os.Open("testfiles/zoidberg.png")
	assert.Equal(t, nil, err)
	url, err := objectstorage.Upload(context.Background(), false, file.Name(), file)
	assert.Equal(t, nil, err)

	// Retrieve the same file (without creds).
	req, err := http.NewRequest("GET", url, nil)
	assert.Equal(t, nil, err)
	resp, err := http.DefaultClient.Do(req)
	assert.Equal(t, nil, err)
	defer resp.Body.Close()
	_, err = ioutil.ReadAll(resp.Body)
	assert.Equal(t, nil, err)
	assert.NotEqual(t, http.StatusOK, resp.StatusCode)

	// Retrieve the same file (with creds).
	_, err = objectstorage.Proxy(context.Background(), url)
	assert.Equal(t, nil, err)
}