You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudreve/pkg/wopi/discovery_test.go

130 lines
75 KiB

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?&lt;ui=UI_LLCC&amp;&gt;&lt;rs=DC_LLCC&amp;&gt;&lt;dchat=DISABLE_CHAT&amp;&gt;&lt;hid=HOST_SESSION_ID&amp;&gt;&lt;IsLicensedUser=BUSINESS_USER&amp;&gt;&lt;actnavid=ACTIVITY_NAVIGATION_ID&amp;&gt;" /><action name="view" ext="ods" default="true" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?&lt;ui=UI_LLCC&amp;&gt;&lt;rs=DC_LLCC&amp;&gt;&lt;dchat=DISABLE_CHAT&amp;&gt;&lt;hid=HOST_SESSION_ID&amp;&gt;&lt;IsLicensedUser=BUSINESS_USER&amp;&gt;&lt;actnavid=ACTIVITY_NAVIGATION_ID&amp;&gt;" /><action name="view" ext="xls" default="true" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?&lt;ui=UI_LLCC&amp;&gt;&lt;rs=DC_LLCC&amp;&gt;&lt;dchat=DISABLE_CHAT&amp;&gt;&lt;hid=HOST_SESSION_ID&amp;&gt;&lt;IsLicensedUser=BUSINESS_USER&amp;&gt;&lt;actnavid=ACTIVITY_NAVIGATION_ID&amp;&gt;" /><action name="view" ext="xlsb" default="true" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?&lt;ui=UI_LLCC&amp;&gt;&lt;rs=DC_LLCC&amp;&gt;&lt;dchat=DISABLE_CHAT&amp;&gt;&lt;hid=HOST_SESSION_ID&amp;&gt;&lt;IsLicensedUser=BUSINESS_USER&amp;&gt;&lt;actnavid=ACTIVITY_NAVIGATION_ID&amp;&gt;" /><action name="view" ext="xlsm" default="true" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?&lt;ui=UI_LLCC&amp;&gt;&lt;rs=DC_LLCC&amp;&gt;&lt;dchat=DISABLE_CHAT&amp;&gt;&lt;hid=HOST_SESSION_ID&amp;&gt;&lt;IsLicensedUser=BUSINESS_USER&amp;&gt;&lt;actnavid=ACTIVITY_NAVIGATION_ID&amp;&gt;" /><action name="view" ext="xlsx" default="true" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?&lt;ui=UI_LLCC&amp;&gt;&lt;rs=DC_LLCC&amp;&gt;&lt;dchat=DISABLE_CHAT&amp;&gt;&lt;hid=HOST_SESSION_ID&amp;&gt;&lt;IsLicensedUser=BUSINESS_USER&amp;&gt;&lt;actnavid=ACTIVITY_NAVIGATION_ID&amp;&gt;" /><action name="edit" ext="ods" requires="update" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?edit=1&amp;&lt;ui=UI_LLCC&amp;&gt;&lt;rs=DC_LLCC&amp;&gt;&lt;dchat=DISABLE_CHAT&amp;&gt;&lt;hid=HOST_SESSION_ID&amp;&gt;&lt;IsLicensedUser=BUSINESS_USER&amp;&gt;&lt;actnavid=ACTIVITY_NAVIGATION_ID&amp;&gt;" /><action name="edit" ext="xlsb" requires="update" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?edit=1&amp;&lt;ui=UI_LLCC&amp;&gt;&lt;rs=DC_LLCC&amp;&gt;&lt;dchat=DISABLE_CHAT&amp;&gt;&lt;hid=HOST_SESSION_ID&amp;&gt;&lt;IsLicensedUser=BUSINESS_USER&amp;&gt;&lt;actnavid=ACTIVITY_NAVIGATION_ID&amp;&gt;" /><action name="edit" ext="xlsm" requires="update" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?edit=1&amp;&lt;ui=UI_LLCC&amp;&gt;&lt;rs=DC_LLCC&amp;&gt;&lt;dchat=DISABLE_CHAT&amp;&gt;&lt;hid=HOST_SESSION_ID&amp;&gt;&lt;IsLicensedUser=BUSINESS_USER&amp;&gt;&lt;actnavid=ACTIVITY_NAVIGATION_ID&amp;&gt;" /><action name="edit" ext="xlsx" requires="update" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?edit=1&amp;&lt;ui=UI_LLCC&amp;&gt;&lt;rs=DC_LLCC&amp;&gt;&lt;dchat=DISABLE_CHAT&amp;&gt;&lt;hid=HOST_SESSION_ID&amp;&gt;&lt;IsLicensedUser=BUSINESS_USER&amp;&gt;&lt;actnavid=ACTIVITY_NAVIGATION_ID&amp;&gt;" /><action name="editnew" ext="ods" requires="update" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?edit=1&amp;new=1&amp;&lt;ui=UI_LLCC&amp;&gt;&lt;rs=DC_LLCC&amp;&gt;&lt;dchat=DISABLE_CHAT&amp;&gt;&lt;hid=HOST_SESSION_ID&amp;&gt;&lt;IsLicensedUser=BUSINESS_USER&amp;&gt;" /><action name="editnew" ext="xlsx" requires="update" urlsrc="https://test.cloudreve.org/x/_layouts/xlviewerinternal.aspx?edit=1&amp;new=1&amp;&lt;ui=UI_LLCC&amp;&gt;&lt;rs=DC_LLCC&amp;&gt;&lt;dchat=DISABLE_CHAT&amp;&gt;&lt;hid=HOST_SESSION_ID&amp;&gt;&lt;IsLicensedUser=BUSINESS_USER&amp;&gt;" /><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)
}
}