~evanj/cms

9c6ee9542bdf3f716f7437f6f0bf5978926d1b64 — Evan M Jones 4 months ago bd9652d
Feat(pkg/e3): Complete testing for E3 client code.
1 files changed, 193 insertions(+), 39 deletions(-)

M pkg/e3/e3_test.go
M pkg/e3/e3_test.go => pkg/e3/e3_test.go +193 -39
@@ 2,60 2,214 @@ package e3_test

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

	"git.sr.ht/~evanj/cms/pkg/e3"
	"github.com/bmizerany/assert"
)

var objectstorage = e3.New(
	os.Getenv("TEST_E3_USER"),
	os.Getenv("TEST_E3_PASS"),
	e3.DefaultURL,
	"github.com/google/uuid"
)

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)
	tests := []e3.E3{
		e3.New(
			os.Getenv("TEST_E3_USER"),
			os.Getenv("TEST_E3_PASS"),
			e3.DefaultURL,
		),
		e3.WithClient(
			os.Getenv("TEST_E3_USER"),
			os.Getenv("TEST_E3_PASS"),
			e3.DefaultURL,
			http.DefaultClient,
		),
	}

	for _, objectstorage := range tests {
		// 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 TestContextOver(t *testing.T) {
	t.Parallel()

	tests := []e3.E3{
		e3.New(
			os.Getenv("TEST_E3_USER"),
			os.Getenv("TEST_E3_PASS"),
			e3.DefaultURL,
		),
		e3.WithClient(
			os.Getenv("TEST_E3_USER"),
			os.Getenv("TEST_E3_PASS"),
			e3.DefaultURL,
			http.DefaultClient,
		),
	}

	for _, objectstorage := range tests {
		// 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)
	}
}

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

	tests := []e3.E3{
		e3.New(
			os.Getenv("TEST_E3_USER"),
			os.Getenv("TEST_E3_PASS"),
			"https://e3.evanjon.es/not-found",
		),
		e3.WithClient(
			os.Getenv("TEST_E3_USER"),
			os.Getenv("TEST_E3_PASS"),
			"https://e3.evanjon.es/not-found",
			http.DefaultClient,
		),
	}

	for _, objectstorage := range tests {
		// Upload public file.
		file, err := os.Open("testfiles/zoidberg.png")
		assert.Equal(t, nil, err)
		_, err = objectstorage.Upload(context.Background(), false, file.Name(), file)
		assert.Equal(t, true, strings.Contains(err.Error(), "unknown user parameters"))
	}
}

func TestURL(t *testing.T) {
	tests := []e3.E3{
		e3.New(
			os.Getenv("TEST_E3_USER"),
			os.Getenv("TEST_E3_PASS"),
			e3.DefaultURL,
		),
		e3.WithClient(
			os.Getenv("TEST_E3_USER"),
			os.Getenv("TEST_E3_PASS"),
			e3.DefaultURL,
			http.DefaultClient,
		),
	}

	for _, objectstorage := range tests {
		assert.Equal(t, e3.DefaultURL, objectstorage.URL())
	}
}

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

	tests := []e3.E3{
		e3.New(
			os.Getenv("TEST_E3_USER"),
			os.Getenv("TEST_E3_PASS"),
			e3.DefaultURL,
		),
		e3.WithClient(
			os.Getenv("TEST_E3_USER"),
			os.Getenv("TEST_E3_PASS"),
			e3.DefaultURL,
			http.DefaultClient,
		),
	}

	for _, objectstorage := range tests {
		// Retrieve the same file (with creds).
		_, err := objectstorage.Proxy(context.Background(), fmt.Sprintf("http://127.0.0.1:9999/%s", uuid.New().String()))
		assert.NotEqual(t, nil, err)
	}
}

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)
	tests := []e3.E3{
		e3.New(
			os.Getenv("TEST_E3_USER"),
			os.Getenv("TEST_E3_PASS"),
			e3.DefaultURL,
		),
		e3.WithClient(
			os.Getenv("TEST_E3_USER"),
			os.Getenv("TEST_E3_PASS"),
			e3.DefaultURL,
			http.DefaultClient,
		),
	}

	for _, objectstorage := range tests {
		{
			ctx, cancel := context.WithCancel(context.Background())
			cancel()

			// 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(ctx, url)
			assert.NotEqual(t, nil, err)
		}
		{
			ctx, cancel := context.WithCancel(context.Background())
			cancel()

			// Upload public file.
			file, err := os.Open("testfiles/zoidberg.png")
			assert.Equal(t, nil, err)
			_, err = objectstorage.Upload(ctx, false, file.Name(), file)
			assert.NotEqual(t, nil, err)
		}
	}
}