Exemplo n.º 1
0
        public void FromApiDownloadShareList()
        {
            // ARRANGE
            Classification    expectedClassification = Classification.StrictlyConfidential;
            DownloadShareList expected = FactoryShare.DownloadShareList;

            ApiDownloadShareList param = new ApiDownloadShareList {
                Range = new ApiRange {
                    Offset = expected.Offset,
                    Limit  = expected.Limit,
                    Total  = expected.Total
                },
                Items = new List <ApiDownloadShare>(expected.Items.Count)
            };

            foreach (DownloadShare current in expected.Items)
            {
                current.Classification = expectedClassification;
                ApiDownloadShare currentApi = new ApiDownloadShare {
                    ShareId               = current.ShareId,
                    NodeId                = current.NodeId,
                    NodePath              = current.NodePath,
                    Name                  = current.Name,
                    Notes                 = current.Notes,
                    Classification        = (int)current.Classification,
                    ExpireAt              = current.ExpireAt,
                    AccessKey             = current.AccessKey,
                    ShowCreatorName       = current.ShowCreatorName,
                    ShowCreatorUserName   = current.ShowCreatorUserName,
                    NotifyCreator         = current.NotifyCreator,
                    MaxAllowedDownloads   = current.MaxAllowedDownloads,
                    CurrentDownloadsCount = current.CurrentDownloadsCount,
                    CreatedAt             = current.CreatedAt,
                    CreatedBy             = new ApiUserInfo {
                        AvatarUuid  = current.CreatedBy.AvatarUUID,
                        DisplayName = current.CreatedBy.DisplayName,
                        Id          = current.CreatedBy.Id.Value
                    },
                    IsProtected = current.IsProtected,
                    IsEncrypted = current.IsEncrypted
                };
                param.Items.Add(currentApi);
                Mock.Arrange(() => ShareMapper.FromApiDownloadShare(currentApi)).Returns(current);
            }

            // ACT
            DownloadShareList actual = ShareMapper.FromApiDownloadShareList(param);

            // ASSERT
            Assert.Equal(expected, actual, new DownloadShareListComparer());
        }
Exemplo n.º 2
0
        public void FromApiDownloadShare()
        {
            // ARRANGE
            DownloadShare expected = FactoryShare.DownloadShare;

            expected.Classification = Classification.Confidential;

            ApiDownloadShare param = new ApiDownloadShare {
                ShareId               = expected.ShareId,
                NodeId                = expected.NodeId,
                NodePath              = expected.NodePath,
                Name                  = expected.Name,
                Notes                 = expected.Notes,
                Classification        = (int)expected.Classification,
                ExpireAt              = expected.ExpireAt,
                AccessKey             = expected.AccessKey,
                ShowCreatorName       = expected.ShowCreatorName,
                ShowCreatorUserName   = expected.ShowCreatorUserName,
                NotifyCreator         = expected.NotifyCreator,
                MaxAllowedDownloads   = expected.MaxAllowedDownloads,
                CurrentDownloadsCount = expected.CurrentDownloadsCount,
                CreatedAt             = expected.CreatedAt,
                CreatedBy             = new ApiUserInfo {
                    AvatarUuid  = expected.CreatedBy.AvatarUUID,
                    DisplayName = expected.CreatedBy.DisplayName,
                    Id          = expected.CreatedBy.Id.Value
                },
                IsProtected = expected.IsProtected,
                IsEncrypted = expected.IsEncrypted
            };

            Mock.Arrange(() => EnumConverter.ConvertClassificationEnumToValue(expected.Classification)).Returns((int)expected.Classification);
            Mock.Arrange(() => UserMapper.FromApiUserInfo(param.CreatedBy)).Returns(expected.CreatedBy);

            // ACT
            DownloadShare actual = ShareMapper.FromApiDownloadShare(param);

            // ASSERT
            Assert.Equal(expected, actual, new DownloadShareComparer());
        }
Exemplo n.º 3
0
        internal static DownloadShare FromApiDownloadShare(ApiDownloadShare apiDownloadShare)
        {
            DownloadShare downloadShare = new DownloadShare {
                ShareId               = apiDownloadShare.ShareId,
                NodeId                = apiDownloadShare.NodeId,
                NodePath              = apiDownloadShare.NodePath,
                Name                  = apiDownloadShare.Name,
                Notes                 = apiDownloadShare.Notes,
                Classification        = EnumConverter.ConvertValueToClassificationEnum(apiDownloadShare.Classification),
                ExpireAt              = apiDownloadShare.ExpireAt,
                AccessKey             = apiDownloadShare.AccessKey,
                ShowCreatorName       = apiDownloadShare.ShowCreatorName,
                ShowCreatorUserName   = apiDownloadShare.ShowCreatorUserName,
                NotifyCreator         = apiDownloadShare.NotifyCreator,
                MaxAllowedDownloads   = apiDownloadShare.MaxAllowedDownloads,
                CurrentDownloadsCount = apiDownloadShare.CurrentDownloadsCount,
                CreatedAt             = apiDownloadShare.CreatedAt,
                CreatedBy             = UserMapper.FromApiUserInfo(apiDownloadShare.CreatedBy),
                IsProtected           = apiDownloadShare.IsProtected,
                IsEncrypted           = apiDownloadShare.IsEncrypted
            };

            return(downloadShare);
        }
Exemplo n.º 4
0
        public DownloadShare CreateDownloadShare(CreateDownloadShareRequest request)
        {
            _client.Executor.CheckApiServerVersion();

            #region Parameter Validation

            request.MustNotNull(nameof(request));

            Node targetNode = _client.NodesImpl.GetNode(request.NodeId);
            // Node id is still checked in previous called getNode()
            // To save much effort throw this restriction instantly and not let the rest api throw this error
            if (targetNode.IsEncrypted.GetValueOrDefault(false) && targetNode.Type != NodeType.File)
            {
                throw new DracoonApiException(DracoonApiCode.VALIDATION_DL_SHARE_CANNOT_CREATE_ON_ENCRYPTED_ROOM_FOLDER);
            }

            request.Name.MustNotNullOrEmptyOrWhitespace(nameof(request.Name), true);
            request.MaxAllowedDownloads.NullableMustPositive(nameof(request.MaxAllowedDownloads));
            if (targetNode.IsEncrypted.GetValueOrDefault(false) && string.IsNullOrWhiteSpace(request.EncryptionPassword) &&
                !string.IsNullOrWhiteSpace(request.AccessPassword))
            {
                throw new ArgumentException("Download share of a encrypted node must have a encryption password and no access password.");
            }

            if (!targetNode.IsEncrypted.GetValueOrDefault(false) && string.IsNullOrWhiteSpace(request.AccessPassword) &&
                !string.IsNullOrWhiteSpace(request.EncryptionPassword))
            {
                throw new ArgumentException("Download share of a not encrypted node must have a access password and no encryption password.");
            }

            if (targetNode.IsEncrypted.GetValueOrDefault(false) && string.IsNullOrWhiteSpace(request.EncryptionPassword))
            {
                throw new ArgumentException("Download share of a encrypted node must have a encryption password.");
            }

            if (!targetNode.IsEncrypted.GetValueOrDefault(false))
            {
                request.AccessPassword?.MustNotNullOrEmptyOrWhitespace(nameof(request.AccessPassword));
            }

            if (request.EmailRecipients != null)
            {
                request.EmailRecipients.EnumerableMustNotNullOrEmpty(nameof(request.EmailRecipients));
                request.EmailRecipients.ForEach(current => current.MustNotNullOrEmptyOrWhitespace(nameof(request.EmailRecipients) + " element"));
                request.EmailBody.MustNotNullOrEmptyOrWhitespace(nameof(request.EmailBody));
                request.EmailSubject.MustNotNullOrEmptyOrWhitespace(nameof(request.EmailSubject));
            }

            if (request.SmsRecipients != null)
            {
                request.SmsRecipients.EnumerableMustNotNullOrEmpty(nameof(request.SmsRecipients));
                request.SmsRecipients.ForEach(current => current.MustNotNullOrEmptyOrWhitespace(nameof(request.SmsRecipients) + " element"));
                if (string.IsNullOrEmpty(request.AccessPassword))
                {
                    throw new ArgumentException("If a SMS should be sent, a access password must be set.");
                }
            }

            #endregion

            ApiCreateDownloadShareRequest apiRequest = ShareMapper.ToUnencryptedApiCreateDownloadShareRequest(request);
            if (targetNode.IsEncrypted.GetValueOrDefault(false))
            {
                UserKeyPair      creatorKeyPair          = _client.AccountImpl.GetAndCheckUserKeyPair();
                EncryptedFileKey creatorEncryptedFileKey = _client.NodesImpl.GetEncryptedFileKey(request.NodeId);
                PlainFileKey     plainFileKey            = _client.NodesImpl.DecryptFileKey(creatorEncryptedFileKey, creatorKeyPair.UserPrivateKey, request.NodeId);
                UserKeyPair      newGeneratedKeyPair     = _client.AccountImpl.GenerateNewUserKeyPair(request.EncryptionPassword);
                EncryptedFileKey newEncryptedFileKey     =
                    _client.NodesImpl.EncryptFileKey(plainFileKey, newGeneratedKeyPair.UserPublicKey, request.NodeId);

                apiRequest.KeyPair = UserMapper.ToApiUserKeyPair(newGeneratedKeyPair);
                apiRequest.FileKey = FileMapper.ToApiFileKey(newEncryptedFileKey);
            }

            IRestRequest     restRequest = _client.Builder.PostCreateDownloadShare(apiRequest);
            ApiDownloadShare resultShare =
                _client.Executor.DoSyncApiCall <ApiDownloadShare>(restRequest, DracoonRequestExecutor.RequestType.PostCreateDownloadShare);
            return(ShareMapper.FromApiDownloadShare(resultShare));
        }