Ejemplo n.º 1
0
        public void CreateDownloadShare_SmsRecipientsNoAccessPw_Fail()
        {
            // ARRANGE
            Node node = FactoryNode.Node;

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

            req.AccessPassword     = null;
            req.EncryptionPassword = null;
            req.SmsRecipients      = new List <string>();
            req.EmailRecipients    = null;
            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().Occurs(1);

            // ACT - ASSERT
            Assert.Throws <ArgumentException>(() => s.CreateDownloadShare(req));
            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(c.NodesImpl);
            Mock.Assert(c.Executor);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        public void ToUnencryptedApiCreateDownloadShareRequest(string smsRecipients, string emailRecipients)
        {
            // ARRANGE
            ApiCreateDownloadShareRequest expected = FactoryShare.ApiCreateDownloadShareRequest;

            List <string> smsRecList = null;

            if (smsRecipients != null)
            {
                smsRecList = new List <string>();
                smsRecList.AddRange(smsRecipients.Split(','));
                expected.SmsRecipients = smsRecipients;
                expected.SendSms       = true;
            }

            List <string> emailRecList = null;

            if (emailRecipients != null)
            {
                emailRecList = new List <string>();
                emailRecList.AddRange(emailRecipients.Split(','));
                expected.MailBody       = "Some body";
                expected.MailSubject    = "You received a DRACOON share!";
                expected.MailRecipients = emailRecipients;
                expected.SendMail       = true;
            }

            CreateDownloadShareRequest param = new CreateDownloadShareRequest(expected.NodeId)
            {
                Name                = expected.Name,
                Notes               = expected.Notes,
                Expiration          = expected.Expiration.ExpireAt,
                ShowCreatorName     = expected.ShowCreatorName,
                ShowCreatorUserName = expected.ShowCreatorUserName,
                NotifyCreator       = expected.NotifyCreator,
                MaxAllowedDownloads = expected.MaxAllowedDownloads,
                AccessPassword      = expected.Password,
                EmailRecipients     = emailRecList,
                EmailBody           = expected.MailBody,
                EmailSubject        = expected.MailSubject,
                SmsRecipients       = smsRecList
            };

            // ACT
            ApiCreateDownloadShareRequest actual = ShareMapper.ToUnencryptedApiCreateDownloadShareRequest(param);

            // ASSERT
            Assert.Equal(expected, actual, new ApiCreateDownloadShareRequestComparer());
        }
Ejemplo n.º 4
0
        internal static ApiCreateDownloadShareRequest ToUnencryptedApiCreateDownloadShareRequest(CreateDownloadShareRequest request)
        {
            ApiExpiration apiExpiration = null;

            if (request.Expiration.HasValue)
            {
                apiExpiration = new ApiExpiration {
                    ExpireAt         = request.Expiration,
                    EnableExpiration = request.Expiration.Value.Ticks != 0
                };
            }

            ApiCreateDownloadShareRequest apiCreateDownloadShareRequest = new ApiCreateDownloadShareRequest {
                NodeId              = request.NodeId,
                Name                = request.Name,
                Notes               = request.Notes,
                Expiration          = apiExpiration,
                ShowCreatorName     = request.ShowCreatorName,
                ShowCreatorUserName = request.ShowCreatorUserName,
                NotifyCreator       = request.NotifyCreator,
                MaxAllowedDownloads = request.MaxAllowedDownloads,
                Password            = request.AccessPassword
            };

            if (request.EmailRecipients != null)
            {
                // Check if the list is not empty is still in the previous validator done
                apiCreateDownloadShareRequest.SendMail       = true;
                apiCreateDownloadShareRequest.MailRecipients = GenerateRecipientString(request.EmailRecipients);
                apiCreateDownloadShareRequest.MailBody       = request.EmailBody;
                apiCreateDownloadShareRequest.MailSubject    = request.EmailSubject;
            }
            else
            {
                apiCreateDownloadShareRequest.SendMail = false;
            }

            if (request.SmsRecipients != null)
            {
                // Check if the list is not empty is still in the previous validator done
                apiCreateDownloadShareRequest.SendSms       = true;
                apiCreateDownloadShareRequest.SmsRecipients = GenerateRecipientString(request.SmsRecipients);
            }
            else
            {
                apiCreateDownloadShareRequest.SendSms = false;
            }

            return(apiCreateDownloadShareRequest);
        }
Ejemplo n.º 5
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));
        }