Skip to content

Commit

Permalink
Adding correct regex for FI and NO phones
Browse files Browse the repository at this point in the history
Incl Tests
  • Loading branch information
mnording committed Feb 2, 2017
1 parent dddda7a commit 7dab08a
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Offline/OfflineOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ public enum Status { NotSent,Sent,Pending,Polling,Complete}
private void verifyPhoneForCountry()
{
Regex r = new Regex("");
if (_config.Country == "SE" || _config.Country == "FI"|| _config.Country == "NO")
if (_config.Country == "SE")
{
r = new Regex(@"^([+]46)\s*(7[0236])\s*(\d{4})\s*(\d{3})$", RegexOptions.IgnoreCase);
}
else
else if(_config.Country == "FI")
{
r = new Regex(@"^((90[0-9]{3})?0|\+358\s?)(?!(100|20(0|2(0|[2-3])|9[8-9])|300|600|700|708|75(00[0-3]|(1|2)\d{2}|30[0-2]|32[0-2]|75[0-2]|98[0-2])))(4|50|10[1-9]|20(1|2(1|[4-9])|[3-9])|29|30[1-9]|71|73|75(00[3-9]|30[3-9]|32[3-9]|53[3-9]|83[3-9])|2|3|5|6|8|9|1[3-9])\s?(\d\s?){4,19}\d$", RegexOptions.IgnoreCase);
}
else if (_config.Country == "NO")
{
r = new Regex(@"^(0047|\+47|47)?[2-9]\d{7}$", RegexOptions.IgnoreCase);
}
else{
r = new Regex("xxx");
}

Expand Down
125 changes: 125 additions & 0 deletions OfflineTest/OfflineOrderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.Globalization;
using Klarna.Offline;
using Klarna.Offline.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Klarna.Entities;
using MerchantConfig = Klarna.Offline.Entities.MerchantConfig;

namespace OfflineTest
{
[TestClass]
public class OfflineOrderTest
{
[TestMethod]
public void MustValidateSwedishPhone()
{
OfflineOrder t = new OfflineOrder(new Cart(),
new Klarna.Offline.Entities.MerchantConfig(CultureInfo.CreateSpecificCulture("sv-se"), "SEK", "SE",
"testid", "testid", MerchantConfig.Server.Test),
"test",
"+46700024576",
"ref");
}

[TestMethod]
public void MustValidateNorwegianPhone()
{
OfflineOrder t = new OfflineOrder(new Cart(),
new Klarna.Offline.Entities.MerchantConfig(CultureInfo.CreateSpecificCulture("nb-no"), "NOK", "NO",
"testid", "testid", MerchantConfig.Server.Test),
"test",
"+4790000000",
"ref");
t = new OfflineOrder(new Cart(),
new Klarna.Offline.Entities.MerchantConfig(CultureInfo.CreateSpecificCulture("nb-no"), "NOK", "NO",
"testid", "testid", MerchantConfig.Server.Test),
"test",
"+4740000000",
"ref");
t = new OfflineOrder(new Cart(),
new Klarna.Offline.Entities.MerchantConfig(CultureInfo.CreateSpecificCulture("nb-no"), "NOK", "NO",
"testid", "testid", MerchantConfig.Server.Test),
"test",
"+4759000000",
"ref");

}

[TestMethod]
public void MustValidateFinnishPhone()
{
OfflineOrder t = new OfflineOrder(new Cart(),
new Klarna.Offline.Entities.MerchantConfig(CultureInfo.CreateSpecificCulture("fi-fi"), "EUR", "FI",
"testid", "testid", MerchantConfig.Server.Test),
"test",
"+358401234567",
"ref");
t = new OfflineOrder(new Cart(),
new Klarna.Offline.Entities.MerchantConfig(CultureInfo.CreateSpecificCulture("fi-fi"), "EUR", "FI",
"testid", "testid", MerchantConfig.Server.Test),
"test",
"+358501234",
"ref");
t = new OfflineOrder(new Cart(),
new Klarna.Offline.Entities.MerchantConfig(CultureInfo.CreateSpecificCulture("fi-fi"), "EUR", "FI",
"testid", "testid", MerchantConfig.Server.Test),
"test",
"+35845733654578",
"ref");
t = new OfflineOrder(new Cart(),
new Klarna.Offline.Entities.MerchantConfig(CultureInfo.CreateSpecificCulture("fi-fi"), "EUR", "FI",
"testid", "testid", MerchantConfig.Server.Test),
"test",
"+35850457894",
"ref");
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void MustThrowErrorOnToLongFIPhone()
{
OfflineOrder t = new OfflineOrder(new Cart(),
new Klarna.Offline.Entities.MerchantConfig(CultureInfo.CreateSpecificCulture("fi-fi"), "EUR", "FI",
"testid", "testid", MerchantConfig.Server.Test),
"test",
"+3584012345673332231312312",
"ref");
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void MustThrowErrorOnToShortFIPhone()
{
OfflineOrder t = new OfflineOrder(new Cart(),
new Klarna.Offline.Entities.MerchantConfig(CultureInfo.CreateSpecificCulture("fi-fi"), "EUR", "FI",
"testid", "testid", MerchantConfig.Server.Test),
"test",
"+35840123",
"ref");
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void MustThrowErrorOnWrongSEhone()
{
OfflineOrder t = new OfflineOrder(new Cart(),
new Klarna.Offline.Entities.MerchantConfig(CultureInfo.CreateSpecificCulture("sv-se"), "SEK", "SE",
"testid", "testid", MerchantConfig.Server.Test),
"test",
"+4670002457622",
"ref");
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void MustThrowErrorOnWrongNOPhone()
{
OfflineOrder t = new OfflineOrder(new Cart(),
new Klarna.Offline.Entities.MerchantConfig(CultureInfo.CreateSpecificCulture("nb-no"), "NOK", "NO",
"testid", "testid", MerchantConfig.Server.Test),
"test",
"+47900000000",
"ref");
}
}
}
5 changes: 5 additions & 0 deletions OfflineTest/OfflineTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Klarna, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Offline\External\Klarna.dll</HintPath>
</Reference>
<Reference Include="System" />
</ItemGroup>
<Choose>
Expand All @@ -55,6 +59,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="MerchantConfigTest.cs" />
<Compile Include="JsonConverterTest.cs" />
<Compile Include="OfflineOrderTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Offline\Offline.csproj">
Expand Down

0 comments on commit 7dab08a

Please sign in to comment.