public void ExternalUserAssociationCheckResultTrueCallsSecurityLog()
		{
			var manager = GetManager();
			var association = new ExternalUserAssociation { Issuer = "Google", UserID = 123, ProviderKey = "abc" };
			var user = new User(association.UserID, DateTime.MinValue);
			_externalUserAssociationRepo.Setup(x => x.Get(association.Issuer, association.ProviderKey)).Returns(association);
			_userRepo.Setup(x => x.GetUser(association.UserID)).Returns(user);
			const string ip = "1.1.1.1";
			var authResult = new ExternalAuthenticationResult { Issuer = "Google", ProviderKey = "abc" };

			manager.ExternalUserAssociationCheck(authResult, ip);

			_securityLogService.Verify(x => x.CreateLogEntry(user, user, ip, It.IsAny<string>(), SecurityLogType.ExternalAssociationCheckSuccessful));
		}
		public void ExternalUserAssociationCheckResultTrueWithHydratedResultIfMatchingAssociationAndUser()
		{
			var manager = GetManager();
			var association = new ExternalUserAssociation { Issuer = "Google", UserID = 123, ProviderKey = "abc"};
			var user = new User(association.UserID, DateTime.MinValue);
			_externalUserAssociationRepo.Setup(x => x.Get(association.Issuer, association.ProviderKey)).Returns(association);
			_userRepo.Setup(x => x.GetUser(association.UserID)).Returns(user);
			var authResult = new ExternalAuthenticationResult {Issuer = "Google", ProviderKey = "abc"};

			var result = manager.ExternalUserAssociationCheck(authResult, "");

			Assert.IsTrue(result.Successful);
			Assert.AreSame(user, result.User);
			Assert.AreSame(association, result.ExternalUserAssociation);
		}
		public ExternalUserAssociation Get(int externalUserAssociationID)
		{
			ExternalUserAssociation externalUserAssociation = null;
			_sqlObjectFactory.GetConnection().Using(connection => connection.Command("SELECT ExternalUserAssociationID, UserID, Issuer, ProviderKey, Name FROM pf_ExternalUserAssociation WHERE ExternalUserAssociationID = @ExternalUserAssociationID")
					.AddParameter("@ExternalUserAssociationID", externalUserAssociationID)
					.ExecuteReader()
					.ReadOne(r => externalUserAssociation = new ExternalUserAssociation
					{
						ExternalUserAssociationID = r.GetInt32(0),
						UserID = r.GetInt32(1),
						Issuer = r.GetString(2),
						ProviderKey = r.GetString(3),
						Name = r.GetString(4)
					}));
			return externalUserAssociation;
		}
		public void GetExternalUserAssociationsCallsRepoWithMatchingUserIDs()
		{
			var manager = GetManager();
			var user = new User(123, DateTime.MaxValue);
			var association = new ExternalUserAssociation { ExternalUserAssociationID = 456, UserID = user.UserID };
			_externalUserAssociationRepo.Setup(x => x.Get(association.ExternalUserAssociationID)).Returns(association);

			manager.RemoveAssociation(user, association.ExternalUserAssociationID, String.Empty);

			_externalUserAssociationRepo.Verify(x => x.Delete(association.ExternalUserAssociationID), Times.Once());
		}
		public void GetExternalUserAssociationsThrowsIfAssociationDoesntMatchUser()
		{
			var manager = GetManager();
			var association = new ExternalUserAssociation { ExternalUserAssociationID = 456, UserID = 789};
			var user = new User(123, DateTime.MaxValue);
			_externalUserAssociationRepo.Setup(x => x.Get(association.ExternalUserAssociationID)).Returns(association);

			Assert.Throws<Exception>(() => manager.RemoveAssociation(user, association.ExternalUserAssociationID, String.Empty));
		}
		public void RemoveAssociationCallsRepoOnSuccessfulMatch()
		{
			var manager = GetManager();
			var association = new ExternalUserAssociation { ExternalUserAssociationID = 123, UserID = 456 };
			var user = new User(association.UserID, DateTime.MaxValue);
			_externalUserAssociationRepo.Setup(x => x.Get(association.ExternalUserAssociationID)).Returns(association);

			manager.RemoveAssociation(user, association.ExternalUserAssociationID, String.Empty);

			_externalUserAssociationRepo.Verify(x => x.Delete(association.ExternalUserAssociationID), Times.Once());
		}
		public void RemoveAssociationLogsTheRemoval()
		{
			var manager = GetManager();
			var association = new ExternalUserAssociation {ExternalUserAssociationID = 123, Issuer = "Google", Name = "Jeffy", ProviderKey = "oihfoihfef", UserID = 456};
			var user = new User(association.UserID, DateTime.MaxValue);
			const string ip = "1.1.1.1";
			_externalUserAssociationRepo.Setup(x => x.Get(association.ExternalUserAssociationID)).Returns(association);

			manager.RemoveAssociation(user, association.ExternalUserAssociationID, ip);

			_securityLogService.Verify(x => x.CreateLogEntry(user, user, ip, It.IsAny<string>(), SecurityLogType.ExternalAssociationRemoved), Times.Once());
		}
		public void ExternalUserAssociationCheckResultFalseNoUserCallsSecurityLog()
		{
			var manager = GetManager();
			var association = new ExternalUserAssociation { Issuer = "Google", UserID = 123, ProviderKey = "abc" };
			_externalUserAssociationRepo.Setup(x => x.Get(association.Issuer, association.ProviderKey)).Returns(association);
			_userRepo.Setup(x => x.GetUser(association.UserID)).Returns((User)null);
			const string ip = "1.1.1.1";
			var authResult = new ExternalAuthenticationResult { Issuer = "Google", ProviderKey = "abc" };

			manager.ExternalUserAssociationCheck(authResult, ip);

			_securityLogService.Verify(x => x.CreateLogEntry((int?)null, null, ip, It.IsAny<string>(), SecurityLogType.ExternalAssociationCheckFailed), Times.Once());
		}