示例#1
0
        public void CreateDownloadShare_Success()
        {
            // ARRANGE
            Node node = FactoryNode.Node;

            node.IsEncrypted = true;
            node.Type        = NodeType.File;
            CreateDownloadShareRequest req = FactoryShare.CreateDownloadShareRequest;

            req.EmailRecipients = new List <string> {
                "985678"
            };
            req.EmailBody     = "Any body!";
            req.EmailSubject  = "Any subject!";
            req.SmsRecipients = new List <string> {
                "28436054"
            };
            IInternalDracoonClient c = FactoryClients.InternalDracoonClientMock(true);
            DracoonSharesImpl      s = new DracoonSharesImpl(c);

            Mock.Arrange(() => Arg.IsAny <CreateDownloadShareRequest>().MustNotNull(Arg.AnyString)).DoNothing().Occurs(1);
            Mock.Arrange(() => c.NodesImpl.GetNode(Arg.AnyLong)).Returns(node).Occurs(1);
            Mock.Arrange(() => Arg.AnyString.MustNotNullOrEmptyOrWhitespace(Arg.AnyString, Arg.AnyBool)).DoNothing().OccursAtLeast(1);
            Mock.Arrange(() => Arg.IsAny <int?>().NullableMustPositive(Arg.AnyString)).DoNothing().Occurs(1);
            Mock.Arrange(() => Arg.IsAny <IEnumerable <string> >().EnumerableMustNotNullOrEmpty(Arg.AnyString)).DoNothing();
            Mock.Arrange(() => ShareMapper.ToUnencryptedApiCreateDownloadShareRequest(Arg.IsAny <CreateDownloadShareRequest>()))
            .Returns(FactoryShare.ApiCreateDownloadShareRequest).Occurs(1);
            Mock.Arrange(() => c.AccountImpl.GetAndCheckUserKeyPair()).Returns(FactoryUser.UserKeyPair).Occurs(1);
            Mock.Arrange(() => c.NodesImpl.GetEncryptedFileKey(Arg.AnyLong)).Returns(FactoryFile.EncryptedFileKey).Occurs(1);
            Mock.Arrange(() => c.NodesImpl.DecryptFileKey(Arg.IsAny <EncryptedFileKey>(), Arg.IsAny <UserPrivateKey>(), Arg.IsAny <long?>())).Returns(FactoryFile.PlainFileKey).Occurs(1);
            Mock.Arrange(() => c.AccountImpl.GenerateNewUserKeyPair(Arg.AnyString)).Returns(FactoryUser.UserKeyPair);
            Mock.Arrange(() => c.NodesImpl.EncryptFileKey(Arg.IsAny <PlainFileKey>(), Arg.IsAny <UserPublicKey>(), Arg.IsAny <long?>())).Returns(FactoryFile.EncryptedFileKey).Occurs(1);
            Mock.Arrange(() => UserMapper.ToApiUserKeyPair(Arg.IsAny <UserKeyPair>())).Returns(FactoryUser.ApiUserKeyPair).Occurs(1);
            Mock.Arrange(() => FileMapper.ToApiFileKey(Arg.IsAny <EncryptedFileKey>())).Returns(FactoryFile.ApiFileKey).Occurs(1);
            Mock.Arrange(() => c.Builder.PostCreateDownloadShare(Arg.IsAny <ApiCreateDownloadShareRequest>())).Returns(FactoryRestSharp.PostCreateDownloadShareMock()).Occurs(1);
            Mock.Arrange(() => c.Executor.DoSyncApiCall <ApiDownloadShare>(Arg.IsAny <IRestRequest>(), RequestType.PostCreateDownloadShare, 0))
            .Returns(FactoryShare.ApiDownloadShare).Occurs(1);
            Mock.Arrange(() => ShareMapper.FromApiDownloadShare(Arg.IsAny <ApiDownloadShare>())).Returns(FactoryShare.DownloadShare).Occurs(1);

            // ACT
            DownloadShare actual = s.CreateDownloadShare(req);

            // ASSERT
            Assert.NotNull(actual);
            Mock.Assert(() => Arg.IsAny <CreateDownloadShareRequest>().MustNotNull(Arg.AnyString));
            Mock.Assert(() => Arg.AnyString.MustNotNullOrEmptyOrWhitespace(Arg.AnyString, Arg.AnyBool));
            Mock.Assert(() => Arg.IsAny <int?>().NullableMustPositive(Arg.AnyString));
            Mock.Assert(() => Arg.IsAny <IEnumerable <string> >().EnumerableMustNotNullOrEmpty(Arg.AnyString));
            Mock.Assert(() => ShareMapper.ToUnencryptedApiCreateDownloadShareRequest(Arg.IsAny <CreateDownloadShareRequest>()));
            Mock.Assert(() => UserMapper.ToApiUserKeyPair(Arg.IsAny <UserKeyPair>()));
            Mock.Assert(() => FileMapper.ToApiFileKey(Arg.IsAny <EncryptedFileKey>()));
            Mock.Assert(() => ShareMapper.FromApiDownloadShare(Arg.IsAny <ApiDownloadShare>()));
            Mock.Assert(c.NodesImpl);
            Mock.Assert(c.AccountImpl);
            Mock.Assert(c.Builder);
            Mock.Assert(c.Executor);
        }
示例#2
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());
        }
示例#3
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());
        }
示例#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));
        }