-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Avatar module * Add tests with mocked api for avatar module * Update dependencies to minor * Remove async/await * Add more gas to execTransaction
- Loading branch information
1 parent
0d4870f
commit aa78099
Showing
7 changed files
with
34,415 additions
and
22,664 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import checkAccount from '~/common/checkAccount'; | ||
import checkOptions from '~/common/checkOptions'; | ||
|
||
/** | ||
* Avatar submodule to upload and delete images from storage. | ||
* | ||
* @access private | ||
* | ||
* @param {Web3} web3 - Web3 instance | ||
* @param {Object} utils - utils module instance | ||
* | ||
* @return {Object} - avatar module instance | ||
*/ | ||
export default function createAvatarModule(web3, utils) { | ||
return { | ||
/** | ||
* Upload an avatar image to storage. | ||
* | ||
* @namespace core.avatar.upload | ||
* | ||
* @param {Object} account - web3 account instance | ||
* @param {Object} userOptions - options | ||
* @param {object} userOptions.data - avatar image file | ||
* | ||
* @return {object} - Returns url, file name and file type of the uploaded image | ||
*/ | ||
upload: (account, userOptions) => { | ||
checkAccount(web3, account); | ||
|
||
const options = checkOptions(userOptions, { | ||
data: { | ||
type: 'object', | ||
}, | ||
}); | ||
|
||
const { data } = options; | ||
|
||
return utils.requestAPI({ | ||
path: ['uploads', 'avatar'], | ||
method: 'POST', | ||
data, | ||
}); | ||
}, | ||
|
||
/** | ||
* Delete from storage an avatar image whose url is not connected to any user entry. | ||
* | ||
* @namespace core.avatar.delete | ||
* | ||
* @param {Object} account - web3 account instance | ||
* @param {Object} userOptions - options | ||
* @param {string} userOptions.url - url of the avatar image | ||
* | ||
* @return {boolean} - Returns true when successful | ||
*/ | ||
delete: async (account, userOptions) => { | ||
checkAccount(web3, account); | ||
|
||
const options = checkOptions(userOptions, { | ||
url: { | ||
type: 'string', | ||
}, | ||
}); | ||
|
||
const { url } = options; | ||
|
||
await utils.requestAPI({ | ||
path: ['uploads', 'avatar'], | ||
method: 'DELETE', | ||
data: { | ||
url, | ||
}, | ||
}); | ||
|
||
return true; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import createCore from './helpers/core'; | ||
import getAccount from './helpers/account'; | ||
import { deploySafeAndToken } from './helpers/transactions'; | ||
import { mockApiAvatarUpload, mockApiAvatarDelete } from './helpers/mocks'; | ||
|
||
let account; | ||
let core; | ||
|
||
beforeAll(async () => { | ||
core = createCore(); | ||
}); | ||
|
||
describe('Avatar - upload and delete', () => { | ||
beforeAll(async () => { | ||
account = getAccount(1); | ||
// The Safe must be deployed and signedup to the Hub before trying to delete the user entry | ||
await deploySafeAndToken(core, account); | ||
}); | ||
|
||
describe('when a user wants to upload an image, and then remove it', () => { | ||
it('should return a success response', async () => { | ||
const data = {}; | ||
mockApiAvatarUpload(data); | ||
const result = await core.avatar.upload(account, { data }); | ||
expect(result.data.url).toEqual(expect.stringContaining('https://')); | ||
|
||
mockApiAvatarDelete(result.data.url); | ||
expect( | ||
await core.avatar.delete(account, { | ||
url: result.data.url, | ||
}), | ||
).toBe(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import httpStatus from 'http-status'; | ||
import nock from 'nock'; | ||
|
||
export function mockApiAvatarUpload(data) { | ||
nock(process.env.API_SERVICE_ENDPOINT) | ||
.post(`/api/uploads/avatar/`, data) | ||
.reply(httpStatus.OK, { | ||
data: { | ||
url: 'https://circles-ubi-development.s3.amazonaws.com/uploads/avatars/e38d4d9365b9a48cd0f2a3e1cddb1c5cc1da5a8282cbc61fe949dbb017c4d52d.jpg', | ||
}, | ||
}); | ||
} | ||
|
||
export function mockApiAvatarDelete(url) { | ||
nock(process.env.API_SERVICE_ENDPOINT) | ||
.delete(`/api/uploads/avatar/`, { | ||
url, | ||
}) | ||
.reply(httpStatus.OK); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters