Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch share again via OCS request if it's not in the response #4988

Merged
merged 5 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions packages/web-app-files/src/views/SharedWithMe.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@
>
<span id="files-list-count-folders" v-text="activeFilesCount.folders" />
<translate :translate-n="activeFilesCount.folders" translate-plural="folders"
>folder</translate
>
>folder
</translate>
<translate>and</translate>
<span id="files-list-count-files" v-text="activeFilesCount.files" />
<translate :translate-n="activeFilesCount.files" translate-plural="files"
>file</translate
>
>file
</translate>
</div>
</template>
</oc-table-files>
Expand Down Expand Up @@ -226,15 +226,36 @@ export default {

async triggerShareAction(resource, type) {
try {
// exec share action
let response = await this.$client.requests.ocs({
service: 'apps/files_sharing',
action: `api/v1/shares/pending/${resource.share.id}`,
method: type
})
response = await response.json()
if (response.ocs.data.length > 0) {

// exit on failure
if (response.status !== 200) {
throw new Error(response.statusText)
}
// get updated share from response or re-fetch it
let share = null
// oc10
if (parseInt(response.headers.get('content-length')) > 0) {
response = await response.json()

if (response.ocs.data.length > 0) {
share = response.ocs.data[0]
}
} else {
// ocis
const { shareInfo } = await this.$client.shares.getShare(resource.share.id)
share = shareInfo
}

// update share in store
if (share) {
const sharedResource = await buildSharedResource(
response.ocs.data[0],
share,
true,
!this.isOcis,
this.configuration.server,
Expand Down
81 changes: 81 additions & 0 deletions packages/web-app-files/tests/views/SharedWithMe.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { mount, createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'
import OwnCloud from 'owncloud-sdk'
import SharedWithMe from '../../src/views/SharedWithMe'
import { createStore } from 'vuex-extensions'
import DesignSystem from 'owncloud-design-system'

const createFile = ({ id, status = 1, type = 'folder' }) => ({
id: `file-id-${id}`,
type,
status,
name: `file-name-${id}`,
path: `/file-path/${id}`,
extension: '',
share: {
id: `file-share-id-${id}`
},
indicators: []
})

const localVue = createLocalVue()
localVue.prototype.$client = new OwnCloud()
localVue.prototype.$client.init({ baseUrl: 'http://none.de' })
localVue.use(Vuex)
localVue.use(DesignSystem)

export const store = createStore(Vuex.Store, {
getters: {
configuration: () => ({
options: {
disablePreviews: true
}
}),
getToken: () => '',
isOcis: () => true
},
modules: {
Files: {
state: {
resource: null
},
getters: {
selectedFiles: () => [],
activeFiles: () => [createFile({ id: 1 }), createFile({ id: 2, status: 2 })],
activeFilesCount: () => ({ files: 0, folders: 1 }),
inProgress: () => [null],
highlightedFile: () => null
},
mutations: {
UPDATE_RESOURCE: (state, resource) => {
state.resource = resource
}
},
namespaced: true
}
}
})

export const wrapper = mount(
{ ...SharedWithMe, created: jest.fn(), mounted: jest.fn() },
{
localVue,
store,
stubs: {
'router-link': true,
translate: true
},
data: () => ({
loading: false
})
}
)

jest.mock('../../src/helpers/resources', () => ({
buildSharedResource: jest.fn(share => share)
}))

export const showMessage = jest.spyOn(wrapper.vm, 'showMessage').mockImplementation(v => v)
export const getShare = jest
.spyOn(localVue.prototype.$client.shares, 'getShare')
.mockImplementation(v => v)
66 changes: 66 additions & 0 deletions packages/web-app-files/tests/views/SharedWithMe.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { wrapper, store, showMessage, getShare } from './SharedWithMe.setup'

describe('SharedWithMe component', () => {
describe('method triggerShareAction', () => {
beforeEach(() => {
fetch.resetMocks()
store.reset()
showMessage.mockClear()
getShare.mockClear()
})

it('errors if status is not 200', async () => {
const statusText = 'status is not 200'
fetch.mockResponse(new Error(), { status: 404, statusText })
await wrapper
.findAll('.file-row-share-status-action')
.at(1)
.trigger('click')

expect(showMessage.mock.results[0].value.desc).toBe(statusText)
})

it('returns a share if content-length header is present and valid (oc10)', async () => {
fetch.mockResponse(JSON.stringify({ ocs: { data: [{ id: 1 }] } }), {
headers: { 'content-length': 1 }
})
await wrapper
.findAll('.file-row-share-status-action')
.at(1)
.trigger('click')
await wrapper.vm.$nextTick()

expect(store.state.Files.resource).toMatchObject({ id: 1 })
expect(getShare).toBeCalledTimes(0)
})

it('errors if content-length header is not valid (oc10)', async () => {
fetch.mockResponse(JSON.stringify({ ocs: { data: [{ id: 1 }] } }), {
headers: { 'content-length': 0 }
})

await wrapper
.findAll('.file-row-share-status-action')
.at(1)
.trigger('click')
await wrapper.vm.$nextTick()

expect(showMessage.mock.results[0]).toBeFalsy()
})

it('returns a share if content-length header is not present (ocis)', async () => {
const shareInfo = { id: 1, shareinfo: true }
fetch.mockResponse(JSON.stringify({ ocs: { data: [{ id: 1 }] } }), {})
getShare.mockReturnValueOnce({ shareInfo })

await wrapper
.findAll('.file-row-share-status-action')
.at(1)
.trigger('click')
await wrapper.vm.$nextTick()

expect(store.state.Files.resource).toMatchObject(shareInfo)
expect(getShare).toBeCalledTimes(1)
})
})
})
2 changes: 1 addition & 1 deletion packages/web-runtime/tests/components/TopBar.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { shallowMount, createLocalVue } from '@vue/test-utils'
import Vuex from 'vuex'
import TopBar from 'web-runtime/src/components/TopBar.vue'
import stubs from '../../../../tests/unit/config/stubs'
import stubs from '../../../../tests/unit/stubs'

const localVue = createLocalVue()
localVue.use(Vuex)
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/config/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = {
'.*\\.(vue)$': 'vue-jest',
'^.+\\.svg$': 'jest-svg-transformer'
},
moduleNameMapper: {
'\\.(css|less)$': '<rootDir>/tests/unit/mocks/style.js'
},
transformIgnorePatterns: ['<rootDir>/node_modules/(?!lodash-es)'],
setupFiles: ['<rootDir>/tests/unit/config/jest.init.js'],
snapshotSerializers: ['jest-serializer-vue'],
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/config/jest.init.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { config } from '@vue/test-utils'
import fetchMock from 'jest-fetch-mock'

fetchMock.enableMocks()
try {
jest.setMock('cross-fetch', fetchMock)
jest.setMock('sync-fetch', fetchMock)
} catch (error) {}

config.mocks = {
$gettext: str => str
Expand Down
1 change: 1 addition & 0 deletions tests/unit/mocks/style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}
File renamed without changes.