2016-02-06 17:22:52 +08:00
|
|
|
package drive
|
|
|
|
|
|
|
|
import (
|
2018-01-24 07:46:41 +08:00
|
|
|
"encoding/json"
|
2018-02-07 19:53:46 +08:00
|
|
|
"mime"
|
2016-02-06 17:22:52 +08:00
|
|
|
"testing"
|
|
|
|
|
2018-01-24 07:46:41 +08:00
|
|
|
"google.golang.org/api/drive/v3"
|
2017-09-02 01:54:14 +08:00
|
|
|
|
2016-06-12 22:06:02 +08:00
|
|
|
"github.com/pkg/errors"
|
2016-02-06 17:22:52 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-02-01 04:03:02 +08:00
|
|
|
const exampleExportFormats = `{
|
|
|
|
"application/vnd.google-apps.document": [
|
|
|
|
"application/rtf",
|
|
|
|
"application/vnd.oasis.opendocument.text",
|
|
|
|
"text/html",
|
|
|
|
"application/pdf",
|
|
|
|
"application/epub+zip",
|
|
|
|
"application/zip",
|
|
|
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
"text/plain"
|
|
|
|
],
|
|
|
|
"application/vnd.google-apps.spreadsheet": [
|
|
|
|
"application/x-vnd.oasis.opendocument.spreadsheet",
|
|
|
|
"text/tab-separated-values",
|
|
|
|
"application/pdf",
|
|
|
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
|
|
"text/csv",
|
|
|
|
"application/zip",
|
|
|
|
"application/vnd.oasis.opendocument.spreadsheet"
|
|
|
|
],
|
|
|
|
"application/vnd.google-apps.jam": [
|
|
|
|
"application/pdf"
|
|
|
|
],
|
|
|
|
"application/vnd.google-apps.script": [
|
|
|
|
"application/vnd.google-apps.script+json"
|
|
|
|
],
|
|
|
|
"application/vnd.google-apps.presentation": [
|
|
|
|
"application/vnd.oasis.opendocument.presentation",
|
|
|
|
"application/pdf",
|
|
|
|
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
|
|
"text/plain"
|
|
|
|
],
|
|
|
|
"application/vnd.google-apps.form": [
|
|
|
|
"application/zip"
|
|
|
|
],
|
|
|
|
"application/vnd.google-apps.drawing": [
|
|
|
|
"image/svg+xml",
|
|
|
|
"image/png",
|
|
|
|
"application/pdf",
|
|
|
|
"image/jpeg"
|
|
|
|
]
|
|
|
|
}`
|
|
|
|
|
|
|
|
// Load the example export formats into exportFormats for testing
|
|
|
|
func TestInternalLoadExampleExportFormats(t *testing.T) {
|
2018-07-24 23:14:23 +08:00
|
|
|
exportFormatsOnce.Do(func() {})
|
|
|
|
assert.NoError(t, json.Unmarshal([]byte(exampleExportFormats), &_exportFormats))
|
2018-02-07 19:53:46 +08:00
|
|
|
_exportFormats = fixMimeTypeMap(_exportFormats)
|
2018-02-01 04:03:02 +08:00
|
|
|
}
|
2018-01-24 07:46:41 +08:00
|
|
|
|
2016-02-06 17:22:52 +08:00
|
|
|
func TestInternalParseExtensions(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in string
|
|
|
|
want []string
|
|
|
|
wantErr error
|
|
|
|
}{
|
2018-02-07 19:53:46 +08:00
|
|
|
{"doc", []string{".doc"}, nil},
|
|
|
|
{" docx ,XLSX, pptx,svg", []string{".docx", ".xlsx", ".pptx", ".svg"}, nil},
|
|
|
|
{"docx,svg,Docx", []string{".docx", ".svg"}, nil},
|
|
|
|
{"docx,potato,docx", []string{".docx"}, errors.New(`couldn't find MIME type for extension ".potato"`)},
|
2016-02-06 17:22:52 +08:00
|
|
|
} {
|
2018-02-07 19:53:46 +08:00
|
|
|
extensions, gotErr := parseExtensions(test.in)
|
2016-06-12 22:06:02 +08:00
|
|
|
if test.wantErr == nil {
|
|
|
|
assert.NoError(t, gotErr)
|
|
|
|
} else {
|
|
|
|
assert.EqualError(t, gotErr, test.wantErr.Error())
|
|
|
|
}
|
2018-02-07 19:53:46 +08:00
|
|
|
assert.Equal(t, test.want, extensions)
|
2016-02-06 17:22:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test it is appending
|
2018-02-07 19:53:46 +08:00
|
|
|
extensions, gotErr := parseExtensions("docx,svg", "docx,svg,xlsx")
|
|
|
|
assert.NoError(t, gotErr)
|
|
|
|
assert.Equal(t, []string{".docx", ".svg", ".xlsx"}, extensions)
|
2016-02-06 17:22:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInternalFindExportFormat(t *testing.T) {
|
2018-07-24 23:14:23 +08:00
|
|
|
item := &drive.File{
|
|
|
|
Name: "file",
|
|
|
|
MimeType: "application/vnd.google-apps.document",
|
|
|
|
}
|
2016-02-06 17:22:52 +08:00
|
|
|
for _, test := range []struct {
|
|
|
|
extensions []string
|
|
|
|
wantExtension string
|
2018-01-24 07:46:41 +08:00
|
|
|
wantMimeType string
|
2016-02-06 17:22:52 +08:00
|
|
|
}{
|
|
|
|
{[]string{}, "", ""},
|
2018-02-07 19:53:46 +08:00
|
|
|
{[]string{".pdf"}, ".pdf", "application/pdf"},
|
|
|
|
{[]string{".pdf", ".rtf", ".xls"}, ".pdf", "application/pdf"},
|
|
|
|
{[]string{".xls", ".rtf", ".pdf"}, ".rtf", "application/rtf"},
|
|
|
|
{[]string{".xls", ".csv", ".svg"}, "", ""},
|
2016-02-06 17:22:52 +08:00
|
|
|
} {
|
|
|
|
f := new(Fs)
|
|
|
|
f.extensions = test.extensions
|
2018-07-24 23:14:23 +08:00
|
|
|
gotExtension, gotFilename, gotMimeType, gotIsDocument := f.findExportFormat(item)
|
2016-02-06 17:22:52 +08:00
|
|
|
assert.Equal(t, test.wantExtension, gotExtension)
|
2018-07-24 23:14:23 +08:00
|
|
|
if test.wantExtension != "" {
|
|
|
|
assert.Equal(t, item.Name+"."+gotExtension, gotFilename)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, "", gotFilename)
|
|
|
|
}
|
2018-01-24 07:46:41 +08:00
|
|
|
assert.Equal(t, test.wantMimeType, gotMimeType)
|
2018-07-24 23:14:23 +08:00
|
|
|
assert.Equal(t, true, gotIsDocument)
|
2016-02-06 17:22:52 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-07 19:53:46 +08:00
|
|
|
|
|
|
|
func TestMimeTypesToExtension(t *testing.T) {
|
|
|
|
for mimeType, extension := range _mimeTypeToExtension {
|
|
|
|
extensions, err := mime.ExtensionsByType(mimeType)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Contains(t, extensions, extension)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtensionToMimeType(t *testing.T) {
|
|
|
|
for mimeType, extension := range _mimeTypeToExtension {
|
|
|
|
gotMimeType := mime.TypeByExtension(extension)
|
|
|
|
mediatype, _, err := mime.ParseMediaType(gotMimeType)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, mimeType, mediatype)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExtensionsForExportFormats(t *testing.T) {
|
|
|
|
if _exportFormats == nil {
|
|
|
|
t.Error("exportFormats == nil")
|
|
|
|
}
|
|
|
|
for fromMT, toMTs := range _exportFormats {
|
|
|
|
for _, toMT := range toMTs {
|
|
|
|
if !isInternalMimeType(toMT) {
|
|
|
|
extensions, err := mime.ExtensionsByType(toMT)
|
|
|
|
assert.NoError(t, err, "invalid MIME type %q", toMT)
|
|
|
|
assert.NotEmpty(t, extensions, "No extension found for %q (from: %q)", fromMT, toMT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|