Skip to content

Commit

Permalink
Merge pull request #196 from aion-dk/QZ-419-blank-validations
Browse files Browse the repository at this point in the history
Make blank validation based on new config in marking type
  • Loading branch information
av-alexander authored Jul 18, 2022
2 parents 6b9e5b2 + 775847a commit 065e4ed
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 14 deletions.
1 change: 1 addition & 0 deletions lib/av_client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export type ContestConfig = {
export type MarkingType = {
minMarks: number
maxMarks: number
blankSubmission: "disabled" | "active_choice" | "implicit"
encoding: {
codeSize: 1 | 2
maxSize: number
Expand Down
13 changes: 10 additions & 3 deletions lib/av_client/validate_selections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ export function validateContestSelection( contestConfig: ContestConfig, contestS

const { markingType, options } = contestConfig

const isBlank = contestSelection.optionSelections.length === 0

// Validate blankSubmission
if( isBlank && markingType.blankSubmission == 'disabled'){
throw new CorruptSelectionError('Blank submissions are not allowed in this contest')
}

// Validate that mark count is within bounds
if( !withinBounds(markingType.minMarks, contestSelection.optionSelections.length, markingType.maxMarks) ){
if( !isBlank && !withinBounds(markingType.minMarks, contestSelection.optionSelections.length, markingType.maxMarks) ){
throw new CorruptSelectionError('Contest selection does not contain a valid amount of option selections')
}
}

// Validate duplicates - that any vote selection is not referencing the same option multiple times
const selectedOptions = contestSelection.optionSelections.map(os => os.reference)
if( hasDuplicates(selectedOptions) ){
throw new CorruptSelectionError('Same option slected multiple times')
throw new CorruptSelectionError('Same option selected multiple times')
}

const getOption = makeGetOption(options)
Expand Down
1 change: 1 addition & 0 deletions test/construct_contest_envelopes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const contestOne: ContestConfig = {
markingType: {
minMarks: 1,
maxMarks: 1,
blankSubmission: "disabled",
encoding: {
codeSize: 1,
maxSize: 1,
Expand Down
1 change: 1 addition & 0 deletions test/decrypt_contest_selections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const bigContest: ContestConfig = {
markingType: {
minMarks: 1,
maxMarks: 1,
blankSubmission: "disabled",
encoding: {
codeSize: 1,
maxSize: 41,
Expand Down
1 change: 1 addition & 0 deletions test/encoding/byte_encoding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const contestConfig: ContestConfig = {
markingType: {
minMarks: 1,
maxMarks: 3,
blankSubmission: "disabled",
encoding: {
codeSize: 1,
maxSize: 20,
Expand Down
2 changes: 2 additions & 0 deletions test/encrypt_contest_selections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const contestOne: ContestConfig = {
markingType: {
minMarks: 1,
maxMarks: 1,
blankSubmission: "disabled",
encoding: {
codeSize: 1,
maxSize: 1,
Expand Down Expand Up @@ -67,6 +68,7 @@ describe('encryptContestSelections', () => {
markingType: {
minMarks: 1,
maxMarks: 1,
blankSubmission: "disabled",
encoding: {
codeSize: 1,
maxSize: 41,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"markingType": {
"minMarks": 1,
"maxMarks": 1,
"blankSubmission": "disabled",
"encoding": {
"codeSize": 1,
"maxSize": 1,
Expand Down Expand Up @@ -73,6 +74,7 @@
"markingType": {
"minMarks": 1,
"maxMarks": 1,
"blankSubmission": "disabled",
"encoding": {
"codeSize": 1,
"maxSize": 1,
Expand Down
2 changes: 2 additions & 0 deletions test/replies/otp_flow/get_us_configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"markingType": {
"minMarks": 1,
"maxMarks": 1,
"blankSubmission": "disabled",
"encoding": {
"codeSize": 1,
"maxSize": 1,
Expand Down Expand Up @@ -73,6 +74,7 @@
"markingType": {
"minMarks": 1,
"maxMarks": 1,
"blankSubmission": "disabled",
"encoding": {
"codeSize": 1,
"maxSize": 1,
Expand Down
44 changes: 33 additions & 11 deletions test/validate_selections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const contestOne: ContestConfig = {
markingType: {
minMarks: 1,
maxMarks: 1,
blankSubmission: "disabled",
encoding: {
codeSize: 1,
maxSize: 1,
Expand All @@ -27,6 +28,13 @@ const contestOne: ContestConfig = {
title: { en: 'Option 1' },
subtitle: { en: 'Option 1' },
description: { en: 'Option 1' },
},
{
reference: 'option-3',
code: 3,
title: { en: 'Option 3' },
subtitle: { en: 'Option 3' },
description: { en: 'Option 3' },
}
]
}
Expand All @@ -36,6 +44,7 @@ const contestTwo: ContestConfig = {
markingType: {
minMarks: 1,
maxMarks: 2,
blankSubmission: "active_choice",
encoding: {
codeSize: 1,
maxSize: 2,
Expand Down Expand Up @@ -81,33 +90,46 @@ describe('validateContestSelection', () => {
{ reference: 'option-1' }
]
}
it('does not throw error', () => {
it('throws an error', () => {
expect(() => {
validateContestSelection( contestOne, contestSelection )
}).to.throw(CorruptSelectionError, 'Contest selection is not matching contest config')
})
})

context('when given a contest selection with no selections', () => {
context('when given a contest selection with no selections and blank disabled', () => {
const contestSelection = {
reference: 'contest-2',
reference: 'contest-1',
optionSelections: []
}
it('does not throw error', () => {
it('throws an error', () => {
expect(() => {
validateContestSelection( contestTwo, contestSelection )
}).to.throw(CorruptSelectionError, 'Contest selection does not contain a valid amount of option selections')
validateContestSelection( contestOne, contestSelection )
}).to.throw(CorruptSelectionError, 'Blank submissions are not allowed in this contest')
})
})

context('when given a contest selection with two selections', () => {
context('when given a contest selection with no selections and blank enabled', () => {
const contestSelection = {
reference: 'contest-2',
optionSelections: []
}
it('does not throw error', () => {
it('does not throw an error', () => {
expect(() => {
validateContestSelection( contestTwo, contestSelection )
}).to.not.throw()
})
})

context('when given a contest selection with two selections', () => {
const contestSelection = {
reference: 'contest-1',
optionSelections: [{ reference: 'option-1' },
{ reference: 'option-3' }]
}
it('throws an error', () => {
expect(() => {
validateContestSelection( contestOne, contestSelection )
}).to.throw(CorruptSelectionError, 'Contest selection does not contain a valid amount of option selections')
})
})
Expand All @@ -119,7 +141,7 @@ describe('validateContestSelection', () => {
{ reference: 'option-2' }
]
}
it('does not throw error', () => {
it('throws an error', () => {
expect(() => {
validateContestSelection( contestOne, contestSelection )
}).to.throw(CorruptSelectionError, 'Option config not found')
Expand All @@ -136,10 +158,10 @@ describe('validateContestSelection', () => {
{ reference: 'option-a' }
]
}
it('does not throw error', () => {
it('throws an error', () => {
expect(() => {
validateContestSelection( contestTwo, contestSelection )
}).to.throw(CorruptSelectionError, 'Same option slected multiple times')
}).to.throw(CorruptSelectionError, 'Same option selected multiple times')
})
})
})
Expand Down
2 changes: 2 additions & 0 deletions test/verifier/readable_contest_selections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const contestConfigs: ContestConfigMap = {
"markingType": {
"minMarks": 1,
"maxMarks": 1,
"blankSubmission": "disabled",
"encoding": {
"codeSize": 1,
"maxSize": 1,
Expand Down Expand Up @@ -50,6 +51,7 @@ const contestConfigs: ContestConfigMap = {
"markingType": {
"minMarks": 1,
"maxMarks": 1,
"blankSubmission": "disabled",
"encoding": {
"codeSize": 1,
"maxSize": 1,
Expand Down

0 comments on commit 065e4ed

Please sign in to comment.