From 6069a9576807c5d251086bf4a391745b455445dc Mon Sep 17 00:00:00 2001 From: Angelina Filippova Date: Tue, 8 Sep 2020 21:02:45 +0300 Subject: [PATCH] Add tests for fetching media proxy settings, evicting single and multiple urls --- src/api/__mocks__/settings.js | 10 ++++ src/views/mediaProxyCache/index.vue | 5 +- test/views/mediaProxyCache/index.test.js | 72 ++++++++++++++++++++++-- test/views/mediaProxyCache/store.conf.js | 21 ------- 4 files changed, 80 insertions(+), 28 deletions(-) delete mode 100644 test/views/mediaProxyCache/store.conf.js diff --git a/src/api/__mocks__/settings.js b/src/api/__mocks__/settings.js index d5fd1a6f..c2243b9a 100644 --- a/src/api/__mocks__/settings.js +++ b/src/api/__mocks__/settings.js @@ -6,6 +6,16 @@ const configsWithTagPolicy = { { tuple: [':policies', ['Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy', 'Pleroma.Web.ActivityPub.MRF.TagPolicy']] }, { tuple: [':transparency', true] }, { tuple: [':transparency_exclusions', []] } + ] }, + { + group: ':pleroma', + key: ':media_proxy', + value: [ + { tuple: [':enabled', true] }, + { tuple: [':invalidation', [ + { tuple: [':provider', 'Pleroma.Web.MediaProxy.Invalidation.Script'] }, + { tuple: [':enabled', true] } + ]] } ] }], need_reboot: false } diff --git a/src/views/mediaProxyCache/index.vue b/src/views/mediaProxyCache/index.vue index 9d34aa2b..1892ddf8 100644 --- a/src/views/mediaProxyCache/index.vue +++ b/src/views/mediaProxyCache/index.vue @@ -146,7 +146,7 @@ export default { }) }, evictURL() { - const urls = this.urls.split(',').map(url => url.trim()).filter(el => el.length > 0) + const urls = this.splitUrls(this.urls) this.$store.dispatch('PurgeUrls', { urls, ban: this.ban }) this.urls = '' }, @@ -163,6 +163,9 @@ export default { }, removeUrl(url) { this.$store.dispatch('RemoveBannedUrls', [url]) + }, + splitUrls(urls) { + return urls.split(',').map(url => url.trim()).filter(el => el.length > 0) } } } diff --git a/test/views/mediaProxyCache/index.test.js b/test/views/mediaProxyCache/index.test.js index ac397c4f..23a593ff 100644 --- a/test/views/mediaProxyCache/index.test.js +++ b/test/views/mediaProxyCache/index.test.js @@ -3,8 +3,11 @@ import { mount, createLocalVue, config } from '@vue/test-utils' import flushPromises from 'flush-promises' import Element from 'element-ui' import MediaProxyCache from '@/views/mediaProxyCache/index' -import storeConfig from './store.conf' -import { cloneDeep } from 'lodash' +import app from '@/store/modules/app' +import mediaProxyCache from '@/store/modules/mediaProxyCache' +import settings from '@/store/modules/settings' +import user from '@/store/modules/user' +import getters from '@/store/getters' config.mocks["$t"] = () => {} @@ -17,11 +20,21 @@ jest.mock('@/api/nodeInfo') jest.mock('@/api/mediaProxyCache') jest.mock('@/api/settings') -describe('', () => { +describe('MediaProxy Cache Invalidation', () => { let store + let actions beforeEach(() => { - store = new Vuex.Store(cloneDeep(storeConfig)) + actions = { ...mediaProxyCache.actions, PurgeUrls: jest.fn() } + store = new Vuex.Store({ + modules: { + app, + mediaProxyCache: { ...mediaProxyCache, actions }, + user, + settings, + }, + getters + }) }) it('fetches initial list of urls', async (done) => { @@ -32,9 +45,56 @@ describe('', () => { }) await flushPromises() - console.log(store.state) - console.log(wrapper.html()) expect(wrapper.vm.urlsCount).toEqual(2) + expect(wrapper.vm.mediaProxyEnabled).toBeTruthy() + done() + }) + + it('evicts single url', async (done) => { + const wrapper = mount(MediaProxyCache, { + store, + localVue, + sync: false + }) + + await flushPromises() + const textarea = wrapper.find('.url-input textarea') + const button = wrapper.find('.url-input-container button') + const value = 'http://example.com/media/asdf89.jpg' + const expectedUrls = ['http://example.com/media/asdf89.jpg'] + + textarea.element.value = value + textarea.trigger('input') + button.trigger('click') + await flushPromises() + + expect(actions.PurgeUrls).toHaveBeenCalled() + expect(wrapper.vm.urls.length).toEqual(0) + expect(wrapper.vm.splitUrls(value)).toEqual(expectedUrls) + done() + }) + + it('evicts multiple urls', async (done) => { + const wrapper = mount(MediaProxyCache, { + store, + localVue, + sync: false + }) + + await flushPromises() + const textarea = wrapper.find('.url-input textarea') + const button = wrapper.find('.url-input-container button') + const value = ' http://example.com/media/asdf89.jpg,http://example.com/media/oi678lk.jpg, http://example.com/media/kdjhf87.jpg , http://example.com/media/98234sd.jpg' + const expectedUrls = ['http://example.com/media/asdf89.jpg', 'http://example.com/media/oi678lk.jpg', 'http://example.com/media/kdjhf87.jpg', 'http://example.com/media/98234sd.jpg'] + + textarea.element.value = value + textarea.trigger('input') + button.trigger('click') + await flushPromises() + + expect(actions.PurgeUrls).toHaveBeenCalled() + expect(wrapper.vm.urls.length).toEqual(0) + expect(wrapper.vm.splitUrls(value)).toEqual(expectedUrls) done() }) }) \ No newline at end of file diff --git a/test/views/mediaProxyCache/store.conf.js b/test/views/mediaProxyCache/store.conf.js deleted file mode 100644 index 199fc904..00000000 --- a/test/views/mediaProxyCache/store.conf.js +++ /dev/null @@ -1,21 +0,0 @@ -import app from '@/store/modules/app' -import mediaProxyCache from '@/store/modules/mediaProxyCache' -import user from '@/store/modules/user' -import users from '@/store/modules/users' -import reports from '@/store/modules/reports' -import settings from '@/store/modules/settings' -import status from '@/store/modules/status' -import getters from '@/store/getters' - -export default { - modules: { - app, - mediaProxyCache, - user, - users, - reports, - settings, - status - }, - getters -}