This repository was archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathentity_test.go
More file actions
292 lines (280 loc) · 10.5 KB
/
entity_test.go
File metadata and controls
292 lines (280 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package api
import (
"io/ioutil"
"strings"
"testing"
)
var badRawSection = []byte("{ ==)")
func TestUnmarshallEntities(t *testing.T) {
tests := []struct {
entity Marshaller
}{
{&AdditionalComments{}},
{&Benefit{}},
{&CivilUnion{}},
{&ClearanceLevel{}},
{&Collection{}},
{&Contacts{}},
{&CoOwners{}},
{&DateRange{}},
{&IdentificationName{}},
{&IdentificationBirthDate{}},
{&IdentificationContacts{}},
{&IdentificationOtherNames{}},
{&IdentificationBirthPlace{}},
{&IdentificationSSN{}},
{&IdentificationPhysical{}},
{&FinancialBankruptcy{}},
{&FinancialGambling{}},
{&FinancialTaxes{}},
{&FinancialCard{}},
{&FinancialCredit{}},
{&FinancialDelinquent{}},
{&FinancialNonpayment{}},
{&HistoryResidence{}},
{&HistoryResidence{}},
{&HistoryEmployment{}},
{&HistoryEducation{}},
{&HistoryFederal{}},
{&Sentence{}},
{&Signature{}},
{&RelationshipsMarital{}},
{&RelationshipsCohabitants{}},
{&RelationshipsPeople{}},
{&RelationshipsRelatives{}},
{&CitizenshipStatus{}},
{&CitizenshipMultiple{}},
{&CitizenshipPassports{}},
{&MilitarySelective{}},
{&MilitaryHistory{}},
{&MilitaryDisciplinary{}},
{&MilitaryForeign{}},
{&ForeignBornDocument{}},
{&ForeignPassport{}},
{&ForeignContacts{}},
{&ForeignTravel{}},
{&ForeignActivitiesBenefits{}},
{&ForeignActivitiesDirect{}},
{&ForeignActivitiesIndirect{}},
{&ForeignActivitiesRealEstate{}},
{&ForeignActivitiesSupport{}},
{&ForeignBusinessAdvice{}},
{&ForeignBusinessConferences{}},
{&ForeignBusinessContact{}},
{&ForeignBusinessEmployment{}},
{&ForeignBusinessFamily{}},
{&ForeignBusinessPolitical{}},
{&ForeignBusinessSponsorship{}},
{&ForeignBusinessVentures{}},
{&ForeignBusinessVoting{}},
{&SubmissionAdditionalComments{}},
{&SubmissionGeneral{}},
{&Submission{}},
{&SubmissionAttachments{}},
{&SubmissionCredit{}},
{&SubmissionMedical{}},
{&Supervisor{}},
{&SubstanceDrugClearance{}},
{&SubstanceDrugMisuse{}},
{&SubstanceDrugOrdered{}},
{&SubstanceDrugPublicSafety{}},
{&SubstanceDrugPurchase{}},
{&SubstanceDrugUsage{}},
{&SubstanceDrugVoluntary{}},
{&SubstanceAlcoholNegative{}},
{&SubstanceAlcoholOrdered{}},
{&SubstanceAlcoholVoluntary{}},
{&SubstanceAlcoholAdditional{}},
{&LegalAssociationsActivitiesToOverthrow{}},
{&LegalAssociationsAdvocating{}},
{&LegalAssociationsEngagedInTerrorism{}},
{&LegalAssociationsMembershipOverthrow{}},
{&LegalAssociationsMembershipViolence{}},
{&LegalAssociationsTerrorismAssociation{}},
{&LegalAssociationsTerroristOrganization{}},
{&LegalCourt{}},
{&LegalInvestigationsDebarred{}},
{&LegalInvestigationsHistory{}},
{&LegalInvestigationsRevoked{}},
{&LegalPoliceAdditionalOffenses{}},
{&LegalPoliceDomesticViolence{}},
{&LegalPoliceOffenses{}},
{&LegalTechnologyManipulating{}},
{&LegalTechnologyUnauthorized{}},
{&LegalTechnologyUnlawful{}},
{&PhysicalAddress{}},
{&PsychologicalCompetence{}},
{&PsychologicalConsultations{}},
{&PsychologicalDiagnoses{}},
{&PsychologicalExisting{}},
{&PsychologicalHospitalizations{}},
{&ReasonLeft{}},
{&Treatment{}},
}
for _, secTest := range tests {
unmarshalErr := secTest.entity.Unmarshal(badRawSection)
if unmarshalErr == nil {
t.Logf("Should have received an error from Unmarshal, instead got nil for type %v", secTest.entity.Marshal().Type)
t.Fail()
}
}
}
func TestUnrecognizedTypePayload(t *testing.T) {
// This test constructs JSON with altered type values so each field fails individually
tests := []struct {
path string
}{
{"testdata/clearancelevel.json"},
{"testdata/civilunion.json"},
{"testdata/foreignborndocument.json"},
{"testdata/location.json"},
{"testdata/physicaladdress.json"},
{"testdata/reasonleft.json"},
{"testdata/sentence.json"},
{"testdata/signature.json"},
{"testdata/ssn.json"},
{"testdata/supervisor.json"},
{"testdata/identification/identification-name.json"},
{"testdata/identification/identification-contacts.json"},
{"testdata/identification/identification-othernames.json"},
{"testdata/identification/identification-othernames-unfinished.json"},
{"testdata/identification/identification-birthdate.json"},
{"testdata/identification/identification-birthplace.json"},
{"testdata/identification/identification-birthplace-full.json"},
{"testdata/identification/identification-ssn.json"},
{"testdata/identification/identification-physical.json"},
{"testdata/financial/financial-bankruptcy.json"},
{"testdata/financial/financial-gambling.json"},
{"testdata/financial/financial-taxes.json"},
{"testdata/financial/financial-card.json"},
{"testdata/financial/financial-credit.json"},
{"testdata/financial/financial-delinquent.json"},
{"testdata/financial/financial-nonpayment.json"},
{"testdata/history/history-residence.json"},
{"testdata/history/history-residence-unfinished-list.json"},
{"testdata/history/history-employment.json"},
{"testdata/history/history-employment-full.json"},
{"testdata/history/history-education.json"},
{"testdata/history/history-federal.json"},
{"testdata/relationships/relationships-status-marital.json"},
{"testdata/relationships/relationships-status-cohabitant.json"},
{"testdata/relationships/relationships-people.json"},
{"testdata/relationships/relationships-relatives.json"},
{"testdata/citizenship/citizenship-status.json"},
{"testdata/citizenship/citizenship-multiple.json"},
{"testdata/citizenship/citizenship-passports.json"},
{"testdata/citizenship/citizenship-passports-thin.json"},
{"testdata/military/military-selective.json"},
{"testdata/military/military-history.json"},
{"testdata/military/military-disciplinary.json"},
{"testdata/military/military-foreign.json"},
{"testdata/foreign/foreign-passport.json"},
{"testdata/foreign/foreign-contacts.json"},
{"testdata/foreign/foreign-travel.json"},
{"testdata/foreign/foreign-activities-benefits.json"},
{"testdata/foreign/foreign-activities-direct.json"},
{"testdata/foreign/foreign-activities-indirect.json"},
{"testdata/foreign/foreign-activities-realestate.json"},
{"testdata/foreign/foreign-business-advice.json"},
{"testdata/foreign/foreign-business-conferences.json"},
{"testdata/foreign/foreign-business-contact.json"},
{"testdata/foreign/foreign-business-employment.json"},
{"testdata/foreign/foreign-business-family.json"},
{"testdata/foreign/foreign-business-political.json"},
{"testdata/foreign/foreign-business-sponsorship.json"},
{"testdata/foreign/foreign-business-ventures.json"},
{"testdata/foreign/foreign-business-voting.json"},
{"testdata/substance/substance-drug-clearance.json"},
{"testdata/substance/substance-drug-misuse.json"},
{"testdata/substance/substance-drug-ordered.json"},
{"testdata/substance/substance-drug-publicsafety.json"},
{"testdata/substance/substance-drug-purchase.json"},
{"testdata/substance/substance-drug-usage.json"},
{"testdata/substance/substance-drug-voluntary.json"},
{"testdata/substance/substance-alcohol-negative.json"},
{"testdata/substance/substance-alcohol-ordered.json"},
{"testdata/substance/substance-alcohol-voluntary.json"},
{"testdata/substance/substance-alcohol-additional.json"},
{"testdata/legal/legal-associations-activities-to-overthrow.json"},
{"testdata/legal/legal-associations-advocating.json"},
{"testdata/legal/legal-associations-engaged-in-terrorism.json"},
{"testdata/legal/legal-associations-membership-overthrow.json"},
{"testdata/legal/legal-associations-membership-violence-or-force.json"},
{"testdata/legal/legal-associations-terrorism-association.json"},
{"testdata/legal/legal-associations-terrorist-organization.json"},
{"testdata/legal/legal-court.json"},
{"testdata/legal/legal-investigations-debarred.json"},
{"testdata/legal/legal-investigations-history.json"},
{"testdata/legal/legal-investigations-revoked.json"},
{"testdata/legal/legal-police-additionaloffenses.json"},
{"testdata/legal/legal-police-domesticviolence.json"},
{"testdata/legal/legal-police-offenses.json"},
{"testdata/legal/legal-technology-manipulating.json"},
{"testdata/legal/legal-technology-unauthorized.json"},
{"testdata/legal/legal-technology-unlawful.json"},
{"testdata/psychological/psychological-competence.json"},
{"testdata/psychological/psychological-consultations.json"},
{"testdata/psychological/psychological-diagnoses.json"},
{"testdata/psychological/psychological-conditions.json"},
{"testdata/psychological/psychological-hospitalizations.json"},
{"testdata/package/comments.json"},
{"testdata/submission.json"},
}
for _, secTest := range tests {
section, err := ioutil.ReadFile(secTest.path)
if err != nil {
t.Logf("Received error: %v", err)
t.Fail()
}
// make a version of the content that will remain unaltered
unchangedLines := strings.Split(string(section), "\n")
// make a copy you can manipulate
alteredLines := make([]string, len(unchangedLines))
// Determine which lines should be changed
linesOfInterest := selectLinesString(unchangedLines)
if len(linesOfInterest) < 1 {
t.Logf("Found no mentions of 'type' in JSON")
t.Fail()
}
for targetIndex := range linesOfInterest {
copy(alteredLines, unchangedLines)
indexOfInterest := linesOfInterest[targetIndex]
alteredLines[indexOfInterest] = strings.Replace(alteredLines[indexOfInterest], alteredLines[indexOfInterest], `"type": "unsinn",`, 1)
replacedSection := strings.Join(alteredLines, "\n")
// Deserialize the initial payload from the replaced JSON
payload := &Payload{}
if err := payload.Unmarshal([]byte(replacedSection)); err != nil {
t.Fatalf("Failed to deserialize JSON: %v\n:Error: %v\n", replacedSection, err)
}
// Extract the entity interface of the payload, kicking off entity.Unmarshal()
_, err := payload.Entity()
if err == nil {
t.Logf("Should have received an error from payload.Entity, instead got nil for type %v", payload.Type)
t.Fail()
}
}
}
}
func selectLinesString(lines []string) []int {
var linesOfInterest []int
leftBracket := 0
rightBracket := 0
for i, line := range lines {
// THIS IS A NIGHTMARE but the JSON's unstructured
// so you'll avoid running anything in a collection type
if strings.Contains(line, "[") {
leftBracket++
}
if strings.Contains(line, "]") {
rightBracket++
}
if leftBracket == rightBracket {
// Don't change lines that use type as part of the prop structure
// (i.e. telephone type domestic)
if strings.Contains(line, `"type"`) && strings.Contains(lines[i+1], `"props"`) {
linesOfInterest = append(linesOfInterest, i)
}
}
}
return linesOfInterest
}