The password game is a web game where you will write a password as it says, but it has a twist, you have to follow some insanely stupid rules to complete the perfect password.
So in this kata you will have a bunch of rules and need to make a method that validates if a String satisfies them all.
Input- A String or empty String
- The given String will not have blank spaces or line breaks
- A boolean which state if the password is strong enough to pass the game
- The maximum lenght of the password is 50 characters
- The minimum lenght of the password is 20 characters
- The length of the password must be a prime number
- Must have at least 1 lower case, 3 upper case letter and 1 special character
- The addition of each of the digits in the password has to be at least 25
- Must have the diminituve of a month
- Must have today's date
- 2 special characters can't be together
- The length of the password must be inside the password
- The months of the year for this kata are [jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec]
- The months may be in upper case, lower case or combined
- The date inside the password will have the following format YYYYMMDD
- The date numbers count as normal digits
- The special characters for this kata are ['-', '.' , '_', '@', '#', '$', '&']
import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Arrays; public class PasswordValidation { private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; public static boolean validatePassword(String password) { if (password.length() < 20 | password.length() > 50) return false; if (!isPrime(password.length())) return false; if (!password.contains(String.valueOf(password.length()))) return false; if (!password.contains(TODAY_DATE)) return false; if (!Arrays.stream(VALID_MONTHS).anyMatch(s -> password.toLowerCase().contains(s))) return false; if (!password.matches(("(.*[A-Z].*){3,}")) || !password.matches("(.*[a-z].*){1,}") || !password.matches("(.*[-._@#$&].*){1,}") || password.matches(".*[-._@#$&][-._@#$&].*")) return false; if (Arrays.stream(password.split("")).filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25) return false; return true; } public static boolean isPrime(int n) { for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) return false; } return true; } }
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.Arrays;
- public class PasswordValidation {
- private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct",
- "nov", "dec" };
- public static boolean validatePassword(String password) {
- if (password.length() < 20 | password.length() > 50)
- return false;
- if (!isPrime(password.length()))
- return false;
- if (!password.contains(String.valueOf(password.length())))
- return false;
- if (!password.contains(TODAY_DATE))
- return false;
- if (!Arrays.stream(VALID_MONTHS).anyMatch(s -> password.toLowerCase().contains(s)))
- return false;
- if (!password.matches(("(.*[A-Z].*){3,}"))
- || !password.matches("(.*[a-z].*){1,}")
- || !password.matches("(.*[-._@#$&].*){1,}")
- || password.matches(".*[-._@#$&][-._@#$&].*"))
- return false;
- if (Arrays.stream(password.split("")).filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25)
- return false;
- return true;
- }
- public static boolean isPrime(int n) {
- for (int i = 2; i <= Math.sqrt(n); i++) {
- if (n % i == 0)
- return false;
- }
- return true;
- }
- }
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Random; class SolutionTest { // Today's date private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); // String of all available characters private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_@#$&0123456789"; // Diminutive of the months private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; // List of all possible lengths. Prime numbers are more than once so they appear // more often private static final int[] LENGTHS = { 20, 21, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 25, 26, 27, 28, 29, 29, 29, 29, 29, 30, 31, 31, 31, 31, 31, 32, 33, 34, 35, 36, 37, 37, 37, 37, 37, 38, 39, 40, 41, 41, 41, 41, 41, 42, 43, 43, 43, 43, 43, 44, 45, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 49, 50 }; // Prime numbers between 20 & 50 private static final int[] PRIME_LENGTHS = { 23, 29, 31, 37, 41, 43, 47 }; @Test void basicTest() { assertFalse(PasswordValidation.validatePassword("")); assertTrue(PasswordValidation.validatePassword("pass73$OCT" + TODAY_DATE + "len23")); assertFalse(PasswordValidation.validatePassword("PASS73$OCT" + TODAY_DATE + "LEN23")); assertFalse(PasswordValidation.validatePassword( TODAY_DATE + "TOdaY.feb29P6g@#fahHl")); assertFalse(PasswordValidation.validatePassword("sib$dic&omonUl98DSA" + TODAY_DATE + "len17shJDAIDB78334&Okk.53jfh")); assertTrue(PasswordValidation.validatePassword("dskjRUE3576&sj_88@mAyJun" + TODAY_DATE + "JUL47.1j6SB2nI7")); } @Test void largestBasicTest() { String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd")); assertTrue(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6gwo@fahH")); assertTrue(PasswordValidation.validatePassword("h&ks-KlOCt" + TODAY_DATE + "#aai78Ho37M7M_lj73.")); assertTrue(PasswordValidation.validatePassword(TODAY_DATE + "&jUN1.df487-ks228d43#hULP0@cb111baQ")); assertFalse(PasswordValidation.validatePassword("k17DiC9@P" + TODAY_DATE)); assertFalse(PasswordValidation.validatePassword("hJAN" + TODAY_DATE + "&17a9")); assertFalse(PasswordValidation.validatePassword("p6NOv#L17" + TODAY_DATE)); assertFalse(PasswordValidation.validatePassword("MyP@ssworD-67sAloU" + TODAY_DATE + "29o")); assertFalse(PasswordValidation.validatePassword("MyP@sswMAy-67sAlU" + TODAY_DATE + "29o")); assertFalse(PasswordValidation.validatePassword("Lol_sC_apR01" + TODAY_DATE + "nOp")); assertFalse(PasswordValidation.validatePassword(".SiL" + TODAY_DATE + "jUt7190lAUG2f27")); assertFalse(PasswordValidation.validatePassword("JupoJUN43.90-12plUSlenght31$h#1")); assertFalse(PasswordValidation.validatePassword("n0TC84nTeInmar.nn_" + randomDay + "poAl98712mQ")); } public static String generateRandomPass() { Random random = new Random(); StringBuilder pass = new StringBuilder(); int length = LENGTHS[random.nextInt(LENGTHS.length)]; int month_pos = random.nextInt(length - 3); int length_pos = random.nextInt(length - 2); int date_pos = random.nextInt(length - 8); for (int i = 0; i < length; i++) { pass.append(CHARS.charAt(random.nextInt(CHARS.length()))); if (i == month_pos) { pass.append(VALID_MONTHS[random.nextInt(VALID_MONTHS.length)]); i += 3; } if (i == length_pos) { pass.append(length); i += 2; } if (i == date_pos) { pass.append(TODAY_DATE); i += 8; } } return pass.toString(); } public static boolean checkPass(String pass) { if (!Arrays.stream(PRIME_LENGTHS).anyMatch(n -> pass.length() == n) || !pass.contains(String.valueOf(pass.length())) || !pass.contains(TODAY_DATE) || !Arrays.stream(VALID_MONTHS).anyMatch(s -> pass.toLowerCase().contains(s)) || !pass.matches("(.*[A-Z].*){3,}") || !pass.matches("(.*[a-z].*){1,}") || !pass.matches("(.*[-._@#$&].*){1,}") || pass.matches(".*[-._@#$&][-._@#$&].*") || Arrays.stream(pass.split("")) .filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25) { return false; } return true; } @RepeatedTest(200) void randomTest() { String randomPass = generateRandomPass(); assertEquals(checkPass(randomPass), PasswordValidation.validatePassword(randomPass)); } }
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertTrue;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.Arrays;
- import java.util.Random;
- class SolutionTest {
- // Today's date
- private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- // String of all available characters
- private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_@#$&0123456789";
- // Diminutive of the months
- private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct",
- "nov", "dec" };
- // List of all possible lengths. Prime numbers are more than once so they appear
- // more often
- private static final int[] LENGTHS = { 20, 21, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 25, 26, 27, 28, 29, 29, 29,
- 29, 29, 30, 31, 31, 31, 31, 31, 32, 33, 34, 35, 36, 37, 37, 37, 37, 37, 38, 39, 40, 41, 41, 41, 41, 41, 42,
- 43, 43, 43, 43, 43, 44, 45, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 49, 50 };
- // Prime numbers between 20 & 50
- private static final int[] PRIME_LENGTHS = { 23, 29, 31, 37, 41, 43, 47 };
- @Test
- void basicTest() {
- assertFalse(PasswordValidation.validatePassword(""));
- assertTrue(PasswordValidation.validatePassword("pass73$OCT" + TODAY_DATE + "len23"));
- assertFalse(PasswordValidation.validatePassword("PASS73$OCT" + TODAY_DATE + "LEN23"));
- assertFalse(PasswordValidation.validatePassword( TODAY_DATE + "TOdaY.feb29P6g@#fahHl"));
- assertFalse(PasswordValidation.validatePassword("sib$dic&omonUl98DSA" + TODAY_DATE + "len17shJDAIDB78334&Okk.53jfh"));
- assertTrue(PasswordValidation.validatePassword("dskjRUE3576&sj_88@mAyJun" + TODAY_DATE + "JUL47.1j6SB2nI7"));
- }
- @Test
- void largestBasicTest() {
- String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- assertTrue(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6gwo@fahH"));
- assertTrue(PasswordValidation.validatePassword("h&ks-KlOCt" + TODAY_DATE + "#aai78Ho37M7M_lj73."));
- assertTrue(PasswordValidation.validatePassword(TODAY_DATE + "&jUN1.df487-ks228d43#hULP0@cb111baQ"));
- assertFalse(PasswordValidation.validatePassword("k17DiC9@P" + TODAY_DATE));
- assertFalse(PasswordValidation.validatePassword("hJAN" + TODAY_DATE + "&17a9"));
- assertFalse(PasswordValidation.validatePassword("p6NOv#L17" + TODAY_DATE));
- assertFalse(PasswordValidation.validatePassword("MyP@ssworD-67sAloU" + TODAY_DATE + "29o"));
- assertFalse(PasswordValidation.validatePassword("MyP@sswMAy-67sAlU" + TODAY_DATE + "29o"));
- assertFalse(PasswordValidation.validatePassword("Lol_sC_apR01" + TODAY_DATE + "nOp"));
- assertFalse(PasswordValidation.validatePassword(".SiL" + TODAY_DATE + "jUt7190lAUG2f27"));
- assertFalse(PasswordValidation.validatePassword("JupoJUN43.90-12plUSlenght31$h#1"));
- assertFalse(PasswordValidation.validatePassword("n0TC84nTeInmar.nn_" + randomDay + "poAl98712mQ"));
- }
- public static String generateRandomPass() {
- Random random = new Random();
- StringBuilder pass = new StringBuilder();
- int length = LENGTHS[random.nextInt(LENGTHS.length)];
- int month_pos = random.nextInt(length - 3);
int date_pos = random.nextInt(length - 8);- int length_pos = random.nextInt(length - 2);
- int date_pos = random.nextInt(length - 8);
- for (int i = 0; i < length; i++) {
- pass.append(CHARS.charAt(random.nextInt(CHARS.length())));
- if (i == month_pos) {
- pass.append(VALID_MONTHS[random.nextInt(VALID_MONTHS.length)]);
- i += 3;
- }
- if (i == length_pos) {
- pass.append(length);
- i += 2;
- }
- if (i == date_pos) {
- pass.append(TODAY_DATE);
- i += 8;
- }
- }
- return pass.toString();
- }
- public static boolean checkPass(String pass) {
- if (!Arrays.stream(PRIME_LENGTHS).anyMatch(n -> pass.length() == n)
- || !pass.contains(String.valueOf(pass.length()))
- || !pass.contains(TODAY_DATE)
- || !Arrays.stream(VALID_MONTHS).anyMatch(s -> pass.toLowerCase().contains(s))
|| !pass.matches(("(.*[A-Z].*){3,}"))- || !pass.matches("(.*[A-Z].*){3,}")
- || !pass.matches("(.*[a-z].*){1,}")
- || !pass.matches("(.*[-._@#$&].*){1,}")
- || pass.matches(".*[-._@#$&][-._@#$&].*")
- || Arrays.stream(pass.split(""))
- .filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25) {
- return false;
- }
- return true;
- }
@RepeatedTest(100)- @RepeatedTest(200)
- void randomTest() {
- String randomPass = generateRandomPass();
- assertEquals(checkPass(randomPass), PasswordValidation.validatePassword(randomPass));
- }
- }
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Random; class SolutionTest { // Today's date private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); // String of all available characters private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_@#$&0123456789"; // Diminutive of the months private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; // List of all possible lengths. Prime numbers are more than once so they appear // more often private static final int[] LENGTHS = { 20, 21, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 25, 26, 27, 28, 29, 29, 29, 29, 29, 30, 31, 31, 31, 31, 31, 32, 33, 34, 35, 36, 37, 37, 37, 37, 37, 38, 39, 40, 41, 41, 41, 41, 41, 42, 43, 43, 43, 43, 43, 44, 45, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 49, 50 }; // Prime numbers between 20 & 50 private static final int[] PRIME_LENGTHS = { 23, 29, 31, 37, 41, 43, 47 }; @Test void basicTest() { assertFalse(PasswordValidation.validatePassword("")); assertTrue(PasswordValidation.validatePassword("pass73$OCT" + TODAY_DATE + "len23")); assertFalse(PasswordValidation.validatePassword("PASS73$OCT" + TODAY_DATE + "LEN23")); assertFalse(PasswordValidation.validatePassword( TODAY_DATE + "TOdaY.feb29P6g@#fahHl")); assertFalse(PasswordValidation.validatePassword("sib$dic&omonUl98DSA" + TODAY_DATE + "len17shJDAIDB78334&Okk.53jfh")); assertTrue(PasswordValidation.validatePassword("dskjRUE3576&sj_88@mAyJun" + TODAY_DATE + "JUL47.1j6SB2nI7")); } @Test void largestBasicTest() { String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd")); assertTrue(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6gwo@fahH")); assertTrue(PasswordValidation.validatePassword("h&ks-KlOCt" + TODAY_DATE + "#aai78Ho37M7M_lj73.")); assertTrue(PasswordValidation.validatePassword(TODAY_DATE + "&jUN1.df487-ks228d43#hULP0@cb111baQ")); assertFalse(PasswordValidation.validatePassword("k17DiC9@P" + TODAY_DATE)); assertFalse(PasswordValidation.validatePassword("hJAN" + TODAY_DATE + "&17a9")); assertFalse(PasswordValidation.validatePassword("p6NOv#L17" + TODAY_DATE)); assertFalse(PasswordValidation.validatePassword("MyP@ssworD-67sAloU" + TODAY_DATE + "29o")); assertFalse(PasswordValidation.validatePassword("MyP@sswMAy-67sAlU" + TODAY_DATE + "29o")); assertFalse(PasswordValidation.validatePassword("Lol_sC_apR01" + TODAY_DATE + "nOp")); assertFalse(PasswordValidation.validatePassword(".SiL" + TODAY_DATE + "jUt7190lAUG2f27")); assertFalse(PasswordValidation.validatePassword("JupoJUN43.90-12plUSlenght31$h#1")); assertFalse(PasswordValidation.validatePassword("n0TC84nTeInmar.nn_" + randomDay + "poAl98712mQ")); } public static String generateRandomPass() { Random random = new Random(); StringBuilder pass = new StringBuilder(); int length = LENGTHS[random.nextInt(LENGTHS.length)]; int month_pos = random.nextInt(length - 3); int date_pos = random.nextInt(length - 2); int length_pos = random.nextInt(length - 8); for (int i = 0; i < length; i++) { pass.append(CHARS.charAt(random.nextInt(CHARS.length()))); if (i == month_pos) { pass.append(VALID_MONTHS[random.nextInt(VALID_MONTHS.length)]); i += 3; } if (i == length_pos) { pass.append(length); i += 2; } if (i == date_pos) { pass.append(TODAY_DATE); i += 8; } } return pass.toString(); } public static boolean checkPass(String pass) { if (!Arrays.stream(PRIME_LENGTHS).anyMatch(n -> pass.length() == n) | !pass.contains(String.valueOf(pass.length())) | !pass.contains(TODAY_DATE) | !Arrays.stream(VALID_MONTHS).anyMatch(s -> pass.toLowerCase().contains(s)) | pass.matches("[^A-Z]{3,}|[^a-z]+|[^-._@#$&]+|.*[-._@#$&][-._@#$&].*") | Arrays.stream(pass.split("")) .filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25) { return false; } return true; } @RepeatedTest(100) void randomTest() { String randomPass = generateRandomPass(); assertEquals(checkPass(randomPass), PasswordValidation.validatePassword(randomPass)); } }
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertTrue;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.Arrays;
- import java.util.Random;
import org.junit.jupiter.api.RepeatedTest;import org.junit.jupiter.api.Test;- class SolutionTest {
- // Today's date
- private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- // String of all available characters
- private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_@#$&0123456789";
- // Diminutive of the months
private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };- private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct",
- "nov", "dec" };
- // List of all possible lengths. Prime numbers are more than once so they appear
- // more often
private static final int[] LENGTHS = { 20, 21, 22, 23, 23, 23, 23, 23, 24, 25, 26, 27, 28, 29, 29, 29, 29, 29, 30,31, 31, 31, 31, 31, 32, 33, 34, 35, 36, 37, 37, 37, 37, 37, 38, 39, 40, 41, 41, 41, 41, 41, 42, 43, 43, 43,43, 43, 44, 45, 46, 47, 47, 47, 47, 47, 48, 49, 50 };@Testvoid basicTest() {assertFalse(PasswordValidation.validatePassword(""));assertTrue(PasswordValidation.validatePassword("pass73$OCT" + TODAY_DATE + "len23"));assertFalse(PasswordValidation.validatePassword("PASS73$OCT" + TODAY_DATE + "LEN23"));assertFalse(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6g@#fahHl"));assertFalse(PasswordValidation.validatePassword("sib$dic&omonUl98DSA" + TODAY_DATE + "len17shJDAIDB78334&Okk.53jfh"));assertTrue(PasswordValidation.validatePassword("dskjRUE3576&sj_88@mAyJun" + TODAY_DATE + "JUL47.1j6SB2nI7"));}@Testvoid largestBasicTest() {String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd"));assertTrue(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6gwo@fahH"));assertTrue(PasswordValidation.validatePassword("h&ks-KlOCt" + TODAY_DATE + "#aai78Ho37M7M_lj73."));assertTrue(PasswordValidation.validatePassword(TODAY_DATE + "&jUN1.df487-ks228d43#hULP0@cb111baQ"));assertFalse(PasswordValidation.validatePassword("k17DiC9@P" + TODAY_DATE));assertFalse(PasswordValidation.validatePassword("hJAN" + TODAY_DATE + "&17a9"));assertFalse(PasswordValidation.validatePassword("p6NOv#L17" + TODAY_DATE));assertFalse(PasswordValidation.validatePassword("MyP@ssworD-67sAloU" + TODAY_DATE + "29o"));assertFalse(PasswordValidation.validatePassword("MyP@sswMAy-67sAlU" + TODAY_DATE + "29o"));assertFalse(PasswordValidation.validatePassword("Lol_sC_apR01" + TODAY_DATE + "nOp"));assertFalse(PasswordValidation.validatePassword(".SiL" + TODAY_DATE + "jUt7190lAUG2f27"));assertFalse(PasswordValidation.validatePassword("JupoJUN43.90-12plUSlenght31$h#1"));assertFalse(PasswordValidation.validatePassword("n0TC84nTeInmar.nn_" + randomDay + "poAl98712mQ"));}- private static final int[] LENGTHS = { 20, 21, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 25, 26, 27, 28, 29, 29, 29,
- 29, 29, 30, 31, 31, 31, 31, 31, 32, 33, 34, 35, 36, 37, 37, 37, 37, 37, 38, 39, 40, 41, 41, 41, 41, 41, 42,
- 43, 43, 43, 43, 43, 44, 45, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 49, 50 };
- // Prime numbers between 20 & 50
- private static final int[] PRIME_LENGTHS = { 23, 29, 31, 37, 41, 43, 47 };
- @Test
- void basicTest() {
- assertFalse(PasswordValidation.validatePassword(""));
- assertTrue(PasswordValidation.validatePassword("pass73$OCT" + TODAY_DATE + "len23"));
- assertFalse(PasswordValidation.validatePassword("PASS73$OCT" + TODAY_DATE + "LEN23"));
- assertFalse(PasswordValidation.validatePassword( TODAY_DATE + "TOdaY.feb29P6g@#fahHl"));
- assertFalse(PasswordValidation.validatePassword("sib$dic&omonUl98DSA" + TODAY_DATE + "len17shJDAIDB78334&Okk.53jfh"));
- assertTrue(PasswordValidation.validatePassword("dskjRUE3576&sj_88@mAyJun" + TODAY_DATE + "JUL47.1j6SB2nI7"));
- }
- @Test
- void largestBasicTest() {
- String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- assertTrue(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6gwo@fahH"));
- assertTrue(PasswordValidation.validatePassword("h&ks-KlOCt" + TODAY_DATE + "#aai78Ho37M7M_lj73."));
- assertTrue(PasswordValidation.validatePassword(TODAY_DATE + "&jUN1.df487-ks228d43#hULP0@cb111baQ"));
- assertFalse(PasswordValidation.validatePassword("k17DiC9@P" + TODAY_DATE));
- assertFalse(PasswordValidation.validatePassword("hJAN" + TODAY_DATE + "&17a9"));
- assertFalse(PasswordValidation.validatePassword("p6NOv#L17" + TODAY_DATE));
- assertFalse(PasswordValidation.validatePassword("MyP@ssworD-67sAloU" + TODAY_DATE + "29o"));
- assertFalse(PasswordValidation.validatePassword("MyP@sswMAy-67sAlU" + TODAY_DATE + "29o"));
- assertFalse(PasswordValidation.validatePassword("Lol_sC_apR01" + TODAY_DATE + "nOp"));
- assertFalse(PasswordValidation.validatePassword(".SiL" + TODAY_DATE + "jUt7190lAUG2f27"));
- assertFalse(PasswordValidation.validatePassword("JupoJUN43.90-12plUSlenght31$h#1"));
- assertFalse(PasswordValidation.validatePassword("n0TC84nTeInmar.nn_" + randomDay + "poAl98712mQ"));
- }
- public static String generateRandomPass() {
- Random random = new Random();
- StringBuilder pass = new StringBuilder();
- int length = LENGTHS[random.nextInt(LENGTHS.length)];
for (int i = 0; i < length; i++) {int position = random.nextInt(4);switch (position) {case 0 -> pass.append(CHARS.charAt(random.nextInt(CHARS.length())));case 1 -> {int monthIndex = random.nextInt(VALID_MONTHS.length);pass.append(VALID_MONTHS[monthIndex]);i += 2;}case 2 -> {pass.append(length);i++;}case 3 ->{pass.append(TODAY_DATE);i += 7;}}}return pass.toString();- int month_pos = random.nextInt(length - 3);
- int date_pos = random.nextInt(length - 2);
- int length_pos = random.nextInt(length - 8);
- for (int i = 0; i < length; i++) {
- pass.append(CHARS.charAt(random.nextInt(CHARS.length())));
- if (i == month_pos) {
- pass.append(VALID_MONTHS[random.nextInt(VALID_MONTHS.length)]);
- i += 3;
- }
- if (i == length_pos) {
- pass.append(length);
- i += 2;
- }
- if (i == date_pos) {
- pass.append(TODAY_DATE);
- i += 8;
- }
- }
- return pass.toString();
- }
- public static boolean checkPass(String pass) {
if (pass.length() < 20 | pass.length() > 50)return false;if (!isPrime(pass.length()))return false;if (!pass.contains(String.valueOf(pass.length())))return false;if (!pass.contains(TODAY_DATE))- if (!Arrays.stream(PRIME_LENGTHS).anyMatch(n -> pass.length() == n)
- | !pass.contains(String.valueOf(pass.length()))
- | !pass.contains(TODAY_DATE)
- | !Arrays.stream(VALID_MONTHS).anyMatch(s -> pass.toLowerCase().contains(s))
- | pass.matches("[^A-Z]{3,}|[^a-z]+|[^-._@#$&]+|.*[-._@#$&][-._@#$&].*")
- | Arrays.stream(pass.split(""))
- .filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25) {
- return false;
if (!Arrays.stream(VALID_MONTHS).anyMatch(s -> pass.toLowerCase().contains(s)))return false;if (pass.matches("[A-Z]{0,2}|[^a-z]+|[^-._@#$&]+|.*[-._@#$&][-._@#$&].*"))return false;if (Arrays.stream(pass.split("")).filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25)return false;return true;}public static boolean isPrime(int n) {for (int i = 2; i <= Math.sqrt(n); i++) {if (n % i == 0)return false;- }
- return true;
- }
- @RepeatedTest(100)
- void randomTest() {
- String randomPass = generateRandomPass();
- assertEquals(checkPass(randomPass), PasswordValidation.validatePassword(randomPass));
- }
- }
The password game is a web game where you will write a password as it says, but it has a twist, you have to follow some insanely stupid rules to complete the perfect password.
So in this kata you will have a bunch of those goofy ass rules and need to make a method that validates if a String satisfies them all.
Input- A string or maybe nothing...
- The String will not have blank spaces or line breaks
- A boolean which state if the password is strong enough to pass the game
- The maximum lenght of the password is 50 characters
- The minimum lenght of the password is 20 characters
- The length of the password must be a prime number
- Must has at least 1 lower case, 3 upper case letter and 1 special character
- The addition of each of the numbers in the password has to be equal or more than 25
- Must have the diminituve of a month
- Must have today's date
- 2 special characters can't be together
- The length of the password must be inside the password
- The months of the year for this kata are [jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec]
- The months may be in upper case, lower case or combined
- The date inside the password will have the following format YYYYMMDD
- The date numbers count as normal numbers for the addition
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Random; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; class SolutionTest { // Today's date private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); // String of all available characters private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_@#$&0123456789"; // Diminutive of the months private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; // List of all possible lengths. Prime numbers are more than once so they appear // more often private static final int[] LENGTHS = { 20, 21, 22, 23, 23, 23, 23, 23, 24, 25, 26, 27, 28, 29, 29, 29, 29, 29, 30, 31, 31, 31, 31, 31, 32, 33, 34, 35, 36, 37, 37, 37, 37, 37, 38, 39, 40, 41, 41, 41, 41, 41, 42, 43, 43, 43, 43, 43, 44, 45, 46, 47, 47, 47, 47, 47, 48, 49, 50 }; @Test void basicTest() { assertFalse(PasswordValidation.validatePassword("")); assertTrue(PasswordValidation.validatePassword("pass73$OCT" + TODAY_DATE + "len23")); assertFalse(PasswordValidation.validatePassword("PASS73$OCT" + TODAY_DATE + "LEN23")); assertFalse(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6g@#fahHl")); assertFalse(PasswordValidation.validatePassword("sib$dic&omonUl98DSA" + TODAY_DATE + "len17shJDAIDB78334&Okk.53jfh")); assertTrue(PasswordValidation.validatePassword("dskjRUE3576&sj_88@mAyJun" + TODAY_DATE + "JUL47.1j6SB2nI7")); } @Test void largestBasicTest() { String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd")); assertTrue(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6gwo@fahH")); assertTrue(PasswordValidation.validatePassword("h&ks-KlOCt" + TODAY_DATE + "#aai78Ho37M7M_lj73.")); assertTrue(PasswordValidation.validatePassword(TODAY_DATE + "&jUN1.df487-ks228d43#hULP0@cb111baQ")); assertFalse(PasswordValidation.validatePassword("k17DiC9@P" + TODAY_DATE)); assertFalse(PasswordValidation.validatePassword("hJAN" + TODAY_DATE + "&17a9")); assertFalse(PasswordValidation.validatePassword("p6NOv#L17" + TODAY_DATE)); assertFalse(PasswordValidation.validatePassword("MyP@ssworD-67sAloU" + TODAY_DATE + "29o")); assertFalse(PasswordValidation.validatePassword("MyP@sswMAy-67sAlU" + TODAY_DATE + "29o")); assertFalse(PasswordValidation.validatePassword("Lol_sC_apR01" + TODAY_DATE + "nOp")); assertFalse(PasswordValidation.validatePassword(".SiL" + TODAY_DATE + "jUt7190lAUG2f27")); assertFalse(PasswordValidation.validatePassword("JupoJUN43.90-12plUSlenght31$h#1")); assertFalse(PasswordValidation.validatePassword("n0TC84nTeInmar.nn_" + randomDay + "poAl98712mQ")); } public static String generateRandomPass() { Random random = new Random(); StringBuilder pass = new StringBuilder(); int length = LENGTHS[random.nextInt(LENGTHS.length)]; int month_pos = random.nextInt(length - 3); int date_pos = random.nextInt(length); int length_pos = random.nextInt(length - 2); for (int i = 0; i < length; i++) { pass.append(CHARS.charAt(random.nextInt(CHARS.length()))); if (i == month_pos) { pass.append(VALID_MONTHS[random.nextInt(VALID_MONTHS.length)]); i += 3; } if (i == length_pos) { pass.append(length); i += 2; } if (i == date_pos) { pass.append(TODAY_DATE); i += 8; } } return pass.toString(); } public static boolean checkPass(String pass) { if (pass.length() < 20 | pass.length() > 50) return false; if (!isPrime(pass.length())) return false; if (!pass.contains(String.valueOf(pass.length()))) return false; if (!pass.contains(TODAY_DATE)) return false; if (!Arrays.stream(VALID_MONTHS).anyMatch(s -> pass.toLowerCase().contains(s))) return false; if (pass.matches("[A-Z]{0,2}|[^a-z]+|[^-._@#$&]+|.*[-._@#$&][-._@#$&].*")) return false; if (Arrays.stream(pass.split("")).filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25) return false; return true; } public static boolean isPrime(int n) { for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) return false; } return true; } @RepeatedTest(100) void randomTest() { String randomPass = generateRandomPass(); assertEquals(checkPass(randomPass), PasswordValidation.validatePassword(randomPass)); } }
- import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertTrue;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import java.util.Arrays;
- import java.util.Random;
- import org.junit.jupiter.api.RepeatedTest;
- import org.junit.jupiter.api.Test;
- class SolutionTest {
private String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));private String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd"));@Testvoid basicTest() {assertFalse(PasswordValidation.validatePassword(""));assertTrue(PasswordValidation.validatePassword("pass73$OCT" + today + "len23"));assertFalse(PasswordValidation.validatePassword("TOdaY" + today + ".feb29P6g@#fahHl"));assertFalse(PasswordValidation.validatePassword("sib$dic&omonUl98DSA" + today + "len17shJDAIDB78334&Okk.53jfh"));assertTrue(PasswordValidation.validatePassword("dskjRUE3576&sj_88@mAyJun" + today + "JUL47.1j6SB2nI7"));}@Testvoid largestBasicTest() {assertTrue(PasswordValidation.validatePassword("TOdaY" + today + ".feb29P6gwo@fahH"));assertTrue(PasswordValidation.validatePassword("h&ks-KlOCt" + today + "#aai78Ho37M7M_lj73."));assertTrue(PasswordValidation.validatePassword(today + "&jUN1.df487-ks228d43#hULP0@cb111baQ"));assertFalse(PasswordValidation.validatePassword("k17DiC9@P" + today));assertFalse(PasswordValidation.validatePassword("hJAN" + today + "&17a9"));assertFalse(PasswordValidation.validatePassword("p6NOv#L17" + today));assertFalse(PasswordValidation.validatePassword("MyP@ssworD-67sAloU" + today + "29o"));assertFalse(PasswordValidation.validatePassword("MyP@sswMAy-67sAlU" + today + "29o"));assertFalse(PasswordValidation.validatePassword("Lol_sC_apR01" + today + "nOp"));assertFalse(PasswordValidation.validatePassword(".SiL" + today + "jUt7190lAUG2f27"));assertFalse(PasswordValidation.validatePassword("JupoJUN43.90-12plUSlenght31$h#1"));assertFalse(PasswordValidation.validatePassword("n0TC84nTeInmar.nn_" + randomDay + "poAl98712mQ"));}- // Today's date
- private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- // String of all available characters
- private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_@#$&0123456789";
- // Diminutive of the months
private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct","nov", "dec" };- private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
- // List of all possible lengths. Prime numbers are more than once so they appear
- // more often
- private static final int[] LENGTHS = { 20, 21, 22, 23, 23, 23, 23, 23, 24, 25, 26, 27, 28, 29, 29, 29, 29, 29, 30,
- 31, 31, 31, 31, 31, 32, 33, 34, 35, 36, 37, 37, 37, 37, 37, 38, 39, 40, 41, 41, 41, 41, 41, 42, 43, 43, 43,
- 43, 43, 44, 45, 46, 47, 47, 47, 47, 47, 48, 49, 50 };
- @Test
- void basicTest() {
- assertFalse(PasswordValidation.validatePassword(""));
- assertTrue(PasswordValidation.validatePassword("pass73$OCT" + TODAY_DATE + "len23"));
- assertFalse(PasswordValidation.validatePassword("PASS73$OCT" + TODAY_DATE + "LEN23"));
- assertFalse(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6g@#fahHl"));
- assertFalse(PasswordValidation.validatePassword("sib$dic&omonUl98DSA" + TODAY_DATE + "len17shJDAIDB78334&Okk.53jfh"));
- assertTrue(PasswordValidation.validatePassword("dskjRUE3576&sj_88@mAyJun" + TODAY_DATE + "JUL47.1j6SB2nI7"));
- }
- @Test
- void largestBasicTest() {
- String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- assertTrue(PasswordValidation.validatePassword("TOdaY" + TODAY_DATE + ".feb29P6gwo@fahH"));
- assertTrue(PasswordValidation.validatePassword("h&ks-KlOCt" + TODAY_DATE + "#aai78Ho37M7M_lj73."));
- assertTrue(PasswordValidation.validatePassword(TODAY_DATE + "&jUN1.df487-ks228d43#hULP0@cb111baQ"));
- assertFalse(PasswordValidation.validatePassword("k17DiC9@P" + TODAY_DATE));
- assertFalse(PasswordValidation.validatePassword("hJAN" + TODAY_DATE + "&17a9"));
- assertFalse(PasswordValidation.validatePassword("p6NOv#L17" + TODAY_DATE));
- assertFalse(PasswordValidation.validatePassword("MyP@ssworD-67sAloU" + TODAY_DATE + "29o"));
- assertFalse(PasswordValidation.validatePassword("MyP@sswMAy-67sAlU" + TODAY_DATE + "29o"));
- assertFalse(PasswordValidation.validatePassword("Lol_sC_apR01" + TODAY_DATE + "nOp"));
- assertFalse(PasswordValidation.validatePassword(".SiL" + TODAY_DATE + "jUt7190lAUG2f27"));
- assertFalse(PasswordValidation.validatePassword("JupoJUN43.90-12plUSlenght31$h#1"));
- assertFalse(PasswordValidation.validatePassword("n0TC84nTeInmar.nn_" + randomDay + "poAl98712mQ"));
- }
- public static String generateRandomPass() {
- Random random = new Random();
- StringBuilder pass = new StringBuilder();
- int length = LENGTHS[random.nextInt(LENGTHS.length)];
- int month_pos = random.nextInt(length - 3);
- int date_pos = random.nextInt(length);
- int length_pos = random.nextInt(length - 2);
- for (int i = 0; i < length; i++) {
- pass.append(CHARS.charAt(random.nextInt(CHARS.length())));
- if (i == month_pos) {
- pass.append(VALID_MONTHS[random.nextInt(VALID_MONTHS.length)]);
- i += 3;
- }
- if (i == length_pos) {
- pass.append(length);
- i += 2;
- }
- if (i == date_pos) {
- pass.append(TODAY_DATE);
- i += 8;
- }
- }
- return pass.toString();
- }
- public static boolean checkPass(String pass) {
- if (pass.length() < 20 | pass.length() > 50)
- return false;
- if (!isPrime(pass.length()))
- return false;
- if (!pass.contains(String.valueOf(pass.length())))
- return false;
- if (!pass.contains(TODAY_DATE))
- return false;
- if (!Arrays.stream(VALID_MONTHS).anyMatch(s -> pass.toLowerCase().contains(s)))
- return false;
if (Arrays.stream(pass.split("")).filter(s -> s.matches("[A-Z]")).count() < 3)return false;if (Arrays.stream(pass.split("")).filter(s -> s.matches("[a-z]")).count() < 1)return false;if (Arrays.stream(pass.split("")).filter(s -> s.matches("[-._@#$&]")).count() < 1)- if (pass.matches("[A-Z]{0,2}|[^a-z]+|[^-._@#$&]+|.*[-._@#$&][-._@#$&].*"))
- return false;
- if (Arrays.stream(pass.split("")).filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25)
- return false;
- return true;
- }
- public static boolean isPrime(int n) {
- for (int i = 2; i <= Math.sqrt(n); i++) {
- if (n % i == 0)
- return false;
- }
- return true;
- }
- @RepeatedTest(100)
- void randomTest() {
- String randomPass = generateRandomPass();
- assertEquals(checkPass(randomPass), PasswordValidation.validatePassword(randomPass));
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.time.LocalDate; import java.time.format.DateTimeFormatter; class SolutionTest { private String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); private String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd")); @Test void basicTest() { assertFalse(PasswordValidation.validatePassword("")); assertTrue(PasswordValidation.validatePassword("pass73$OCT" + today + "len23")); assertFalse(PasswordValidation.validatePassword("TOdaY" + today + ".feb29P6g@#fahHl")); assertFalse(PasswordValidation.validatePassword("sib$dic&omonUl98DSA" + today + "len17shJDAIDB78334&Okk.53jfh")); assertTrue(PasswordValidation.validatePassword("dskjRUE3576&sj_88@mAyJun" + today + "JUL47.1j6SB2nI7")); } @Test void largestBasicTest() { assertTrue(PasswordValidation.validatePassword("TOdaY" + today + ".feb29P6gwo@fahH")); assertTrue(PasswordValidation.validatePassword("h&ks-KlOCt" + today + "#aai78Ho37M7M_lj73.")); assertTrue(PasswordValidation.validatePassword(today + "&jUN1.df487-ks228d43#hULP0@cb111baQ")); assertFalse(PasswordValidation.validatePassword("k17DiC9@P" + today)); assertFalse(PasswordValidation.validatePassword("hJAN" + today + "&17a9")); assertFalse(PasswordValidation.validatePassword("p6NOv#L17" + today)); assertFalse(PasswordValidation.validatePassword("MyP@ssworD-67sAloU" + today + "29o")); assertFalse(PasswordValidation.validatePassword("MyP@sswMAy-67sAlU" + today + "29o")); assertFalse(PasswordValidation.validatePassword("Lol_sC_apR01" + today + "nOp")); assertFalse(PasswordValidation.validatePassword(".SiL" + today + "jUt7190lAUG2f27")); assertFalse(PasswordValidation.validatePassword("JupoJUN43.90-12plUSlenght31$h#1")); assertFalse(PasswordValidation.validatePassword("n0TC84nTeInmar.nn_" + randomDay + "poAl98712mQ")); } // Today's date private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); // String of all available characters private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_@#$&0123456789"; // Diminutive of the months private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; // List of all possible lengths. Prime numbers are more than once so they appear // more often private static final int[] LENGTHS = { 20, 21, 22, 23, 23, 23, 23, 23, 24, 25, 26, 27, 28, 29, 29, 29, 29, 29, 30, 31, 31, 31, 31, 31, 32, 33, 34, 35, 36, 37, 37, 37, 37, 37, 38, 39, 40, 41, 41, 41, 41, 41, 42, 43, 43, 43, 43, 43, 44, 45, 46, 47, 47, 47, 47, 47, 48, 49, 50 }; public static String generateRandomPass() { Random random = new Random(); StringBuilder pass = new StringBuilder(); int length = LENGTHS[random.nextInt(LENGTHS.length)]; int month_pos = random.nextInt(length - 3); int date_pos = random.nextInt(length); int length_pos = random.nextInt(length - 2); for (int i = 0; i < length; i++) { pass.append(CHARS.charAt(random.nextInt(CHARS.length()))); if (i == month_pos) { pass.append(VALID_MONTHS[random.nextInt(VALID_MONTHS.length)]); i += 3; } if (i == length_pos) { pass.append(length); i += 2; } if (i == date_pos) { pass.append(TODAY_DATE); i += 8; } } return pass.toString(); } public static boolean checkPass(String pass) { if (pass.length() < 20 | pass.length() > 50) return false; if (!isPrime(pass.length())) return false; if (!pass.contains(String.valueOf(pass.length()))) return false; if (!pass.contains(TODAY_DATE)) return false; if (!Arrays.stream(VALID_MONTHS).anyMatch(s -> pass.toLowerCase().contains(s))) return false; if (Arrays.stream(pass.split("")).filter(s -> s.matches("[A-Z]")).count() < 3) return false; if (Arrays.stream(pass.split("")).filter(s -> s.matches("[a-z]")).count() < 1) return false; if (Arrays.stream(pass.split("")).filter(s -> s.matches("[-._@#$&]")).count() < 1) return false; if (Arrays.stream(pass.split("")).filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25) return false; return true; } public static boolean isPrime(int n) { for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) return false; } return true; } @RepeatedTest(100) void randomTest() { String randomPass = generateRandomPass(); assertEquals(checkPass(randomPass), PasswordValidation.validatePassword(randomPass)); } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertTrue;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
// TODO: Replace examples and use TDD by writing your own tests- class SolutionTest {
private String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));private String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd"));- private String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- private String randomDay = LocalDate.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
@Test- @Test
- void basicTest() {
- assertFalse(PasswordValidation.validatePassword(""));
- assertTrue(PasswordValidation.validatePassword("pass73$OCT" + today + "len23"));
- assertFalse(PasswordValidation.validatePassword("TOdaY" + today + ".feb29P6g@#fahHl"));
- assertFalse(PasswordValidation.validatePassword("sib$dic&omonUl98DSA" + today + "len17shJDAIDB78334&Okk.53jfh"));
- assertTrue(PasswordValidation.validatePassword("dskjRUE3576&sj_88@mAyJun" + today + "JUL47.1j6SB2nI7"));
- }
- @Test
- void largestBasicTest() {
- assertTrue(PasswordValidation.validatePassword("TOdaY" + today + ".feb29P6gwo@fahH"));
- assertTrue(PasswordValidation.validatePassword("h&ks-KlOCt" + today + "#aai78Ho37M7M_lj73."));
- assertTrue(PasswordValidation.validatePassword(today + "&jUN1.df487-ks228d43#hULP0@cb111baQ"));
- assertFalse(PasswordValidation.validatePassword("k17DiC9@P" + today));
- assertFalse(PasswordValidation.validatePassword("hJAN" + today + "&17a9"));
- assertFalse(PasswordValidation.validatePassword("p6NOv#L17" + today));
- assertFalse(PasswordValidation.validatePassword("MyP@ssworD-67sAloU" + today + "29o"));
- assertFalse(PasswordValidation.validatePassword("MyP@sswMAy-67sAlU" + today + "29o"));
- assertFalse(PasswordValidation.validatePassword("Lol_sC_apR01" + today + "nOp"));
- assertFalse(PasswordValidation.validatePassword(".SiL" + today + "jUt7190lAUG2f27"));
- assertFalse(PasswordValidation.validatePassword("JupoJUN43.90-12plUSlenght31$h#1"));
- assertFalse(PasswordValidation.validatePassword("n0TC84nTeInmar.nn_" + randomDay + "poAl98712mQ"));
- }
- // Today's date
- private static final String TODAY_DATE = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
- // String of all available characters
- private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_@#$&0123456789";
- // Diminutive of the months
- private static final String[] VALID_MONTHS = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct",
- "nov", "dec" };
- // List of all possible lengths. Prime numbers are more than once so they appear
- // more often
- private static final int[] LENGTHS = { 20, 21, 22, 23, 23, 23, 23, 23, 24, 25, 26, 27, 28, 29, 29, 29, 29, 29, 30,
- 31, 31, 31, 31, 31, 32, 33, 34, 35, 36, 37, 37, 37, 37, 37, 38, 39, 40, 41, 41, 41, 41, 41, 42, 43, 43, 43,
- 43, 43, 44, 45, 46, 47, 47, 47, 47, 47, 48, 49, 50 };
- public static String generateRandomPass() {
- Random random = new Random();
- StringBuilder pass = new StringBuilder();
- int length = LENGTHS[random.nextInt(LENGTHS.length)];
- int month_pos = random.nextInt(length - 3);
- int date_pos = random.nextInt(length);
- int length_pos = random.nextInt(length - 2);
- for (int i = 0; i < length; i++) {
- pass.append(CHARS.charAt(random.nextInt(CHARS.length())));
- if (i == month_pos) {
- pass.append(VALID_MONTHS[random.nextInt(VALID_MONTHS.length)]);
- i += 3;
- }
- if (i == length_pos) {
- pass.append(length);
- i += 2;
- }
- if (i == date_pos) {
- pass.append(TODAY_DATE);
- i += 8;
- }
- }
- return pass.toString();
- }
- public static boolean checkPass(String pass) {
- if (pass.length() < 20 | pass.length() > 50)
- return false;
- if (!isPrime(pass.length()))
- return false;
- if (!pass.contains(String.valueOf(pass.length())))
- return false;
- if (!pass.contains(TODAY_DATE))
- return false;
- if (!Arrays.stream(VALID_MONTHS).anyMatch(s -> pass.toLowerCase().contains(s)))
- return false;
- if (Arrays.stream(pass.split("")).filter(s -> s.matches("[A-Z]")).count() < 3)
- return false;
- if (Arrays.stream(pass.split("")).filter(s -> s.matches("[a-z]")).count() < 1)
- return false;
- if (Arrays.stream(pass.split("")).filter(s -> s.matches("[-._@#$&]")).count() < 1)
- return false;
- if (Arrays.stream(pass.split("")).filter(s -> s.matches("[0-9]")).mapToInt(s -> Integer.valueOf(s)).sum() < 25)
- return false;
- return true;
- }
- public static boolean isPrime(int n) {
- for (int i = 2; i <= Math.sqrt(n); i++) {
- if (n % i == 0)
- return false;
- }
- return true;
- }
- @RepeatedTest(100)
- void randomTest() {
- String randomPass = generateRandomPass();
- assertEquals(checkPass(randomPass), PasswordValidation.validatePassword(randomPass));
- }
- }
The password game is a web game where you will write a password as it says, but it has a twist, you have to follow some insanely stupid rules to complete the perfect password.
So in this kata you will have a bunch of those goofy ass rules and need to make a method that validates if a String satisfies them all.
Input
- A string, or maybe nothing...
- A boolean which state if the password is strong enough to pass the game
- The maximum length of the password is 40 characters
- The minimum length of the password is 15 characters
- The length of the password must be a prime number
- Must at least 1 lower case and 3 upper case letter, 1 number, 1 special character
- The addition of each of the numbers in the password has to be equal or more than 25
- Must have the diminituve of a month
- Must have today's date
- 2 special characters can't be together
- The length of the password must be inside the password
- The months of the year for this kata are -> [jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec]
- The date inside the password will have the following format YYYYMMDD
public class PasswordValidation {
public static boolean validatePassword(String password) {
return false;
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
// TODO: Replace examples and use TDD by writing your own tests
class SolutionTest {
@Test
void testSomething() {
// assertEquals("expected", "actual");
}
}