Skip to content

Commit

Permalink
auth: add language and timezone check on signup
Browse files Browse the repository at this point in the history
  • Loading branch information
darakeon committed Feb 18, 2024
1 parent c02a157 commit d93eb18
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 27 deletions.
8 changes: 8 additions & 0 deletions core/BusinessLogic/Services/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using DFM.Entities.Bases;
using Keon.Util.Extensions;
using Error = DFM.BusinessLogic.Exceptions.Error;
using DFM.Generic.Datetime;
using DFM.Language;

namespace DFM.BusinessLogic.Services
{
Expand All @@ -24,6 +26,12 @@ public void SaveUser(SignUpInfo info)
{
info.VerifyPassword();
if (!PlainText.AcceptLanguage(info.Language))
throw Error.LanguageUnknown.Throw();
if (!TZ.IsValid(info.TimeZone))
throw Error.TimeZoneUnknown.Throw();
var user = info.GetEntity();
user.Control.MiscDna = Misc.RandomDNA();
Expand Down
2 changes: 1 addition & 1 deletion core/Entities/Bases/Defaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace DFM.Entities.Bases
public static class Defaults
{
public const String SettingsLanguage = "pt-BR";
public const String SettingsTimeZone = "E. South America Standard Time";
public const String SettingsTimeZone = "UTC-03:00";

public const Boolean SettingsUseCategories = false;
public const Boolean SettingsUseAccountsSigns = false;
Expand Down
64 changes: 40 additions & 24 deletions core/Tests/BusinessLogic/A.Auth/a.SaveUser.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

Scenario: Aa01. Save user with empty e-mail
Given I have this user data
| Email | Password | Retype Password |
| | password | password |
| Email | Password | Retype Password | Language | Timezone |
| | password | password | pt-BR | UTC-03:00 |
When I try to save the user
Then I will receive this core error: UserEmailRequired
And the user will not be saved

Scenario: Aa02. Save user with empty password
Given I have this user data
| Email | Password | Retype Password |
| {scenarioCode}@dontflymoney.com | | |
| Email | Password | Retype Password | Language | Timezone |
| {scenarioCode}@dontflymoney.com | | | pt-BR | UTC-03:00 |
When I try to save the user
Then I will receive this core error: UserPasswordRequired
And the user will not be saved

Scenario: Aa03. Save user with invalid e-mail
Given I have this user data
| Email | Password | Retype Password |
| {scenarioCode} | password | password |
| Email | Password | Retype Password | Language | Timezone |
| {scenarioCode} | password | password | pt-BR | UTC-03:00 |
When I try to save the user
Then I will receive this core error: UserEmailInvalid
And the user will not be saved
Expand All @@ -29,74 +29,90 @@ Scenario: Aa04. Save user with repeated e-mail
| Email | Password |
| {scenarioCode}@dontflymoney.com | password |
And I have this user data
| Email | Password | Retype Password |
| {scenarioCode}@dontflymoney.com | password | password |
| Email | Password | Retype Password | Language | Timezone |
| {scenarioCode}@dontflymoney.com | password | password | pt-BR | UTC-03:00 |
When I try to save the user
Then I will receive this core error: UserAlreadyExists
And the user will not be changed

Scenario: Aa05. Save user wrong retype
Given I have this user data
| Email | Password | Retype Password |
| {scenarioCode}@dontflymoney.com | password | password_wrong |
| Email | Password | Retype Password | Language | Timezone |
| {scenarioCode}@dontflymoney.com | password | password_wrong | pt-BR | UTC-03:00 |
When I try to save the user
Then I will receive this core error: RetypeWrong
And the user will not be saved

Scenario: Aa06. Save user with info all right
Given I have this user data
| Email | Password | Retype Password |
| {scenarioCode}@dontflymoney.com | password | password |
| Email | Password | Retype Password | Language | Timezone |
| {scenarioCode}@dontflymoney.com | password | password | pt-BR | UTC-03:00 |
When I try to save the user
Then I will receive no core error
And the user will be saved
And it will have a misc

Scenario: Aa07. Save user without e-mail
Given I have this user data
| Password | Retype Password |
| password | password |
| Password | Retype Password | Language | Timezone |
| password | password | pt-BR | UTC-03:00 |
When I try to save the user
Then I will receive this core error: UserEmailRequired
And the user will not be saved

Scenario: Aa08. Save user without password
Given I have this user data
| Email | Retype Password |
| {scenarioCode}@dontflymoney.com | password |
| Email | Retype Password | Language | Timezone |
| {scenarioCode}@dontflymoney.com | password | pt-BR | UTC-03:00 |
When I try to save the user
Then I will receive this core error: UserPasswordRequired
And the user will not be saved

Scenario: Aa09. Save user without retype password
Given I have this user data
| Email | Password |
| {scenarioCode}@dontflymoney.com | password |
| Email | Password | Language | Timezone |
| {scenarioCode}@dontflymoney.com | password | pt-BR | UTC-03:00 |
When I try to save the user
Then I will receive this core error: RetypeWrong
And the user will not be saved

Scenario: Aa10. Too large e-mail username (65)
Given I have this user data
| Password | Retype Password | Email |
| password | password | ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM@dontflymoney.com |
| Password | Retype Password | Email | Language | Timezone |
| password | password | ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM@dontflymoney.com | pt-BR | UTC-03:00 |
When I try to save the user
Then I will receive this core error: TooLargeUserEmail
And the user will not be saved

Scenario: Aa11. Too large e-mail domain (256)
Given I have this user data
| Password | Retype Password | Email |
| password | password | ABCDEFGHIJKLMNOPQRSTUVWXYZ@dontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.com |
| Password | Retype Password | Email | Language | Timezone |
| password | password | ABCDEFGHIJKLMNOPQRSTUVWXYZ@dontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.com | pt-BR | UTC-03:00 |
When I try to save the user
Then I will receive this core error: TooLargeUserEmail
And the user will not be saved

Scenario: Aa12. Exactly length username (64) and domain (255)
Given I have this user data
| Password | Retype Password | Email |
| password | password | ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKL@dontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.co |
| Password | Retype Password | Email | Language | Timezone |
| password | password | ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKL@dontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.comdontflymoney.co | pt-BR | UTC-03:00 |
When I try to save the user
Then I will receive no core error
And the user will be saved
And it will have a misc

Scenario: Aa13. Save user with invalid language
Given I have this user data
| Email | Password | Retype Password | Language | Timezone |
| {scenarioCode}@dontflymoney.com | P | P | at-ST | UTC-03:00 |
When I try to save the user
Then I will receive this core error: LanguageUnknown
And the user will not be saved

Scenario: Aa14. Save user with invalid timezone
Given I have this user data
| Email | Password | Retype Password | Language | Timezone |
| {scenarioCode}@dontflymoney.com | P | P | pt-BR | GMT-03 |
When I try to save the user
Then I will receive this core error: TimeZoneUnknown
And the user will not be saved
6 changes: 5 additions & 1 deletion core/Tests/BusinessLogic/Steps/BaseStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected void createUserIfNotExists(
Email = email,
Password = password,
RetypePassword = password,
Language = Defaults.SettingsLanguage,
Language = language ?? Defaults.SettingsLanguage
};

if (timezone != null)
Expand All @@ -158,6 +158,10 @@ protected void createUserIfNotExists(

info.TimeZone = $"UTC{userHour:+00;-00; 00}:00";
}
else
{
info.TimeZone = Defaults.SettingsTimeZone;
}

if (language != null)
{
Expand Down
21 changes: 20 additions & 1 deletion core/Tests/BusinessLogic/Steps/_A.SafeStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ private String currentPassword
set => set("CurrentPassword", value);
}

private String language
{
get => get<String>("Language");
set => set("Language", value);
}

private String timezone
{
get => get<String>("Timezone");
set => set("Timezone", value);
}

private SecurityAction action
{
get => get<SecurityAction>("Action");
Expand Down Expand Up @@ -112,6 +124,12 @@ public void GivenIHaveThisUserData(Table table)

if (table.Header.Any(c => c == "Retype Password"))
retypePassword = table.Rows[0]["Retype Password"];

if (table.Header.Any(c => c == "Language"))
language = table.Rows[0]["Language"];

if (table.Header.Any(c => c == "Timezone"))
timezone = table.Rows[0]["Timezone"];
}

[When(@"I try to save the user")]
Expand All @@ -124,7 +142,8 @@ public void WhenITryToSaveTheUser()
Email = email,
Password = password,
RetypePassword = retypePassword,
Language = Defaults.SettingsLanguage,
Language = language,
TimeZone = timezone,
};

service.Auth.SaveUser(info);
Expand Down

0 comments on commit d93eb18

Please sign in to comment.