//LAYER_Class_NameOfTheMethod_TestedScenario_ExpectedBehaviour public void DAL_IUser_InsertUser_InsertUser_UserIsInserted() { //arrange int userId = 1; string userName = "******"; string hashedPassword = "******"; string firstName = "Adrian"; string lastName = "Frunza"; string email = "*****@*****.**"; bool newsletterOptOut = false; DateTimeOffset createdOn = DateTimeOffset.Now; User user_m1 = new User( userId, userName, hashedPassword, firstName, lastName, email, newsletterOptOut, createdOn ); IUsers _DbUsers = new DbUsers(); //act int result = _DbUsers.insertUser(user_m1); //assert Assert.IsTrue(result >= (int)ENUM.CODE.TRANSLATO_DATABASE_SEED); }
//returns [int >= TRANSLATO_DATABASE_SEED] if successful //returns [int < TRANSLATO_DATABASE_SEED] if not internal int insertUser(User user) { int returnCode = (int)CODE.ZERO; int result = (int)CODE.MINUS_ONE; //validate userName if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || string.IsNullOrWhiteSpace(user.userName) || !Validate.isAlphaNumericWithUnderscore(user.userName) || !Validate.hasMinLength(user.userName, 5) || !Validate.hasMaxLength(user.userName, 15) ) { returnCode = (int)CODE.CTRUSER_INSERTUSER_INVALID_USERNAME; result = (int)CODE.ZERO; } //validate password(stored in the hashedPassword field at this point. Will be replaced with hash + salt later) if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || string.IsNullOrWhiteSpace(user.hashedPassword) || !Validate.hasMinLength(user.hashedPassword, 8) || !Validate.hasMaxLength(user.hashedPassword, 100) ) { returnCode = (int)CODE.CTRUSER_INSERTUSER_INVALID_PASSWORD; result = (int)CODE.ZERO; } //validate firstName if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || string.IsNullOrWhiteSpace(user.firstName) || !Validate.hasMinLength(user.firstName, 2) || !Validate.hasMaxLength(user.firstName, 20) ) { returnCode = (int)CODE.CTRUSER_INSERTUSER_INVALID_FIRSTNAME; result = (int)CODE.ZERO; } //validate lastName if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || string.IsNullOrWhiteSpace(user.lastName) || !Validate.hasMinLength(user.lastName, 2) || !Validate.hasMaxLength(user.lastName, 20) ) { returnCode = (int)CODE.CTRUSER_INSERTUSER_INVALID_LASTNAME; result = (int)CODE.ZERO; } //validate email if ( result == (int)CODE.ZERO || returnCode != (int)CODE.ZERO || string.IsNullOrWhiteSpace(user.email) || !Validate.hasMinLength(user.email, 5) || !Validate.hasMaxLength(user.email, 50) || !user.email.Contains("@") ) { returnCode = (int)CODE.CTRUSER_INSERTUSER_INVALID_EMAIL; result = (int)CODE.ZERO; } if (returnCode == (int)CODE.ZERO && result != (int)CODE.ZERO)//safe to proceed { user.userName = user.userName; user.hashedPassword = Security.hashPassword(user.hashedPassword); user.firstName = user.firstName; user.lastName = user.lastName; user.email = user.email; user.newsletterOptOut = user.newsletterOptOut; user.createdOn = DateTimeOffset.Now; IUsers _DbUsers = new DbUsers(); try { using (var trScope = TransactionScopeBuilder.CreateSerializable()) { returnCode = _DbUsers.insertUser(user); trScope.Complete(); } } catch (TransactionAbortedException taEx) { returnCode = (int)CODE.CTRUSER_INSERTUSER_EXCEPTION; Log.Add(taEx.ToString()); } catch (ApplicationException aEx) { returnCode = (int)CODE.CTRUSER_INSERTUSER_EXCEPTION; Log.Add(aEx.ToString()); } catch (Exception ex) { returnCode = (int)CODE.CTRUSER_INSERTUSER_EXCEPTION; Log.Add(ex.ToString()); } } else { } return(returnCode); }