public async Task GetEmailSettingsAsync_Should_Get_EmailSettings_Information_From_Repository()
        {
            // Arrange
            _adminPrivilege = InstanceAdminPrivileges.ViewInstanceSettings;

            // Act
            var emailSettingsDto = await _emailSettingsService.GetEmailSettingsAsync(UserId);

            // Assert
            Assert.AreEqual(_emailSettings.HostName, emailSettingsDto.Outgoing.ServerAddress);
            Assert.AreEqual(_emailSettings.Port, emailSettingsDto.Outgoing.Port);
            Assert.AreEqual(_emailSettings.EnableSSL, emailSettingsDto.Outgoing.EnableSsl);
            Assert.AreEqual(_emailSettings.Authenticated, emailSettingsDto.Outgoing.AuthenticatedSmtp);
            Assert.AreEqual(null, emailSettingsDto.Outgoing.AccountPassword);
            Assert.AreEqual(_emailSettings.UserName, emailSettingsDto.Outgoing.AccountUsername);
            Assert.AreEqual(_emailSettings.SenderEmailAddress, emailSettingsDto.Outgoing.AccountEmailAddress);
            Assert.AreEqual(false, emailSettingsDto.Outgoing.IsPasswordDirty);
            Assert.AreEqual(true, emailSettingsDto.Outgoing.HasPassword);

            Assert.AreEqual(_emailSettings.IncomingHostName, emailSettingsDto.Incoming.ServerAddress);
            Assert.AreEqual(_emailSettings.IncomingPort, emailSettingsDto.Incoming.Port);
            Assert.AreEqual(_emailSettings.IncomingServerType, (int)emailSettingsDto.Incoming.ServerType);
            Assert.AreEqual(_emailSettings.IncomingEnableSSL, emailSettingsDto.Incoming.EnableSsl);
            Assert.AreEqual(_emailSettings.IncomingUserName, emailSettingsDto.Incoming.AccountUsername);
            Assert.AreEqual(null, emailSettingsDto.Incoming.AccountPassword);
            Assert.AreEqual(false, emailSettingsDto.Incoming.IsPasswordDirty);
            Assert.AreEqual(true, emailSettingsDto.Incoming.HasPassword);

            Assert.AreEqual(_emailSettings.EnableNotifications, emailSettingsDto.EnableReviewNotifications);
            Assert.AreEqual(_emailSettings.EnableEmailDiscussion, emailSettingsDto.EnableEmailNotifications);
            Assert.AreEqual(_emailSettings.EnableEmailReplies, emailSettingsDto.EnableDiscussions);
        }
Ejemplo n.º 2
0
        public async Task Demand(int userId, InstanceAdminPrivileges privileges)
        {
            var currentPrivileges = await _privilegeRepository.GetInstanceAdminPrivilegesAsync(userId);

            if (!currentPrivileges.HasFlag(privileges))
            {
                throw new AuthorizationException(ErrorMessages.UserDoesNotHavePermissions, ErrorCodes.Forbidden);
            }
        }
Ejemplo n.º 3
0
        public async Task DemandAny(int userId, int projectId, InstanceAdminPrivileges instancePrivileges, ProjectAdminPrivileges projectPrivileges)
        {
            var instancePermissions = await _privilegeRepository.GetInstanceAdminPrivilegesAsync(userId);

            if (!instancePermissions.HasFlag(instancePrivileges))
            {
                var projectPermissions = await _privilegeRepository.GetProjectAdminPermissionsAsync(userId, projectId);

                if (!projectPermissions.HasFlag(projectPrivileges))
                {
                    throw new AuthorizationException(ErrorMessages.UserDoesNotHavePermissions, ErrorCodes.Forbidden);
                }
            }
        }
        public async Task SendTestEmailAsync_Should_Throw_AuthorizationException_When_User_Doesnt_Have_ManageInstanceSettings_Privilege()
        {
            // Arrange
            _adminPrivilege = InstanceAdminPrivileges.ViewInstanceSettings;

            // Act
            try
            {
                await _emailSettingsService.SendTestEmailAsync(UserId, _outgoingSettings);
            }
            // Assert
            catch (AuthorizationException ex)
            {
                Assert.AreEqual(ex.ErrorCode, ErrorCodes.Forbidden);
                return;
            }

            Assert.Fail("A BadRequestException was not thrown.");
        }
        public async Task GetEmailSettingsAsync_Should_Throw_AuthorizationException_When_User_Doesnt_Have_ViewInstanceSettings()
        {
            // Arrange
            _adminPrivilege = InstanceAdminPrivileges.ManageProjects;

            // Act
            try
            {
                await _emailSettingsService.GetEmailSettingsAsync(UserId);
            }
            // Assert
            catch (AuthorizationException ex)
            {
                Assert.AreEqual(ex.ErrorCode, ErrorCodes.Forbidden);

                return;
            }

            Assert.Fail("AuthorizationException was not thrown.");
        }
        public async Task CheckIncomingEmailConnectionAsync_Should_Throw_AuthorizationException_When_User_Doesnt_Have_ManageInstanceSettings_Permission()
        {
            // Arrange
            _adminPrivilege = InstanceAdminPrivileges.ViewInstanceSettings;

            // Act
            try
            {
                await _emailSettingsService.TestIncomingEmailConnectionAsync(UserId, _incomingSettings);
            }
            // Assert
            catch (AuthorizationException ex)
            {
                Assert.AreEqual(ErrorCodes.Forbidden, ex.ErrorCode);

                return;
            }

            Assert.Fail("No AuthorizationException was thrown.");
        }
        public void Initialize()
        {
            Mock <IPrivilegesRepository>  privilegesRepositoryMock  = new Mock <IPrivilegesRepository>();
            Mock <IUserRepository>        userRepositoryMock        = new Mock <IUserRepository>();
            Mock <IWebsiteAddressService> websiteAddressServiceMock = new Mock <IWebsiteAddressService>();

            _instanceSettingsRepositoryMock = new Mock <IInstanceSettingsRepository>();
            _emailHelperMock          = new Mock <IEmailHelper>();
            _incomingEmailServiceMock = new Mock <IIncomingEmailService>();

            _emailSettingsService = new EmailSettingsService(new PrivilegesManager(privilegesRepositoryMock.Object),
                                                             userRepositoryMock.Object,
                                                             _emailHelperMock.Object,
                                                             websiteAddressServiceMock.Object,
                                                             _instanceSettingsRepositoryMock.Object,
                                                             _incomingEmailServiceMock.Object);

            privilegesRepositoryMock.Setup(repo => repo.GetInstanceAdminPrivilegesAsync(UserId)).ReturnsAsync(() => _adminPrivilege);

            userRepositoryMock.Setup(repo => repo.GetUserAsync(UserId)).ReturnsAsync(() => _user);

            websiteAddressServiceMock.Setup(service => service.GetWebsiteAddress()).Returns(WebsiteAddress);

            _instanceSettingsRepositoryMock.Setup(repo => repo.GetEmailSettings()).ReturnsAsync(() => _emailSettings);

            // Setup Default Values
            _outgoingSettings = new EmailOutgoingSettings()
            {
                AuthenticatedSmtp   = true,
                AccountPassword     = "******",
                AccountUsername     = "******",
                AccountEmailAddress = "*****@*****.**",
                EnableSsl           = true,
                Port            = 2,
                ServerAddress   = "smtp.blueprintsys.com",
                IsPasswordDirty = true
            };

            _incomingSettings = new EmailIncomingSettings()
            {
                AccountUsername = "******",
                AccountPassword = "******",
                EnableSsl       = true,
                Port            = 2,
                ServerAddress   = "mail.test.com",
                ServerType      = EmailClientType.Imap,
                IsPasswordDirty = true
            };

            _adminPrivilege = InstanceAdminPrivileges.ManageInstanceSettings;

            _user = new User()
            {
                Email = "*****@*****.**"
            };

            _emailSettings = new EmailSettings()
            {
                Password              = EncryptedPassword,
                IncomingPassword      = EncryptedPassword,
                Authenticated         = true,
                EnableEmailDiscussion = false,
                EnableEmailReplies    = true,
                EnableNotifications   = true,
                EnableSSL             = true,
                SenderEmailAddress    = "*****@*****.**",
                HostName              = "smtp.test.com",
                Port               = 1234,
                UserName           = "******",
                IncomingEnableSSL  = false,
                IncomingHostName   = "pop3.test.com",
                IncomingPort       = 2345,
                IncomingServerType = 0,
                IncomingUserName   = "******"
            };

            _emailSettingsDto = new EmailSettingsDto()
            {
                EnableDiscussions         = false,
                EnableEmailNotifications  = true,
                EnableReviewNotifications = false,
                Incoming = new EmailIncomingSettings()
                {
                    AccountPassword = "******",
                    AccountUsername = "******",
                    EnableSsl       = true,
                    IsPasswordDirty = true,
                    Port            = 8765,
                    ServerAddress   = "imap.test.com",
                    ServerType      = EmailClientType.Pop3
                },
                Outgoing = new EmailOutgoingSettings()
                {
                    AccountEmailAddress = "*****@*****.**",
                    AccountPassword     = "******",
                    AccountUsername     = "******",
                    AuthenticatedSmtp   = true,
                    EnableSsl           = true,
                    IsPasswordDirty     = true,
                    Port          = 9876,
                    ServerAddress = "mail.test.com"
                }
            };
        }