1package cloudstorage_test
2
3import (
4	"encoding/json"
5	"testing"
6	"time"
7
8	"github.com/stretchr/testify/assert"
9
10	"github.com/lytics/cloudstorage"
11	"github.com/lytics/cloudstorage/localfs"
12	"github.com/lytics/cloudstorage/testutils"
13)
14
15func TestAll(t *testing.T) {
16	localFsConf := &cloudstorage.Config{
17		Type:       localfs.StoreType,
18		AuthMethod: localfs.AuthFileSystem,
19		LocalFS:    "/tmp/mockcloud",
20		TmpDir:     "/tmp/localcache",
21	}
22
23	store, err := cloudstorage.NewStore(localFsConf)
24	if err != nil {
25		t.Fatalf("Could not create store: config=%+v  err=%v", localFsConf, err)
26		return
27	}
28	testutils.RunTests(t, store)
29	// verify cleanup
30	cloudstorage.CleanupCacheFiles(time.Minute*1, localFsConf.TmpDir)
31}
32
33func TestStore(t *testing.T) {
34	invalidConf := &cloudstorage.Config{}
35
36	store, err := cloudstorage.NewStore(invalidConf)
37	assert.NotEqual(t, nil, err)
38	assert.Equal(t, nil, store)
39
40	missingStoreConf := &cloudstorage.Config{
41		Type: "non-existent-store",
42	}
43
44	store, err = cloudstorage.NewStore(missingStoreConf)
45	assert.NotEqual(t, nil, err)
46	assert.Equal(t, nil, store)
47
48	// test missing temp dir, assign local temp
49	localFsConf := &cloudstorage.Config{
50		Type:       localfs.StoreType,
51		AuthMethod: localfs.AuthFileSystem,
52		LocalFS:    "/tmp/mockcloud",
53	}
54
55	store, err = cloudstorage.NewStore(localFsConf)
56	assert.Equal(t, nil, err)
57	assert.NotEqual(t, nil, store)
58}
59
60func TestJwtConf(t *testing.T) {
61	configInput := `
62	{
63		"JwtConf": {
64			"type": "service_account",
65			"project_id": "testing",
66			"private_key_id": "abcdefg",
67			"private_key": "aGVsbG8td29ybGQ=",
68			"client_email": "testing@testing.iam.gserviceaccount.com",
69			"client_id": "117058426251532209964",
70			"scopes": [
71				"https://www.googleapis.com/auth/devstorage.read_write"
72			]
73		}
74	}`
75
76	// v := base64.StdEncoding.EncodeToString([]byte("hello-world"))
77	// t.Logf("b64  %q", v)
78	conf := &cloudstorage.Config{}
79	err := json.Unmarshal([]byte(configInput), conf)
80	assert.Equal(t, nil, err)
81	conf.JwtConf.PrivateKey = "------helo-------\naGVsbG8td29ybGQ=\n-----------------end--------"
82	assert.NotEqual(t, nil, conf.JwtConf)
83	assert.Equal(t, nil, conf.JwtConf.Validate())
84	assert.Equal(t, "aGVsbG8td29ybGQ=", conf.JwtConf.PrivateKey)
85	assert.Equal(t, "service_account", conf.JwtConf.Type)
86
87	// note on this one the "keytype" & "private_keybase64"
88	configInput = `
89	{
90		"JwtConf": {
91			"keytype": "service_account",
92			"project_id": "testing",
93			"private_key_id": "abcdefg",
94			"private_keybase64": "aGVsbG8td29ybGQ=",
95			"client_email": "testing@testing.iam.gserviceaccount.com",
96			"client_id": "117058426251532209964",
97			"scopes": [
98				"https://www.googleapis.com/auth/devstorage.read_write"
99			]
100		}
101	}`
102	conf = &cloudstorage.Config{}
103	err = json.Unmarshal([]byte(configInput), conf)
104	assert.Equal(t, nil, err)
105	assert.NotEqual(t, nil, conf.JwtConf)
106	assert.Equal(t, nil, conf.JwtConf.Validate())
107	assert.Equal(t, "aGVsbG8td29ybGQ=", conf.JwtConf.PrivateKey)
108	assert.Equal(t, "service_account", conf.JwtConf.Type)
109}
110