public void UpdateOrCreateToken(Guid userId, string dropboxToken = null, string exactToken = null, DateTime? exactTokenExpiration = null)
        {
            var userToken = _context.UserTokens.SingleOrDefault(x => x.UserId == userId);

            if (userToken == null)
            {
                userToken = new UserToken
                {
                    Id = Guid.NewGuid(),
                    UserId = userId
                };

                _context.UserTokens.Add(userToken);
            }

            userToken.DropboxAccessToken = dropboxToken ?? userToken.DropboxAccessToken;
            userToken.ExactAccessToken = exactToken ?? userToken.ExactAccessToken;
            userToken.ExactAccessTokenExpiration = exactTokenExpiration ?? userToken.ExactAccessTokenExpiration;

            _context.SaveChanges();
        }
        public void TryRetrieveTokenFromDatabase_KnownId_ReturnsObjectFromDatabase()
        {
            var userId = Guid.NewGuid();
            var expected = new UserToken
            {
                Id = Guid.NewGuid(),
                UserId = userId,
                DropboxAccessToken = "dropbox_token",
                ExactAccessToken = "exact_token"
            };

            var data = new List<UserToken> { expected }.AsQueryable();

            var fakeDbSet = SetupFakeDbSet(data);
            var fakeContext = SetupFakeDbContext(fakeDbSet);

            var sut = new UserTokenRepository(fakeContext);

            var result = sut.TryRetrieveTokenFromDatabase(userId);

            Assert.That(result, Is.EqualTo(expected));
        }
        public void UpdateOrCreateToken_ExistingToken_UpdatesObjectWithInput()
        {
            var id = Guid.NewGuid();
            var userId = Guid.NewGuid();
            var existing = new UserToken
            {
                Id = id,
                UserId = userId,
                DropboxAccessToken = "dropbox_token",
                ExactAccessToken = "exact_token"
            };
            var expected = new UserToken
            {
                Id = id,
                UserId = userId,
                DropboxAccessToken = "new_dropbox_token",
                ExactAccessToken = "new_exact_token"
            };

            var data = new List<UserToken> { existing }.AsQueryable();

            var fakeDbSet = SetupFakeDbSet(data);
            var fakeContext = SetupFakeDbContext(fakeDbSet);

            var sut = new UserTokenRepository(fakeContext);

            sut.UpdateOrCreateToken(userId, "new_dropbox_token", "new_exact_token");

            Assert.That(existing, Is.EqualTo(expected));
        }