-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
IbanValidatorTests.cs
205 lines (177 loc) · 5.88 KB
/
IbanValidatorTests.cs
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
using System;
using System.Collections.Generic;
using FluentAssertions;
using IbanNet.Registry;
using NUnit.Framework;
namespace IbanNet
{
[TestFixture]
internal class IbanValidatorTests
{
private IbanValidator _validator;
private ICountryValidationSupport _countryValidationSupport;
[SetUp]
public void SetUp()
{
_validator = new IbanValidator();
_countryValidationSupport = _validator;
}
[Test]
public void When_validating_null_value_should_not_validate()
{
// Act
ValidationResult actual = _validator.Validate(null);
// Assert
actual.Should().BeEquivalentTo(new ValidationResult
{
Result = IbanValidationResult.InvalidLength
});
}
[TestCase("NL91ABNA041716430!")]
[TestCase("NL91ABNA^417164300")]
public void When_validating_iban_with_illegal_characters_should_not_validate(string ibanWithIllegalChars)
{
// Act
ValidationResult actual = _validator.Validate(ibanWithIllegalChars);
// Assert
actual.Should().BeEquivalentTo(new ValidationResult
{
Value = ibanWithIllegalChars,
Result = IbanValidationResult.IllegalCharacters,
Country = _countryValidationSupport.SupportedCountries[ibanWithIllegalChars.Substring(0, 2)]
});
}
[TestCase("0091ABNA0417164300")]
[TestCase("4591ABNA0417164300")]
[TestCase("#L91ABNA0417164300")]
public void When_validating_iban_with_illegal_country_code_should_not_validate(string ibanWithIllegalCountryCode)
{
// Act
ValidationResult actual = _validator.Validate(ibanWithIllegalCountryCode);
// Assert
actual.Should().BeEquivalentTo(new ValidationResult
{
Value = ibanWithIllegalCountryCode,
Result = IbanValidationResult.IllegalCharacters
});
}
[TestCase("NL00ABNA0417164300")]
[TestCase("NL01ABNA0417164300")]
[TestCase("NL99ABNA0417164300")]
public void When_validating_iban_with_invalid_checksum_should_not_validate(string ibanWithInvalidChecksum)
{
// Act
ValidationResult actual = _validator.Validate(ibanWithInvalidChecksum);
// Assert
actual.Should().BeEquivalentTo(new ValidationResult
{
Value = ibanWithInvalidChecksum,
Result = IbanValidationResult.IllegalCharacters,
Country = _countryValidationSupport.SupportedCountries[ibanWithInvalidChecksum.Substring(0, 2)]
});
}
[TestCase("NL91ABNA04171643000")]
[TestCase("NL91ABNA041716430")]
[TestCase("NO938601111794")]
[TestCase("NO93860111179470")]
public void When_validating_iban_with_incorrect_length_should_not_validate(string ibanWithIncorrectLength)
{
// Act
ValidationResult actual = _validator.Validate(ibanWithIncorrectLength);
// Assert
actual.Should().BeEquivalentTo(new ValidationResult
{
Value = ibanWithIncorrectLength,
Result = IbanValidationResult.InvalidLength,
Country = _countryValidationSupport.SupportedCountries[ibanWithIncorrectLength.Substring(0, 2)]
});
}
[TestCase("AA91ABNA0417164300")]
[TestCase("ZZ93860111179470")]
public void When_validating_iban_with_unknown_country_code_should_not_validate(string ibanWithUnknownCountryCode)
{
// Act
ValidationResult actual = _validator.Validate(ibanWithUnknownCountryCode);
// Assert
actual.Should().BeEquivalentTo(new ValidationResult
{
Value = ibanWithUnknownCountryCode,
Result = IbanValidationResult.UnknownCountryCode
});
}
[TestCase("NL91ABNA041716430A")]
[TestCase("NO938601111794A")]
public void When_validating_iban_with_invalid_structure_should_not_validate(string ibanWithInvalidStructure)
{
// Act
ValidationResult actual = _validator.Validate(ibanWithInvalidStructure);
// Assert
actual.Should().BeEquivalentTo(new ValidationResult
{
Value = ibanWithInvalidStructure,
Result = IbanValidationResult.InvalidStructure,
Country = _countryValidationSupport.SupportedCountries[ibanWithInvalidStructure.Substring(0, 2)]
});
}
[TestCase("NL92ABNA0417164300")]
[TestCase("NO9486011117947")]
public void When_validating_tampered_iban_should_not_validate(string tamperedIban)
{
// Act
ValidationResult actual = _validator.Validate(tamperedIban);
// Assert
actual.Should().BeEquivalentTo(new ValidationResult
{
Value = tamperedIban,
Result = IbanValidationResult.InvalidCheckDigits,
Country = _countryValidationSupport.SupportedCountries[tamperedIban.Substring(0, 2)]
});
}
[TestCase("NL91 ABNA 0417 1643 00")]
[TestCase("NL91\tABNA\t0417\t1643\t00")]
[TestCase(" NL91 ABNA041 716 4300 ")]
public void When_iban_contains_whitespace_should_validate(string ibanWithWhitespace)
{
// Act
ValidationResult actual = _validator.Validate(ibanWithWhitespace);
// Assert
actual.Should().BeEquivalentTo(new ValidationResult
{
Value = Iban.Normalize(ibanWithWhitespace),
Result = IbanValidationResult.Valid,
Country = _countryValidationSupport.SupportedCountries["NL"]
});
}
[TestCaseSource(typeof(IbanTestCaseData), nameof(IbanTestCaseData.GetValidIbanPerCountry))]
public void When_validating_good_iban_should_validate(string countryCode, string iban)
{
var expectedResult = new ValidationResult
{
Value = iban,
Result = IbanValidationResult.Valid,
Country = _countryValidationSupport.SupportedCountries[iban.Substring(0, 2)]
};
// Act
ValidationResult actual = _validator.Validate(iban);
// Assert
actual.Should().BeEquivalentTo(expectedResult);
}
[Test]
public void When_getting_supported_countries_should_match_default_registry()
{
_validator.SupportedCountries
.Should()
.BeEquivalentTo(new IbanRegistry());
}
[Test]
public void When_casting_readonly_countries_dictionary_should_not_be_able_to_add()
{
var countries = (IDictionary<string, CountryInfo>)((ICountryValidationSupport)_validator).SupportedCountries;
// Act
Action act = () => countries.Add("key", new CountryInfo());
// Assert
act.Should().Throw<NotSupportedException>()
.WithMessage("Collection is read-only.");
}
}
}