Пример #1
0
func Test_client_Delete(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := `{"is_deleted": true, "revision": 346, "rev": "15a1441bd38", "thumb_exists": false, "bytes": 0, "modified": "Sun, 29 Sep 2013 18:04:57 +0000", "client_mtime": "Wed, 31 Dec 1969 23:59:59 +0000", "path": "/hello-file", "is_dir": false, "icon": "page_white", "root": "app_folder", "mime_type": "application/octet-stream", "size": "0 bytes"}`
		w.Write([]byte(b))
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		c := client{}
		stat, err := c.Delete("hello-file")
		assert.NoError(t, err)
		assert.Equal(t, stat.Rev, "15a1441bd38")
		assert.True(t, stat.IsDeleted)
		assert.False(t, stat.ThumbExists)
		assert.Equal(t, stat.Bytes, 0)
		assert.Equal(t, stat.Modified, "Sun, 29 Sep 2013 18:04:57 +0000")
		assert.Equal(t, stat.ClientMtime, "Wed, 31 Dec 1969 23:59:59 +0000")
		assert.Equal(t, stat.Path, "/hello-file")
		assert.False(t, stat.IsDir)
		assert.Equal(t, stat.Icon, "page_white")
		assert.Equal(t, stat.Root, "app_folder")
		assert.Equal(t, stat.MimeType, "application/octet-stream")
		assert.Equal(t, stat.Size, "0 bytes")
	})
}
Пример #2
0
func Test_client_Stat_File(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := `{"revision": 363, "rev": "16b1441bd38", "thumb_exists": false, "bytes": 13, "modified": "Sun, 29 Sep 2013 18:58:40 +0000", "client_mtime": "Sun, 29 Sep 2013 18:58:40 +0000", "path": "/hello-file", "is_dir": false, "icon": "page_white", "root": "app_folder", "mime_type": "application/octet-stream", "size": "13 bytes"}`
		w.Write([]byte(b))
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		c := client{}
		stat, err := c.Stat("hello-directory/foobar", nil)
		assert.NoError(t, err)
		assert.Equal(t, stat.Rev, "16b1441bd38")
		assert.Equal(t, stat.Bytes, 13)
		assert.Equal(t, stat.Modified, "Sun, 29 Sep 2013 18:58:40 +0000")
		assert.Equal(t, stat.ClientMtime, "Sun, 29 Sep 2013 18:58:40 +0000")
		assert.Equal(t, stat.Path, "/hello-file")
		assert.Equal(t, stat.Root, "app_folder")
		assert.False(t, stat.IsDir)
		assert.False(t, stat.IsDeleted)
		assert.Equal(t, stat.Icon, "page_white")
		assert.Equal(t, stat.Size, "13 bytes")
		assert.False(t, stat.ThumbExists)
		assert.Equal(t, stat.MimeType, "application/octet-stream")
	})
}
Пример #3
0
func Test_client_Stat_Dir(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := `{"hash": "30c75756a82043e6837af4a63be09d05", "revision": 365, "rev": "16d1441bd38", "thumb_exists": false, "bytes": 0, "modified": "Sun, 29 Sep 2013 19:06:01 +0000", "path": "/hello-directory", "is_dir": true, "icon": "folder", "root": "app_folder", "contents": [{"revision": 366, "rev": "16e1441bd38", "thumb_exists": false, "bytes": 0, "modified": "Sun, 29 Sep 2013 19:06:01 +0000", "path": "/hello-directory/foobar", "is_dir": true, "icon": "folder", "root": "dropbox", "size": "0 bytes"}], "size": "0 bytes"}`
		w.Write([]byte(b))
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		c := client{}
		stat, err := c.Stat("hello-directory", nil)
		assert.NoError(t, err)
		assert.Equal(t, stat.Rev, "16d1441bd38")
		assert.Equal(t, stat.Hash, "30c75756a82043e6837af4a63be09d05")
		assert.Equal(t, stat.Revision, 365)
		assert.False(t, stat.ThumbExists)
		assert.Equal(t, stat.Bytes, 0)
		assert.Equal(t, stat.Modified, "Sun, 29 Sep 2013 19:06:01 +0000")
		assert.Equal(t, stat.ClientMtime, "")
		assert.Equal(t, stat.Path, "/hello-directory")
		assert.Equal(t, stat.Root, "app_folder")
		assert.True(t, stat.IsDir)
		assert.Equal(t, stat.IsDeleted, false)
		assert.Equal(t, stat.Icon, "folder")
		assert.Equal(t, stat.Size, "0 bytes")
		assert.Equal(t, stat.ThumbExists, false)
	})
}
Пример #4
0
func Test_client_Move(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := `{"hash": "2b277c1fe6420488dd5bd0db40ed7753", "revision": 394, "rev": "18a1441bd38", "thumb_exists": false, "bytes": 0, "modified": "Sun, 20 Oct 2013 15:17:47 +0000", "path": "/experiments/client/move/bar", "is_dir": true, "icon": "folder", "root": "app_folder", "contents": [], "size": "0 bytes"}`
		w.Write([]byte(b))
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		c := client{}
		stat, err := c.Move("foo", "bar")
		assert.NoError(t, err)
		assert.Equal(t, stat.Rev, "18a1441bd38")
		assert.Equal(t, stat.Hash, "2b277c1fe6420488dd5bd0db40ed7753")
		assert.Equal(t, stat.Revision, 394)
		assert.False(t, stat.ThumbExists)
		assert.Equal(t, stat.Bytes, 0)
		assert.Equal(t, stat.Modified, "Sun, 20 Oct 2013 15:17:47 +0000")
		assert.Equal(t, stat.ClientMtime, "")
		assert.Equal(t, stat.Path, "/experiments/client/move/bar")
		assert.Equal(t, stat.Root, "app_folder")
		assert.True(t, stat.IsDir)
		assert.Equal(t, stat.IsDeleted, false)
		assert.Equal(t, stat.Icon, "folder")
		assert.Equal(t, stat.Size, "0 bytes")
		assert.Equal(t, stat.ThumbExists, false)
	})
}
func Test_datastore_getSnapshot(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := `{"rows": [{"tid": "tasks", "data": {"taskname": "mow the lawn", "completed": false, "created": {"T": "1379279475645"}}, "rowid": "_176ea3j26i8_js_7ycGk"}, {"tid": "tasks", "data": {"taskname": "do the laundry", "completed": true, "created": {"T": "1379277915417"}}, "rowid": "_176e8l33qd8_js_cjDMS"}, {"tid": "tasks", "data": {"taskname": "do the dishes", "completed": false, "created": {"T": "1379430400763"}}, "rowid": "_176imlgfa30_js_FCHyT"}, {"tid": "tasks", "data": {"taskname": "wash clothes", "completed": false, "created": {"T": "1379277963243"}}, "rowid": "_176e8mgnbfo_js_INijK"}], "rev": 22}`
		w.Write([]byte(b))
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		ds := datastore{}

		snap, err := ds.getSnapshot()
		assert.NoError(t, err)

		assert.Equal(t, snap.Rev, 22)
		assert.Equal(t, len(snap.Rows), 4)

		for i, _ := range snap.Rows {
			r := snap.Rows[i].toRow()
			d := r.Data

			switch i {

			case 0:
				assert.Equal(t, r.TableKey, "tasks")
				assert.Equal(t, r.RowKey, "_176ea3j26i8_js_7ycGk")
				assert.Equal(t, len(d), 3)
				assert.Equal(t, d["taskname"], "mow the lawn")
				assert.Equal(t, d["completed"], false)
				assert.Equal(t, d["created"], time.Unix(0, 1379279475645*1000000))

			case 1:
				assert.Equal(t, r.TableKey, "tasks")
				assert.Equal(t, r.RowKey, "_176e8l33qd8_js_cjDMS")
				assert.Equal(t, len(d), 3)
				assert.Equal(t, d["taskname"], "do the laundry")
				assert.Equal(t, d["completed"], true)
				assert.Equal(t, d["created"], time.Unix(0, 1379277915417*1000000))

			case 2:
				assert.Equal(t, r.TableKey, "tasks")
				assert.Equal(t, r.RowKey, "_176imlgfa30_js_FCHyT")
				assert.Equal(t, len(d), 3)
				assert.Equal(t, d["taskname"], "do the dishes")
				assert.Equal(t, d["completed"], false)
				assert.Equal(t, d["created"], time.Unix(0, 1379430400763*1000000))

			case 3:
				assert.Equal(t, r.TableKey, "tasks")
				assert.Equal(t, r.RowKey, "_176e8mgnbfo_js_INijK")
				assert.Equal(t, len(d), 3)
				assert.Equal(t, d["taskname"], "wash clothes")
				assert.Equal(t, d["completed"], false)
				assert.Equal(t, d["created"], time.Unix(0, 1379277963243*1000000))

			}
		}
	})
}
func Test_dsm_getDatastoreInfo_ErrorPath(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "BadRequest", http.StatusBadRequest)
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		dsm := datastoreManager{}
		_, e := dsm.getDatastoreInfo("some_datastore_id")
		assert.Error(t, e)
	})
}
func Test_client_FindByName(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := `[{"revision": 485, "rev": "1e51441bd38", "thumb_exists": false, "bytes": 0, "modified": "Sun, 20 Oct 2013 17:26:59 +0000", "path": "/experiments/client/find_by_name/fooA", "is_dir": true, "icon": "folder", "root": "dropbox", "size": "0 bytes"}, {"revision": 486, "rev": "1e61441bd38", "thumb_exists": false, "bytes": 0, "modified": "Sun, 20 Oct 2013 17:26:59 +0000", "path": "/experiments/client/find_by_name/fooB", "is_dir": true, "icon": "folder", "root": "dropbox", "size": "0 bytes"}]`
		w.Write([]byte(b))
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		c := client{}
		stats, err := c.FindByName("foo", "bar", nil)
		assert.NoError(t, err)
		assert.Equal(t, len(stats), 2)
	})
}
func Test_dsm_OpenDefaultDatastore(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := `{"handle": "bbb", "rev": 33, "created": false}`
		w.Write([]byte(b))
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		dsm := datastoreManager{}
		ds, err := dsm.OpenDefaultDatastore()
		assert.NoError(t, err)
		assert.Equal(t, ds.GetId(), "default")
	})
}
Пример #9
0
func Test_client_MakeURL(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := `{"url": "https://db.tt/f9akEL4H", "expires": "Tue, 01 Jan 2030 00:00:00 +0000"}`
		w.Write([]byte(b))
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		c := client{}
		share, err := c.MakeURL("foobar/binbaz", nil)
		assert.NoError(t, err)
		assert.Equal(t, share.URL.Path, "/f9akEL4H")
		assert.Equal(t, share.Expires.Year(), 2030)
	})
}
func Test_client_requestJSON_HappyPath(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(`{ "hello": "world" }`))
	})
	defer server.Close()

	req, err := http.NewRequest("GET", server.URL, nil)
	assert.NoError(t, err)

	var foo struct{ Hello string }

	c := client{}
	c.requestJSON(req, &foo)

	assert.Equal(t, foo.Hello, "world")
}
func Test_dsm_getDatastoreInfo_HappyPath(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := `{"handle": "lzBkwO43xw9mrUX5BGdjxIzCmsmdCK", "rev": 0}`
		w.Write([]byte(b))
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		dsm := datastoreManager{}

		info, e := dsm.getDatastoreInfo("some_datastore_id")
		assert.NoError(t, e)
		assert.Equal(t, info.Id, "some_datastore_id")
		assert.Equal(t, info.Handle, "lzBkwO43xw9mrUX5BGdjxIzCmsmdCK")
		assert.Equal(t, info.Rev, 0)
	})
}
func Test_dsm_getOrCreateDatastore_Created_True(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := `{"handle": "bbb", "rev": 10, "created": true}`
		w.Write([]byte(b))
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		dsm := datastoreManager{}
		info, err := dsm.getOrCreateDatastore("binbar")
		assert.NoError(t, err)
		assert.Equal(t, info.Id, "binbar")
		assert.True(t, info.Created)
		assert.Equal(t, info.Handle, "bbb")
		assert.Equal(t, info.Rev, 10)
	})
}
func Test_client_requestBody_ErrorPath(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "BadRequest", http.StatusBadRequest)
	})
	defer server.Close()

	req, err := http.NewRequest("GET", server.URL, nil)
	assert.NoError(t, err)

	c := client{
		options: ClientOptions{
			Token: "init__token",
		}}

	_, err = c.requestBody(req)
	assert.Error(t, err)
}
func Test_client_requestBody_HappyPath(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("x"))
	})
	defer server.Close()

	req, err := http.NewRequest("GET", server.URL, nil)
	assert.NoError(t, err)

	c := client{
		options: ClientOptions{
			Token: "init__token",
		}}

	body, err := c.requestBody(req)
	assert.NoError(t, err)
	assert.Equal(t, body, []byte("x"))
}
func Test_dsm_createDatastore(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := `{"handle": "KB0Pxo49ejanvbwfnn6dYYDbr5BmFf", "rev": 0, "created": true}`
		w.Write([]byte(b))
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		dsm := datastoreManager{}

		info, e := dsm.createDatastore()
		assert.NoError(t, e)
		assert.Equal(t, info.Id, "")
		assert.Equal(t, info.Created, true)
		assert.Equal(t, info.Handle, "KB0Pxo49ejanvbwfnn6dYYDbr5BmFf")
		assert.Equal(t, info.Rev, 0)
	})
}
func Test_dsm_DeleteDatastore_HappyPath(t *testing.T) {
	requestCount := 0
	responses := []string{
		`{"handle": "lzBkwO43xw9mrUX5BGdjxIzCmsmdCK", "rev": 0}`,
		`{"ok": "Deleted datastore with handle: u'lzBkwO43xw9mrUX5BGdjxIzCmsmdCK'"}`,
	}

	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := responses[requestCount]
		w.Write([]byte(b))
		requestCount++
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		dsm := datastoreManager{}
		e := dsm.DeleteDatastore("another_datastore_id")
		assert.NoError(t, e)
	})
}
func Test_dsm_ListDatastores_HappyPath(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := `{"datastores": [{"handle": "X21bXh1EfdZsnI1cTcdepkRpdWeEYL", "rev": 3, "dsid": "default"}], "token": "ae"}`
		w.Write([]byte(b))
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		dsm := datastoreManager{}

		infos, e := dsm.ListDatastores()
		assert.NoError(t, e)
		assert.True(t, len(infos) == 1)
		if len(infos) > 0 {
			info := infos[0]
			assert.Equal(t, info.Id, "default")
			assert.Equal(t, info.Created, false)
			assert.Equal(t, info.Handle, "X21bXh1EfdZsnI1cTcdepkRpdWeEYL")
			assert.Equal(t, info.Rev, 3)
		}
	})
}
Пример #18
0
func Test_client_Mkdir(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := `{"revision": 348, "rev": "15c1441bd38", "thumb_exists": false, "bytes": 0, "modified": "Sun, 29 Sep 2013 18:35:23 +0000", "path": "/hello-directory/foobar", "is_dir": true, "icon": "folder", "root": "app_folder", "size": "0 bytes"}`
		w.Write([]byte(b))
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		c := client{}
		stat, err := c.Mkdir("hello-directory/foobar")
		assert.NoError(t, err)
		assert.Equal(t, stat.Rev, "15c1441bd38")
		assert.Equal(t, stat.Bytes, 0)
		assert.Equal(t, stat.Modified, "Sun, 29 Sep 2013 18:35:23 +0000")
		assert.Equal(t, stat.Path, "/hello-directory/foobar")
		assert.Equal(t, stat.Root, "app_folder")
		assert.True(t, stat.IsDir)
		assert.False(t, stat.IsDeleted)
		assert.Equal(t, stat.Icon, "folder")
		assert.Equal(t, stat.Size, "0 bytes")
		assert.False(t, stat.ThumbExists)
	})
}
func Test_client_GetAccountInfo(t *testing.T) {
	server := test_helpers.NewServer(func(w http.ResponseWriter, r *http.Request) {
		b := `{"referral_link": "https://db.tt/t5R5HR7e", "display_name": "Levi Cook", "uid": 608456, "country": "US", "quota_info": {"datastores": 2, "shared": 27129455635, "quota": 111400714240, "normal": 463460503}, "email": "*****@*****.**"}`
		w.Write([]byte(b))
	})
	defer server.Close()

	withServer(t, server.URL, func() {
		c := client{}
		info, err := c.GetAccountInfo()
		assert.NoError(t, err)
		assert.Equal(t, info.ReferralLink, "https://db.tt/t5R5HR7e")
		assert.Equal(t, info.Uid, uint(608456))
		assert.Equal(t, info.DisplayName, "Levi Cook")
		assert.Equal(t, info.Email, "*****@*****.**")
		assert.Equal(t, info.Country, "US")

		quota := info.QuotaInfo
		assert.Equal(t, quota.Normal, 463460503)
		assert.Equal(t, quota.Shared, 27129455635)
		assert.Equal(t, quota.Quota, 111400714240)
		assert.Equal(t, quota.Datastores, 2)
	})
}