public void TestXmlNoModClass() { Mockery mocks = new Mockery(); IInputContext context = mocks.NewMock<IInputContext>(); IDnaDataReader mockedReader = mocks.NewMock<IDnaDataReader>(); Stub.On(context).Method("CreateDnaDataReader").Will(Return.Value(mockedReader)); Stub.On(mockedReader).Method("Execute"); Stub.On(mockedReader).Method("Dispose"); IAction mockReaderResults = new MockedReaderResults(new object[] { true, false }); Stub.On(mockedReader).Method("Read").Will(mockReaderResults); // Check Stored Procedure is called as expected. Expect.Never.On(mockedReader).Method("AddParameter").With("modclassid"); Stub.On(mockedReader).Method("GetInt32NullAsZero").With("reasonid").Will(Return.Value(6)); Stub.On(mockedReader).Method("GetStringNullAsEmpty").With("displayname").Will(Return.Value("DisplayName")); Stub.On(mockedReader).Method("GetStringNullAsEmpty").With("emailname").Will(Return.Value("EmailName")); Stub.On(mockedReader).Method("GetTinyIntAsInt").With("editorsonly").Will(Return.Value(0)); ModerationReasons modReasons = new ModerationReasons(context); modReasons.GenerateXml(0); //Check XML.is as expected. XmlNode node = modReasons.RootElement.SelectSingleNode(@"MOD-REASONS"); Assert.IsNotNull(node, "ModerationReasons Element Found"); Assert.IsNull(modReasons.RootElement.SelectSingleNode(@"MOD-REASONS/@MODCLASSID"), "Moderation Class Id"); Assert.IsNotNull(modReasons.RootElement.SelectSingleNode(@"MOD-REASONS/MOD-REASON[@DISPLAYNAME='DisplayName']"), "Displayname"); Assert.IsNotNull(modReasons.RootElement.SelectSingleNode(@"MOD-REASONS/MOD-REASON[@EMAILNAME='EmailName']"), "Emailname"); Assert.IsNotNull(modReasons.RootElement.SelectSingleNode(@"MOD-REASONS/MOD-REASON[@EDITORSONLY='0']"), "Editors Only"); mocks.VerifyAllExpectationsHaveBeenMet(); }
public void VerifyXmlWithSchema() { Mockery mocks = new Mockery(); IInputContext context = mocks.NewMock<IInputContext>(); IDnaDataReader mockedReader = mocks.NewMock<IDnaDataReader>(); Stub.On(context).Method("CreateDnaDataReader").Will(Return.Value(mockedReader)); Stub.On(mockedReader).Method("Execute"); Stub.On(mockedReader).Method("Dispose"); IAction mockReaderResults = new MockedReaderResults(new object[] { true, false }); Stub.On(mockedReader).Method("Read").Will(mockReaderResults); Stub.On(mockedReader).Method("GetInt32NullAsZero").With("reasonid").Will(Return.Value(6)); Stub.On(mockedReader).Method("GetStringNullAsEmpty").With("displayname").Will(Return.Value("DisplayName")); Stub.On(mockedReader).Method("GetStringNullAsEmpty").With("emailname").Will(Return.Value("EmailName")); Stub.On(mockedReader).Method("GetTinyIntAsInt").With("editorsonly").Will(Return.Value(0)); ModerationReasons modReasons = new ModerationReasons(context); modReasons.ProcessRequest(); DnaXmlValidator validator = new DnaXmlValidator(modReasons.RootElement.InnerXml, "Mod-Reasons.xsd"); validator.Validate(); }
public void TestNonBannedUserWithInvalidCookieGetsCreatedOk() { Console.WriteLine("Before TestNonBannedUserWithInvalidCookieGetsCreatedOk"); Mockery mockery = new Mockery(); IInputContext mockedInput = mockery.NewMock<IInputContext>(); DnaCookie testCookie = new DnaCookie(); testCookie.Value = "THIS-IS-A-TEST-COOKIE-THAT-IS-NOT-VALID"; testCookie.Name = "SSO2-UID"; Stub.On(mockedInput).Method("GetCookie").With("SSO2-UID").Will(Return.Value(testCookie)); Stub.On(mockedInput).Method("GetCookie").With("H2G2DEBUG").Will(Return.Value(null)); Stub.On(mockedInput).Method("GetParamIntOrZero").With("s_sync", "User's details must be synchronised with the data in SSO.").Will(Return.Value(0)); CreateMockedProfileConnection(mockery, mockedInput, 1063883681, "Fink", "TEST-TEST-TEST", "*****@*****.**", true); SetDefaultDiagnostics(mockery, mockedInput); CreateMockedSite(mockery, mockedInput, 1, "h2g2", "h2g2", true, "comment"); // Add the mocked datareader for getting the user info from the database. IDnaDataReader mockedNormalUserReader = mockery.NewMock<IDnaDataReader>(); Stub.On(mockedNormalUserReader).Method("AddParameter").Will(Return.Value(mockedNormalUserReader)); Stub.On(mockedNormalUserReader).Method("Execute").Will(Return.Value(mockedNormalUserReader)); Stub.On(mockedNormalUserReader).Method("Dispose").Will(Return.Value(null)); Stub.On(mockedNormalUserReader).Method("Read").Will(Return.Value(false)); // Create an action list for the HasRows property IAction mockReaderResults = new MockedReaderResults(new bool[] { false, true, true }); Stub.On(mockedNormalUserReader).GetProperty("HasRows").Will(mockReaderResults); // These both need to return the same results, so use the same mocked reader for both requests Stub.On(mockedInput).Method("CreateDnaDataReader").With("fetchusersgroups").Will(Return.Value(mockedNormalUserReader)); Stub.On(mockedInput).Method("CreateDnaDataReader").With("finduserfromid").Will(Return.Value(mockedNormalUserReader)); Stub.On(mockedInput).Method("CreateDnaDataReader").With("GetDnaUserIDFromSSOUserID").Will(Return.Value(mockedNormalUserReader)); Stub.On(mockedInput).Method("CreateDnaDataReader").With("GetDnaUserIDFromIdentityUserID").Will(Return.Value(mockedNormalUserReader)); // Mock the siteoption call for the checkusernameset option Stub.On(mockedInput).Method("GetSiteOptionValueBool").With("General", "CheckUserNameSet").Will(Return.Value(false)); // Create the mocked data reader for the createnewuserfromuserid call CreateMockedCreateUserDataReader(mockery, mockedInput, 106663681); // Now mock the database reader for the two requests. The first needs to return true for email in banned list. CreateMockedReaderForEmailInBannedListCheck(mockery, mockedInput, 1, false); // Mock the siteoption call for the checkusernameset option Stub.On(mockedInput).Method("GetSiteOptionValueBool").With("General", "CheckUserNameSet").Will(Return.Value(false)); // Ok, create the user object and try to create the user. User testUser = new User(mockedInput); testUser.CreateUser(); // This will fail if the create user call got to check the data base more than once! mockery.VerifyAllExpectationsHaveBeenMet(); Console.WriteLine("After TestNonBannedUserWithInvalidCookieGetsCreatedOk"); }
public void TestServiceWithNoEmailAttributeCreatesUsers() { Console.WriteLine("Before TestBannedUserCaughtEvenWithinvalidCookie"); // Create the input context to run the requests in Mockery mockery = new Mockery(); IInputContext mockedInput = mockery.NewMock<IInputContext>(); // Create the invalid cookie. Invalid cookies are less or equal to 64 chars long DnaCookie cookie = new DnaCookie(); cookie.Name = "SSO2-UID"; cookie.Value = "VALID-COOKIE-ABCDEFGHIJKLMNOPQRRSTUVWXYZ-SSO-NORMAL-USER-ACCOUNT"; // Stub the cookie to the context Stub.On(mockedInput).Method("GetCookie").With("SSO2-UID").Will(Return.Value(cookie)); Stub.On(mockedInput).Method("GetCookie").With("H2G2DEBUG").Will(Return.Value(null)); Stub.On(mockedInput).Method("GetParamIntOrZero").With("s_sync", "User's details must be synchronised with the data in SSO.").Will(Return.Value(0)); // Now add the mocked profile connection CreateMockedProfileConnection(mockery, mockedInput, 106663681, "Fink", "TEST-TEST-TEST-NOEMAIL-ATTRIBUTE", "", false); SetDefaultDiagnostics(mockery, mockedInput); CreateMockedSite(mockery, mockedInput, 1, "h2g2", "h2g2", true, "comment"); // Add the mocked datareader for getting the user info from the database. IDnaDataReader mockedNormalUserReader = mockery.NewMock<IDnaDataReader>(); Stub.On(mockedNormalUserReader).Method("AddParameter").Will(Return.Value(mockedNormalUserReader)); Stub.On(mockedNormalUserReader).Method("Execute").Will(Return.Value(mockedNormalUserReader)); Stub.On(mockedNormalUserReader).Method("Dispose").Will(Return.Value(null)); Stub.On(mockedNormalUserReader).Method("Read").Will(Return.Value(false)); // Create an action list for the HasRows property IAction mockReaderResults = new MockedReaderResults(new bool[] { false, true, true }); Stub.On(mockedNormalUserReader).GetProperty("HasRows").Will(mockReaderResults); // These both need to return the same results, so use the same mocked reader for both requests Stub.On(mockedInput).Method("CreateDnaDataReader").With("fetchusersgroups").Will(Return.Value(mockedNormalUserReader)); Stub.On(mockedInput).Method("CreateDnaDataReader").With("finduserfromid").Will(Return.Value(mockedNormalUserReader)); //Stub.On(mockedInput).Method("CreateDnaDataReader").With("GetDnaUserIDFromSSOUserID").Will(Return.Value(mockedNormalUserReader)); Stub.On(mockedInput).Method("CreateDnaDataReader").With("GetDnaUserIDFromIdentityUserID").Will(Return.Value(mockedNormalUserReader)); // Create the mocked data reader for the createnewuserfromuserid call CreateMockedCreateUserDataReader(mockery, mockedInput, 106663681); // Mock the siteoption call for the checkusernameset option Stub.On(mockedInput).Method("GetSiteOptionValueBool").With("General", "CheckUserNameSet").Will(Return.Value(false)); // Ok, create the user object and try to create the user. User testUser = new User(mockedInput); testUser.CreateUser(); // This will fail if the create user call got to check the data base more than twice! mockery.VerifyAllExpectationsHaveBeenMet(); Console.WriteLine("Before TestBannedUserCaughtEvenWithinvalidCookie"); }
//See above - work with Mark H to understand what the test is for. public void TestNormalUserCanBeCreatedAfterABannedUserIsCaught() { Console.WriteLine("Before TestNormalUserCanBeCreatedAfterABannedUserIsCaught"); // Start by creating two contexts. One for each request Mockery mockery = new Mockery(); IInputContext mockedInput = mockery.NewMock<IInputContext>(); IInputContext mockedInput2 = mockery.NewMock<IInputContext>(); DnaCookie testBannedCookie = new DnaCookie(); testBannedCookie.Value = TestUserAccounts.GetBannedUserAccount.Cookie; //"6042004|DotNetUserBanned|DotNetUserBanned|1273497847257|0|9d9ee980c4b831e419915b452b050f327862bba748ff"; testBannedCookie.Name = "IDENTITY"; DnaCookie testBannedCookie2 = new DnaCookie(); testBannedCookie2.Value = TestUserAccounts.GetBannedUserAccount.SecureCookie; //"a684c1a5736f052c4acc1b35908f8dbad2e2ea0b"; testBannedCookie2.Name = "IDENTITY-HTTPS"; DnaCookie testNormalCookie = new DnaCookie(); testNormalCookie.Value = TestUserAccounts.GetBannedUserAccount.Cookie; //"6042002|DotNetNormalUser|DotNetNormalUser|1273497514775|0|bf78fdd57a1f70faee630c07ba31674eab181a3f6c6f"; testNormalCookie.Name = "IDENTITY"; DnaCookie testNormalCookie2 = new DnaCookie(); testNormalCookie2.Value = TestUserAccounts.GetBannedUserAccount.SecureCookie; // "1eda650cb28e56156217427336049d0b8e164765"; testNormalCookie2.Name = "IDENTITY-HTTPS"; // Now set the two test cookies. One for each user DnaCookie cookie1 = new DnaCookie(); cookie1.Name = "SSO2-UID"; cookie1.Value = "VALID-COOKIE-ABCDEFGHIJKLMNOPQRRSTUVWXYZ-SSO-BANNED-USER-ACCOUNT"; DnaCookie cookie2 = new DnaCookie(); cookie2.Name = "SSO2-UID"; cookie2.Value = "VALID-COOKIE-ABCDEFGHIJKLMNOPQRRSTUVWXYZ-SSO-NORMAL-USER-ACCOUNT"; // Stub the cookies to the contexts Stub.On(mockedInput).Method("GetCookie").With("IDENTITY").Will(Return.Value(testBannedCookie)); Stub.On(mockedInput).Method("GetCookie").With("IDENTITY-HTTPS").Will(Return.Value(testBannedCookie2)); Stub.On(mockedInput2).Method("GetCookie").With("IDENTITY").Will(Return.Value(testNormalCookie)); Stub.On(mockedInput2).Method("GetCookie").With("IDENTITY-HTTPS").Will(Return.Value(testNormalCookie2)); Stub.On(mockedInput).Method("GetCookie").With("SSO2-UID").Will(Return.Value(cookie1)); Stub.On(mockedInput2).Method("GetCookie").With("SSO2-UID").Will(Return.Value(cookie2)); Stub.On(mockedInput).Method("GetCookie").With("H2G2DEBUG").Will(Return.Value(null)); Stub.On(mockedInput2).Method("GetCookie").With("H2G2DEBUG").Will(Return.Value(null)); Stub.On(mockedInput).Method("GetParamIntOrZero").With("s_sync", "User's details must be synchronised with the data in SSO.").Will(Return.Value(0)); Stub.On(mockedInput2).Method("GetParamIntOrZero").With("s_sync", "User's details must be synchronised with the data in SSO.").Will(Return.Value(0)); // Now add the mocked profile connections CreateMockedProfileConnection(mockery, mockedInput, 106663681, "BadFink", "TEST-TEST-TEST-BANNED-USER", "*****@*****.**", true); IDnaIdentityWebServiceProxy mockedGoodProfile = CreateMockedProfileConnection(mockery, mockedInput2, 1063883681, "GoodFink", "TEST-TEST-TEST-NORMAL-USER", "*****@*****.**", true); // Create the mocked data reader for the createnewuserfromuserid call CreateMockedCreateUserDataReader(mockery, mockedInput2, 106663681); // Set the diagnostics for the contexts SetDefaultDiagnostics(mockery, mockedInput); SetDefaultDiagnostics(mockery, mockedInput2); // Add the site for the contexts CreateMockedSite(mockery, mockedInput, 1, "h2g2", "h2g2", true, "comment"); CreateMockedSite(mockery, mockedInput2, 1, "h2g2", "h2g2", true, "comment"); // Add the mocked datareader for getting the user info from the database. CreateMockedReaderForUserNotInDataBase(mockery, mockedInput); //CreateMockedReaderForUserNotInDataBase(mockery, mockedInput2); IDnaDataReader mockedNormalUserReader = mockery.NewMock<IDnaDataReader>(); Stub.On(mockedNormalUserReader).Method("AddParameter").Will(Return.Value(mockedNormalUserReader)); Stub.On(mockedNormalUserReader).Method("Execute").Will(Return.Value(mockedNormalUserReader)); Stub.On(mockedNormalUserReader).Method("Dispose").Will(Return.Value(null)); Stub.On(mockedNormalUserReader).Method("Read").Will(Return.Value(false)); // Create an action list for the Read and HasRows method/property IAction mockReaderResults = new MockedReaderResults(new bool[] {false,true,true}); Stub.On(mockedNormalUserReader).GetProperty("HasRows").Will(mockReaderResults); // These both need to return the same results, so use the same mocked reader for both requests Stub.On(mockedInput2).Method("CreateDnaDataReader").With("fetchusersgroups").Will(Return.Value(mockedNormalUserReader)); Stub.On(mockedInput2).Method("CreateDnaDataReader").With("finduserfromid").Will(Return.Value(mockedNormalUserReader)); //Stub.On(mockedInput2).Method("CreateDnaDataReader").With("GetDnaUserIDFromSSOUserID").Will(Return.Value(mockedNormalUserReader)); Stub.On(mockedInput2).Method("CreateDnaDataReader").With("GetDnaUserIDFromIdentityUserID").Will(Return.Value(mockedNormalUserReader)); // Now mock the database reader for the two requests. The first needs to return true for email in banned list. CreateMockedReaderForEmailInBannedListCheck(mockery, mockedInput, 1, true); CreateMockedReaderForEmailInBannedListCheck(mockery, mockedInput2, 1, false); // Mock the siteoption call for the checkusernameset option Stub.On(mockedInput).Method("GetSiteOptionValueBool").With("General", "CheckUserNameSet").Will(Return.Value(false)); Stub.On(mockedInput2).Method("GetSiteOptionValueBool").With("General", "CheckUserNameSet").Will(Return.Value(false)); // Now do the test. // 1. Test banned user is caught and not created in the database // 2. Test that a normal user can be created after the banned user // 3. Test to make sure the banned user is caught by the banned cookie list with out calling the database. // Banned user first call. This should check the database and then add their cookie to the list User bannedUser = new User(mockedInput); bannedUser.CreateUser(); // Normal user. This should check the database and return false for being in the banned list. This should create the user in the database. User normalUser = new User(mockedInput2); normalUser.CreateUser(); // Banned user second call. This should not call the database, but be caught by the cookie list. bannedUser.CreateUser(); // This will fail if the create user call got to check the data base more than once for both users! mockery.VerifyAllExpectationsHaveBeenMet(); Console.WriteLine("After TestNormalUserCanBeCreatedAfterABannedUserIsCaught"); }