Implement showDonations in the database

- Add to backend tests.
This commit is contained in:
Wolfgang Huß 2021-04-15 10:49:56 +02:00
parent a48355f8bc
commit d40dd94868
10 changed files with 100 additions and 94 deletions

View File

@ -197,6 +197,7 @@ Factory.define('comment')
Factory.define('donations')
.attr('id', uuid)
.attr('showDonations', true)
.attr('goal', 15000)
.attr('progress', 0)
.after((buildObject, options) => {

View File

@ -2,6 +2,7 @@ import { v4 as uuid } from 'uuid'
export default {
id: { type: 'string', primary: true, default: uuid },
showDonations: { type: 'boolean' },
goal: { type: 'number' },
progress: { type: 'number' },
createdAt: { type: 'string', isoDate: true, default: () => new Date().toISOString() },

View File

@ -9,9 +9,10 @@ const instance = getNeode()
const driver = getDriver()
const updateDonationsMutation = gql`
mutation($goal: Int, $progress: Int) {
UpdateDonations(goal: $goal, progress: $progress) {
mutation($showDonations: Boolean, $goal: Int, $progress: Int) {
UpdateDonations(showDonations: $showDonations, goal: $goal, progress: $progress) {
id
showDonations
goal
progress
createdAt
@ -23,6 +24,7 @@ const donationsQuery = gql`
query {
Donations {
id
showDonations
goal
progress
}
@ -64,20 +66,20 @@ describe('donations', () => {
errors: [{ message: 'Not Authorised!' }],
})
})
})
describe('authenticated', () => {
beforeEach(async () => {
currentUser = await Factory.build('user', {
id: 'normal-user',
role: 'user',
})
authenticatedUser = await currentUser.toJson()
describe('authenticated', () => {
beforeEach(async () => {
currentUser = await Factory.build('user', {
id: 'normal-user',
role: 'user',
})
authenticatedUser = await currentUser.toJson()
})
it('returns the current Donations info', async () => {
await expect(query({ query: donationsQuery, variables })).resolves.toMatchObject({
data: { Donations: [{ goal: 15000, progress: 0 }] },
})
it('returns the current Donations info', async () => {
await expect(query({ query: donationsQuery, variables })).resolves.toMatchObject({
data: { Donations: [{ showDonations: true, goal: 15000, progress: 0 }] },
})
})
})
@ -85,7 +87,7 @@ describe('donations', () => {
describe('update donations', () => {
beforeEach(() => {
variables = { goal: 20000, progress: 3000 }
variables = { showDonations: false, goal: 20000, progress: 3000 }
})
describe('unauthenticated', () => {
@ -97,75 +99,75 @@ describe('donations', () => {
errors: [{ message: 'Not Authorised!' }],
})
})
})
describe('authenticated', () => {
describe('as a normal user', () => {
beforeEach(async () => {
currentUser = await Factory.build('user', {
id: 'normal-user',
role: 'user',
})
authenticatedUser = await currentUser.toJson()
describe('authenticated', () => {
describe('as a normal user', () => {
beforeEach(async () => {
currentUser = await Factory.build('user', {
id: 'normal-user',
role: 'user',
})
authenticatedUser = await currentUser.toJson()
})
it('throws authorization error', async () => {
await expect(
mutate({ mutation: updateDonationsMutation, variables }),
).resolves.toMatchObject({
data: { UpdateDonations: null },
errors: [{ message: 'Not Authorised!' }],
})
it('throws authorization error', async () => {
await expect(
mutate({ mutation: updateDonationsMutation, variables }),
).resolves.toMatchObject({
data: { UpdateDonations: null },
errors: [{ message: 'Not Authorised!' }],
})
})
})
describe('as a moderator', () => {
beforeEach(async () => {
currentUser = await Factory.build('user', {
id: 'moderator',
role: 'moderator',
})
authenticatedUser = await currentUser.toJson()
})
it('throws authorization error', async () => {
await expect(
mutate({ mutation: updateDonationsMutation, variables }),
).resolves.toMatchObject({
data: { UpdateDonations: null },
errors: [{ message: 'Not Authorised!' }],
})
})
})
describe('as an admin', () => {
beforeEach(async () => {
currentUser = await Factory.build('user', {
id: 'admin',
role: 'admin',
})
authenticatedUser = await currentUser.toJson()
})
it('updates Donations info', async () => {
await expect(
mutate({ mutation: updateDonationsMutation, variables }),
).resolves.toMatchObject({
data: { UpdateDonations: { showDonations: false, goal: 20000, progress: 3000 } },
errors: undefined,
})
})
describe('as a moderator', () => {
beforeEach(async () => {
currentUser = await Factory.build('user', {
id: 'moderator',
role: 'moderator',
})
authenticatedUser = await currentUser.toJson()
})
it('throws authorization error', async () => {
await expect(
mutate({ mutation: updateDonationsMutation, variables }),
).resolves.toMatchObject({
data: { UpdateDonations: null },
errors: [{ message: 'Not Authorised!' }],
})
})
})
describe('as an admin', () => {
beforeEach(async () => {
currentUser = await Factory.build('user', {
id: 'admin',
role: 'admin',
})
authenticatedUser = await currentUser.toJson()
})
it('updates Donations info', async () => {
await expect(
mutate({ mutation: updateDonationsMutation, variables }),
).resolves.toMatchObject({
data: { UpdateDonations: { goal: 20000, progress: 3000 } },
errors: undefined,
})
})
it('updates the updatedAt attribute', async () => {
newlyCreatedDonations = await newlyCreatedDonations.toJson()
const {
data: { UpdateDonations },
} = await mutate({ mutation: updateDonationsMutation, variables })
expect(newlyCreatedDonations.updatedAt).toBeTruthy()
expect(Date.parse(newlyCreatedDonations.updatedAt)).toEqual(expect.any(Number))
expect(UpdateDonations.updatedAt).toBeTruthy()
expect(Date.parse(UpdateDonations.updatedAt)).toEqual(expect.any(Number))
expect(newlyCreatedDonations.updatedAt).not.toEqual(UpdateDonations.updatedAt)
})
it('updates the updatedAt attribute', async () => {
newlyCreatedDonations = await newlyCreatedDonations.toJson()
const {
data: { UpdateDonations },
} = await mutate({ mutation: updateDonationsMutation, variables })
expect(newlyCreatedDonations.updatedAt).toBeTruthy()
expect(Date.parse(newlyCreatedDonations.updatedAt)).toEqual(expect.any(Number))
expect(UpdateDonations.updatedAt).toBeTruthy()
expect(Date.parse(UpdateDonations.updatedAt)).toEqual(expect.any(Number))
expect(newlyCreatedDonations.updatedAt).not.toEqual(UpdateDonations.updatedAt)
})
})
})

View File

@ -1,7 +1,8 @@
type Donations {
id: ID!
goal: Int!
progress: Int!
showDonations: Boolean! # Wolle make it required in the schema
goal: Int! # Wolle make it required in the schema
progress: Int! # Wolle make it required in the schema
createdAt: String
updatedAt: String
}
@ -11,5 +12,5 @@ type Query {
}
type Mutation {
UpdateDonations(goal: Int, progress: Int): Donations
UpdateDonations(showDonations: Boolean, goal: Int, progress: Int): Donations
}

View File

@ -48,7 +48,7 @@ export default {
},
update({ Donations }) {
if (!Donations[0]) return
const { goal, progress } = Donations[0]
const { goal, progress } = Donations[0] // Wolle showDonations
this.goal = goal
this.progress = progress
},

View File

@ -1,2 +1,2 @@
export const DONATIONS_SHOW_MENU_BUTTON = true
export const DONATIONS_SHOW_MENU_BUTTON = true // Wolle
export const DONATIONS_SHOW_INFO = true

View File

@ -1 +1 @@
export const NEWSFEED_SHOW_INFO_LEFT_LANE = true
export const NEWSFEED_SHOW_INFO_LEFT_LANE = true // Wolle

View File

@ -4,6 +4,7 @@ export const DonationsQuery = () => gql`
query {
Donations {
id
showDonations
goal
progress
}
@ -12,9 +13,10 @@ export const DonationsQuery = () => gql`
export const UpdateDonations = () => {
return gql`
mutation($goal: Int, $progress: Int) {
UpdateDonations(goal: $goal, progress: $progress) {
mutation($showDonations: Boolean, $goal: Int, $progress: Int) {
UpdateDonations(showDonations: $showDonations, goal: $goal, progress: $progress) {
id
showDonations
goal
progress
updatedAt

View File

@ -29,12 +29,8 @@
icon="money"
:disabled="!showDonations"
/>
<base-button
class="donations-info-button"
filled
type="submit"
:disabled="!formData.goal || !formData.progress"
>
<base-button class="donations-info-button" filled type="submit">
<!-- Wolle :disabled="formData.showDonations === null || !formData.goal || !formData.progress" -->
{{ $t('actions.save') }}
</base-button>
</ds-form>
@ -59,11 +55,13 @@ export default {
},
methods: {
submit() {
const { showDonations } = this
const { goal, progress } = this.formData
this.$apollo
.mutate({
mutation: UpdateDonations(),
variables: {
showDonations,
goal: parseInt(goal),
progress: parseInt(progress),
},
@ -81,7 +79,8 @@ export default {
},
update({ Donations }) {
if (!Donations[0]) return
const { goal, progress } = Donations[0]
const { showDonations, goal, progress } = Donations[0]
this.showDonations = showDonations
this.formData = {
goal,
progress,

View File

@ -90,8 +90,8 @@
</template>
<script>
import { DONATIONS_SHOW_INFO } from '~/constants/donations'
import { NEWSFEED_SHOW_INFO_LEFT_LANE } from '~/constants/newsfeed'
import { DONATIONS_SHOW_INFO } from '~/constants/donations' // Wolle
import { NEWSFEED_SHOW_INFO_LEFT_LANE } from '~/constants/newsfeed' // Wolle
import postListActions from '~/mixins/postListActions'
import DonationInfo from '~/components/DonationInfo/DonationInfo.vue'
import HashtagsFilter from '~/components/HashtagsFilter/HashtagsFilter.vue'