|
|
|
package wopi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/mocks/requestmock"
|
|
|
|
"github.com/cloudreve/Cloudreve/v3/pkg/request"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
testMock "github.com/stretchr/testify/mock"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestClient_AvailableExts(t *testing.T) {
|
|
|
|
a := assert.New(t)
|
|
|
|
endpoint, _ := url.Parse("http://localhost:8001/hosting/discovery")
|
|
|
|
client := &client{
|
|
|
|
cache: cache.NewMemoStore(),
|
|
|
|
config: config{
|
|
|
|
discoveryEndpoint: endpoint,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discovery failed
|
|
|
|
{
|
|
|
|
expectedErr := errors.New("error")
|
|
|
|
mockHttp := &requestmock.RequestMock{}
|
|
|
|
client.http = mockHttp
|
|
|
|
mockHttp.On(
|
|
|
|
"Request",
|
|
|
|
"GET",
|
|
|
|
endpoint.String(),
|
|
|
|
testMock.Anything,
|
|
|
|
testMock.Anything,
|
|
|
|
).Return(&request.Response{
|
|
|
|
Err: expectedErr,
|
|
|
|
})
|
|
|
|
res := client.AvailableExts()
|
|
|
|
a.Empty(res)
|
|
|
|
mockHttp.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// pass
|
|
|
|
{
|
|
|
|
client.discovery = &WopiDiscovery{}
|
|
|
|
client.actions = map[string]map[string]Action{
|
|
|
|
".doc": {
|
|
|
|
string(ActionPreviewFallback): Action{},
|
|
|
|
},
|
|
|
|
".ppt": {},
|
|
|
|
".xls": {
|
|
|
|
"not_supported": Action{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
res := client.AvailableExts()
|
|
|
|
a.Len(res, 1)
|
|
|
|
a.Equal("doc", res[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient_RefreshDiscovery(t *testing.T) {
|
|
|
|
a := assert.New(t)
|
|
|
|
endpoint, _ := url.Parse("http://localhost:8001/hosting/discovery")
|
|
|
|
client := &client{
|
|
|
|
cache: cache.NewMemoStore(),
|
|
|
|
config: config{
|
|
|
|
discoveryEndpoint: endpoint,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// cache hit
|
|
|
|
{
|
|
|
|
client.cache.Set(DiscoverResponseCacheKey, WopiDiscovery{Text: "test"}, 0)
|
|
|
|
a.NoError(client.checkDiscovery())
|
|
|
|
a.Equal("test", client.discovery.Text)
|
|
|
|
client.discovery = &WopiDiscovery{}
|
|
|
|
client.cache.Delete([]string{DiscoverResponseCacheKey}, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// malformed xml
|
|
|
|
{
|
|
|
|
mockHttp := &requestmock.RequestMock{}
|
|
|
|
client.http = mockHttp
|
|
|
|
mockHttp.On(
|
|
|
|
"Request",
|
|
|
|
"GET",
|
|
|
|
endpoint.String(),
|
|
|
|
testMock.Anything,
|
|
|
|
testMock.Anything,
|
|
|
|
).Return(&request.Response{
|
|
|
|
Response: &http.Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: io.NopCloser(strings.NewReader(`{"code":203}`)),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
res := client.refreshDiscovery()
|
|
|
|
a.ErrorContains(res, "failed to parse")
|
|
|
|
mockHttp.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// all pass
|
|
|
|
{
|
|
|
|
testResponse := `
|
|
|
|
<?xml version="1.0" encoding="utf-8"?><wopi-discovery><net-zone name="external-https"><app name="Excel" favIconUrl="https://test.cloudreve.org/x/_layouts/resources/FavIcon_Excel.ico" bootstrapperUrl="https://document/x/_layouts/app_scripts/excel-boot.min.js" checkLicense="true"><action name="view" ext="csv" default="true" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?<ui=UI_LLCC&><rs=DC_LLCC&><dchat=DISABLE_CHAT&><hid=HOST_SESSION_ID&><IsLicensedUser=BUSINESS_USER&><actnavid=ACTIVITY_NAVIGATION_ID&>" /><action name="view" ext="ods" default="true" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?<ui=UI_LLCC&><rs=DC_LLCC&><dchat=DISABLE_CHAT&><hid=HOST_SESSION_ID&><IsLicensedUser=BUSINESS_USER&><actnavid=ACTIVITY_NAVIGATION_ID&>" /><action name="view" ext="xls" default="true" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?<ui=UI_LLCC&><rs=DC_LLCC&><dchat=DISABLE_CHAT&><hid=HOST_SESSION_ID&><IsLicensedUser=BUSINESS_USER&><actnavid=ACTIVITY_NAVIGATION_ID&>" /><action name="view" ext="xlsb" default="true" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?<ui=UI_LLCC&><rs=DC_LLCC&><dchat=DISABLE_CHAT&><hid=HOST_SESSION_ID&><IsLicensedUser=BUSINESS_USER&><actnavid=ACTIVITY_NAVIGATION_ID&>" /><action name="view" ext="xlsm" default="true" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?<ui=UI_LLCC&><rs=DC_LLCC&><dchat=DISABLE_CHAT&><hid=HOST_SESSION_ID&><IsLicensedUser=BUSINESS_USER&><actnavid=ACTIVITY_NAVIGATION_ID&>" /><action name="view" ext="xlsx" default="true" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?<ui=UI_LLCC&><rs=DC_LLCC&><dchat=DISABLE_CHAT&><hid=HOST_SESSION_ID&><IsLicensedUser=BUSINESS_USER&><actnavid=ACTIVITY_NAVIGATION_ID&>" /><action name="edit" ext="ods" requires="update" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?edit=1&<ui=UI_LLCC&><rs=DC_LLCC&><dchat=DISABLE_CHAT&><hid=HOST_SESSION_ID&><IsLicensedUser=BUSINESS_USER&><actnavid=ACTIVITY_NAVIGATION_ID&>" /><action name="edit" ext="xlsb" requires="update" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?edit=1&<ui=UI_LLCC&><rs=DC_LLCC&><dchat=DISABLE_CHAT&><hid=HOST_SESSION_ID&><IsLicensedUser=BUSINESS_USER&><actnavid=ACTIVITY_NAVIGATION_ID&>" /><action name="edit" ext="xlsm" requires="update" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?edit=1&<ui=UI_LLCC&><rs=DC_LLCC&><dchat=DISABLE_CHAT&><hid=HOST_SESSION_ID&><IsLicensedUser=BUSINESS_USER&><actnavid=ACTIVITY_NAVIGATION_ID&>" /><action name="edit" ext="xlsx" requires="update" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?edit=1&<ui=UI_LLCC&><rs=DC_LLCC&><dchat=DISABLE_CHAT&><hid=HOST_SESSION_ID&><IsLicensedUser=BUSINESS_USER&><actnavid=ACTIVITY_NAVIGATION_ID&>" /><action name="editnew" ext="ods" requires="update" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?edit=1&new=1&<ui=UI_LLCC&><rs=DC_LLCC&><dchat=DISABLE_CHAT&><hid=HOST_SESSION_ID&><IsLicensedUser=BUSINESS_USER&>" /><action name="editnew" ext="xlsx" requires="update" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?edit=1&new=1&<ui=UI_LLCC&><rs=DC_LLCC&><dchat=DISABLE_CHAT&><hid=HOST_SESSION_ID&><IsLicensedUser=BUSINESS_USER&>" /><action name="interactivepreview" ext="xlsb" urlsrc="ht
|
|
|
|
mockHttp := &requestmock.RequestMock{}
|
|
|
|
client.http = mockHttp
|
|
|
|
mockHttp.On(
|
|
|
|
"Request",
|
|
|
|
"GET",
|
|
|
|
endpoint.String(),
|
|
|
|
testMock.Anything,
|
|
|
|
testMock.Anything,
|
|
|
|
).Return(&request.Response{
|
|
|
|
Response: &http.Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: io.NopCloser(strings.NewReader(testResponse)),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
res := client.refreshDiscovery()
|
|
|
|
a.NoError(res, res)
|
|
|
|
a.NotEmpty(client.actions[".docx"])
|
|
|
|
a.NotEmpty(client.actions[".docx"][string(ActionPreview)])
|
|
|
|
a.NotEmpty(client.actions[".docx"][string(ActionEdit)])
|
|
|
|
mockHttp.AssertExpectations(t)
|
|
|
|
}
|
|
|
|
}
|