Example #1
0
        /// <inheritdoc />
        public RequestResult <IEnumerable <UserNote> > GetNotes(string hdId, int page = 0, int pageSize = 500)
        {
            int offset = page * pageSize;
            DBResult <IEnumerable <Note> > dbNotes = this.noteDelegate.GetNotes(hdId, offset, pageSize);

            UserProfile profile = this.profileDelegate.GetUserProfile(hdId).Payload;
            string?     key     = profile.EncryptionKey;

            // If there is no key yet, generate one and store it in the profile. Only valid while not all profiles have a encryption key.
            if (key == null)
            {
                this.logger.LogInformation($"First time note retrieval with key for user ${hdId}");
                key     = this.EncryptFirstTime(profile, dbNotes.Payload);
                dbNotes = this.noteDelegate.GetNotes(hdId, offset, pageSize);
            }

            // Check that the key has been set
            if (key == null)
            {
                this.logger.LogError($"User does not have a key: ${hdId}");
                return(new RequestResult <IEnumerable <UserNote> >()
                {
                    ResultStatus = ResultType.Error,
                    ResultError = new RequestResultError()
                    {
                        ResultMessage = "Profile Key not set", ErrorCode = ErrorTranslator.InternalError(ErrorType.InvalidState)
                    },
                });
            }

            RequestResult <IEnumerable <UserNote> > result = new RequestResult <IEnumerable <UserNote> >()
            {
                ResourcePayload  = UserNote.CreateListFromDbModel(dbNotes.Payload, this.cryptoDelegate, key),
                PageIndex        = page,
                PageSize         = pageSize,
                TotalResultCount = dbNotes.Payload.ToList().Count,
                ResultStatus     = dbNotes.Status == DBStatusCode.Read ? ResultType.Success : ResultType.Error,
                ResultError      = dbNotes.Status == DBStatusCode.Read ? null : new RequestResultError()
                {
                    ResultMessage = dbNotes.Message, ErrorCode = ErrorTranslator.ServiceError(ErrorType.CommunicationInternal, ServiceType.Database)
                },
            };

            return(result);
        }
        private Tuple <RequestResult <IEnumerable <UserNote> >, List <UserNote> > ExecuteGetNotes(string encryptionKey = null, Database.Constants.DBStatusCode notesDBResultStatus = Database.Constants.DBStatusCode.Read)
        {
            DBResult <UserProfile> profileDBResult = new DBResult <UserProfile>
            {
                Payload = new UserProfile()
                {
                    EncryptionKey = encryptionKey
                }
            };

            Mock <IUserProfileDelegate> profileDelegateMock = new Mock <IUserProfileDelegate>();

            profileDelegateMock.Setup(s => s.GetUserProfile(hdid)).Returns(profileDBResult);

            Mock <ICryptoDelegate> cryptoDelegateMock = new Mock <ICryptoDelegate>();

            cryptoDelegateMock.Setup(s => s.Encrypt(It.IsAny <string>(), It.IsAny <string>())).Returns((string key, string text) => text + key);
            cryptoDelegateMock.Setup(s => s.Decrypt(It.IsAny <string>(), It.IsAny <string>())).Returns((string key, string text) => text.Remove(text.Length - key.Length));

            List <Note> noteList = new List <Note>();

            noteList.Add(new Note
            {
                HdId            = hdid,
                Title           = "First Note",
                Text            = "First Note text",
                CreatedDateTime = new DateTime(2020, 1, 1)
            });

            noteList.Add(new Note
            {
                HdId            = hdid,
                Title           = "Second Note",
                Text            = "Second Note text",
                CreatedDateTime = new DateTime(2020, 2, 2)
            });
            List <UserNote> userNoteList = null;

            if (encryptionKey != null)
            {
                userNoteList = UserNote.CreateListFromDbModel(noteList, cryptoDelegateMock.Object, encryptionKey).ToList();
            }

            DBResult <IEnumerable <Note> > notesDBResult = new DBResult <IEnumerable <Note> >
            {
                Payload = noteList,
                Status  = notesDBResultStatus
            };

            Mock <INoteDelegate> noteDelegateMock = new Mock <INoteDelegate>();

            noteDelegateMock.Setup(s => s.GetNotes(hdid, 0, 500)).Returns(notesDBResult);

            INoteService service = new NoteService(
                new Mock <ILogger <NoteService> >().Object,
                noteDelegateMock.Object,
                profileDelegateMock.Object,
                cryptoDelegateMock.Object
                );

            var userNoteResult = service.GetNotes(hdid, 0, 500);

            return(new Tuple <RequestResult <IEnumerable <UserNote> >, List <UserNote> >(userNoteResult, userNoteList));
        }
Example #3
0
        public void ShouldGetNotes()
        {
            string encryptionKey = "abc";
            DBResult <UserProfile> profileDBResult = new DBResult <UserProfile>
            {
                Payload = new UserProfile()
                {
                    EncryptionKey = encryptionKey
                }
            };

            Mock <IUserProfileDelegate> profileDelegateMock = new Mock <IUserProfileDelegate>();

            profileDelegateMock.Setup(s => s.GetUserProfile(hdid)).Returns(profileDBResult);

            Mock <ICryptoDelegate> cryptoDelegateMock = new Mock <ICryptoDelegate>();

            cryptoDelegateMock.Setup(s => s.Encrypt(It.IsAny <string>(), It.IsAny <string>())).Returns((string key, string text) => text + key);
            cryptoDelegateMock.Setup(s => s.Decrypt(It.IsAny <string>(), It.IsAny <string>())).Returns((string key, string text) => text.Remove(text.Length - key.Length));

            List <Note> noteList = new List <Note>();

            noteList.Add(new Note
            {
                HdId            = hdid,
                Title           = "First Note",
                Text            = "First Note text",
                CreatedDateTime = new DateTime(2020, 1, 1)
            });

            noteList.Add(new Note
            {
                HdId            = hdid,
                Title           = "Second Note",
                Text            = "Second Note text",
                CreatedDateTime = new DateTime(2020, 2, 2)
            });
            List <UserNote> userNoteList = UserNote.CreateListFromDbModel(noteList, cryptoDelegateMock.Object, encryptionKey).ToList();

            DBResult <IEnumerable <Note> > notesDBResult = new DBResult <IEnumerable <Note> >
            {
                Payload = noteList,
                Status  = Database.Constants.DBStatusCode.Read
            };

            Mock <INoteDelegate> noteDelegateMock = new Mock <INoteDelegate>();

            noteDelegateMock.Setup(s => s.GetNotes(hdid, 0, 500)).Returns(notesDBResult);

            INoteService service = new NoteService(
                new Mock <ILogger <NoteService> >().Object,
                noteDelegateMock.Object,
                profileDelegateMock.Object,
                cryptoDelegateMock.Object
                );

            RequestResult <IEnumerable <UserNote> > actualResult = service.GetNotes(hdid, 0, 500);

            Assert.Equal(Common.Constants.ResultType.Success, actualResult.ResultStatus);
            Assert.True(actualResult.ResourcePayload.IsDeepEqual(userNoteList));
        }