Ejemplo n.º 1
0
        /// <summary>
        /// Tags a certain playlist as "liked" by a certain user and adds it to his/her Favorites collection
        /// </summary>
        /// <param name="playlistId">ID of the playlist</param>
        /// <param name="userId">ID of the user</param>
        /// <returns>True of successful, otherwise false</returns>
        public async Task <bool> AddPlaylistToFavoritesAsync(int playlistId, int userId)
        {
            var result = await this.context.Favorites
                         .FirstOrDefaultAsync(fav => fav.PlaylistId == playlistId &&
                                              fav.UserId == userId);

            if (result != null)
            {
                if (result.IsFavorite == true)
                {
                    return(false);
                }
                else
                {
                    result.IsFavorite = true;
                    await context.SaveChangesAsync();

                    return(true);
                }
            }

            PlaylistFavorite item = new PlaylistFavorite
            {
                PlaylistId = playlistId,
                IsFavorite = true,
                UserId     = userId
            };

            await context.Favorites.AddAsync(item);

            await context.SaveChangesAsync();

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Changes the LockoutEnabled status to true and LockoutEnd to the desired time of a user entry in the database
        /// </summary>
        /// <param name="id">The ID of the user to search for</param>
        /// <returns>If successful, returns true else returns false</returns>
        public async Task <bool> BanUserById(int id)
        {
            var user = await context.Users.FirstOrDefaultAsync(x => x.Id == id);

            if (user == null)
            {
                return(false);
            }
            user.LockoutEnabled = true;
            user.LockoutEnd     = DateTime.Now.AddYears(1);

            await context.SaveChangesAsync();

            return(true);
        }
        public async Task SetDeletedStatus_Currectly()
        {
            //Arrange
            var options = Utils.GetOptions(nameof(SetDeletedStatus_Currectly));

            User user = new User
            {
                Id       = 1,
                UserName = "******",
                Email    = "*****@*****.**"
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Users.AddAsync(user);

                await arrangeContext.SaveChangesAsync();

                var  sut       = new AdminService(arrangeContext);
                bool delResult = await sut.DeleteUser(1);
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var userFromDb = await assertContext.Users.FirstOrDefaultAsync(u => u.Id == user.Id);

                Assert.IsTrue(userFromDb.IsDeleted);
            }
        }
Ejemplo n.º 4
0
        public async Task SetLockoutStatus_ToFalseCurrectly()
        {
            //Arrange
            var options = Utils.GetOptions(nameof(SetLockoutStatus_ToFalseCurrectly));

            User user = new User
            {
                Id             = 1,
                UserName       = "******",
                Email          = "*****@*****.**",
                LockoutEnabled = true
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Users.AddAsync(user);

                await arrangeContext.SaveChangesAsync();

                var  sut         = new AdminService(arrangeContext);
                bool unbanResult = await sut.UnbanUserById(1);
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var userFromDb = await assertContext.Users.FirstOrDefaultAsync(u => u.Id == user.Id);

                Assert.IsFalse(userFromDb.LockoutEnabled);
            }
        }
        public async Task ReturnUserByHisId_Correctly()
        {
            //Arrange
            var options = Utils.GetOptions(nameof(ReturnUserByHisId_Correctly));

            User user = new User
            {
                Id       = 1,
                UserName = "******",
                Email    = "*****@*****.**"
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Users.AddAsync(user);

                await arrangeContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var sut        = new AdminService(assertContext);
                var userResult = await sut.GetUserById(1);

                Assert.AreEqual(userResult.UserName, user.UserName);
                Assert.AreEqual(userResult.Email, user.Email);
            }
        }
Ejemplo n.º 6
0
        public async Task ReturnTopThreePlaylists_Correctly()
        {
            //Arrange
            var options = Utils.GetOptions(nameof(ReturnTopThreePlaylists_Correctly));
            var plMock  = new Mock <IPlaylistService>();

            var playlists = new List <Playlist>()
            {
                new Playlist()
                {
                    Id    = 1,
                    Title = "Travel music",
                    Rank  = 222
                },
                new Playlist()
                {
                    Id    = 2,
                    Title = "Road Jam",
                    Rank  = 333
                },
                new Playlist()
                {
                    Id    = 3,
                    Title = "Riding Dirty",
                    Rank  = 444
                },
                new Playlist()
                {
                    Id    = 4,
                    Title = "Heavy metal",
                    Rank  = 555
                }
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Playlists.AddRangeAsync(playlists);

                await arrangeContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var sut    = new StatisticsService(assertContext, plMock.Object);
                var result = await sut.TopPlaylists();

                Assert.IsTrue(result.Count == 3);
                Assert.IsTrue(result.FirstOrDefault(r => r.Title == "Travel Music") == null);
            }
        }
Ejemplo n.º 7
0
        public async Task ReturnThreeArthistsFromDB_Correctly()
        {
            //Arrange
            var options = Utils.GetOptions(nameof(ReturnThreeArthistsFromDB_Correctly));
            var plMock  = new Mock <IPlaylistService>();

            var artists = new List <Artist>()
            {
                new Artist()
                {
                    Id               = 1,
                    ArtistName       = "The weeknd",
                    ArtistPictureURL = "/images/artist.png"
                },
                new Artist()
                {
                    Id               = 2,
                    ArtistName       = "Bad Wolfs",
                    ArtistPictureURL = "/images/artist.png"
                },
                new Artist()
                {
                    Id               = 3,
                    ArtistName       = "Shakira",
                    ArtistPictureURL = "/images/artist.png"
                },
                new Artist()
                {
                    Id               = 4,
                    ArtistName       = "G-easy",
                    ArtistPictureURL = "/images/artist.png"
                }
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Artists.AddRangeAsync(artists);

                await arrangeContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var sut    = new StatisticsService(assertContext, plMock.Object);
                var result = await sut.FeaturedArtists();

                Assert.IsTrue(result.Count == 3);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Downloads tracks from the external Deezer API according to a specified music genre
        /// and deserializes the downloaded data to local objects: tracks, artists, albums and music genres.
        /// </summary>
        /// <param name="incomingGenre">The music genre of the tracks to download</param>
        /// <returns>Void</returns>
        public async Task DownloadTrackData(string incomingGenre)
        {
            string startUrl = $"https://api.deezer.com/search/playlist?q={incomingGenre}";

            var response = await client.GetAsync(startUrl);

            var jsonString = await response.Content.ReadAsStringAsync();

            var deezerPlaylistCollection = JsonSerializer.Deserialize <DeezerPlaylistCollection>(jsonString);

            foreach (var item in deezerPlaylistCollection.DeezerPlaylists)
            {
                await TraverseTracklist(item.TracklistUrl, incomingGenre);
            }

            await context.SaveChangesAsync();
        }
Ejemplo n.º 9
0
        public async Task ReturnUserCollection_MatchingTheEmailString()
        {
            //Arrange
            var options     = Utils.GetOptions(nameof(ReturnUserCollection_MatchingTheEmailString));
            int expected    = 2;
            var userEntries = new List <User>()
            {
                new User()
                {
                    Id       = 1,
                    UserName = "******",
                    Email    = "*****@*****.**",
                },
                new User()
                {
                    Id       = 2,
                    UserName = "******",
                    Email    = "*****@*****.**",
                },
                new User()
                {
                    Id       = 3,
                    UserName = "******",
                    Email    = "*****@*****.**",
                }
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Users.AddRangeAsync(userEntries);

                await arrangeContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var sut        = new AdminService(assertContext);
                var userResult = await sut.SearchByEmail("*****@*****.**");

                Assert.AreEqual(userResult.Count, expected);
            }
        }
        public async Task EditUserOn_OnlyPasswordSubmited()
        {
            //Arrange
            var options = Utils.GetOptions(nameof(EditUserOn_OnlyPasswordSubmited));
            var hasher  = new PasswordHasher <User>();

            User user = new User()
            {
                Id       = 2,
                UserName = "******",
                Email    = "*****@*****.**",
            };

            user.PasswordHash = hasher.HashPassword(user, "unbreakablepass");

            UserDTO inputDTO = new UserDTO()
            {
                Id       = 2,
                Password = "******"
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Users.AddAsync(user);

                await arrangeContext.SaveChangesAsync();

                var sut     = new AdminService(arrangeContext);
                var editDTO = await sut.EditUser(inputDTO);
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var sut    = new AdminService(assertContext);
                var result = assertContext.Users.FirstOrDefaultAsync(u => u.UserName == inputDTO.UserName);

                var expected = hasher.VerifyHashedPassword(user, user.PasswordHash, inputDTO.Password);

                Assert.IsTrue(expected == PasswordVerificationResult.Success);
            }
        }
        public async Task ReturnUserCountOfDb_Correctly()
        {
            //Arrange
            var options = Utils.GetOptions(nameof(ReturnUserCountOfDb_Correctly));
            var plMock  = new Mock <IPlaylistService>();

            var users = new List <User>()
            {
                new User()
                {
                    Id       = 1,
                    UserName = "******",
                    Email    = "*****@*****.**"
                },
                new User()
                {
                    Id       = 2,
                    UserName = "******",
                    Email    = "*****@*****.**"
                }
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Users.AddRangeAsync(users);

                await arrangeContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var sut    = new StatisticsService(assertContext, plMock.Object);
                int result = await sut.UserCount();

                Assert.IsTrue(result == 2);
            }
        }
        public async Task EditUserOn_OnlyEmailSubmited()
        {
            //Arrange
            var  options = Utils.GetOptions(nameof(EditUserOn_OnlyEmailSubmited));
            User user    = new User()
            {
                Id       = 2,
                LastName = "Topchiev",
                UserName = "******",
                Email    = "*****@*****.**"
            };

            UserDTO inputDTO = new UserDTO()
            {
                Id    = 2,
                Email = "*****@*****.**"
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Users.AddAsync(user);

                await arrangeContext.SaveChangesAsync();

                var sut     = new AdminService(arrangeContext);
                var editDTO = await sut.EditUser(inputDTO);
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var sut    = new AdminService(assertContext);
                var result = assertContext.Users.FirstOrDefaultAsync(u => u.Email == inputDTO.Email);

                Assert.IsNotNull(result);
            }
        }
        public async Task ReturnTrackCountOfDb_Correctly()
        {
            //Arrange
            var options = Utils.GetOptions(nameof(ReturnTrackCountOfDb_Correctly));
            var plMock  = new Mock <IPlaylistService>();

            var tracks = new List <Track>()
            {
                new Track()
                {
                    Id         = 1,
                    TrackTitle = "The Hills"
                },
                new Track()
                {
                    Id         = 2,
                    TrackTitle = "Starboy"
                }
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Tracks.AddRangeAsync(tracks);

                await arrangeContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var sut    = new StatisticsService(assertContext, plMock.Object);
                int result = await sut.TrackCount();

                Assert.IsTrue(result == 2);
            }
        }
Ejemplo n.º 14
0
        public async Task GetAllPlaylistsWithDeletedStatus()
        {
            //Arrange
            var options   = Utils.GetOptions(nameof(GetAllPlaylistsWithDeletedStatus));
            var playlists = new List <Playlist>()
            {
                new Playlist()
                {
                    Id        = 1,
                    Title     = "Good Times",
                    IsDeleted = true
                },
                new Playlist()
                {
                    Id        = 2,
                    Title     = "Rock Hard",
                    IsDeleted = true
                }
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Playlists.AddRangeAsync(playlists);

                await arrangeContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var sut    = new AdminService(assertContext);
                var result = await sut.GetDeletedPlaylists();

                Assert.IsTrue(result.Count == 2);
            }
        }
Ejemplo n.º 15
0
        public async Task ReturnArtistCountOfDb_Correctly()
        {
            //Arrange
            var options = Utils.GetOptions(nameof(ReturnArtistCountOfDb_Correctly));
            var plMock  = new Mock <IPlaylistService>();

            var artists = new List <Artist>()
            {
                new Artist()
                {
                    Id         = 1,
                    ArtistName = "The weeknd"
                },
                new Artist()
                {
                    Id         = 2,
                    ArtistName = "Bad Wolfs"
                }
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Artists.AddRangeAsync(artists);

                await arrangeContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var sut    = new StatisticsService(assertContext, plMock.Object);
                int result = await sut.ArtistCount();

                Assert.IsTrue(result == 2);
            }
        }
Ejemplo n.º 16
0
        public async Task ReturnAllUsers_Correctly()
        {
            //Arrange
            var options     = Utils.GetOptions(nameof(ReturnAllUsers_Correctly));
            var userEntries = new List <User>()
            {
                new User()
                {
                    Id       = 1,
                    UserName = "******",
                    Email    = "*****@*****.**"
                },
                new User()
                {
                    Id       = 2,
                    UserName = "******",
                    Email    = "*****@*****.**"
                }
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Users.AddRangeAsync(userEntries);

                await arrangeContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var sut        = new AdminService(assertContext);
                var userResult = await sut.GetAllUsers();

                Assert.AreEqual(userResult.Count, userEntries.Count);
            }
        }
        public async Task ReturnPlaylistCountOfDb_Correctly()
        {
            //Arrange
            var options = Utils.GetOptions(nameof(ReturnPlaylistCountOfDb_Correctly));
            var plMock  = new Mock <IPlaylistService>();

            var playlists = new List <Playlist>()
            {
                new Playlist()
                {
                    Id    = 1,
                    Title = "Travel music"
                },
                new Playlist()
                {
                    Id    = 2,
                    Title = "Road Jam"
                }
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Playlists.AddRangeAsync(playlists);

                await arrangeContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var sut    = new StatisticsService(assertContext, plMock.Object);
                int result = await sut.PlaylistCount();

                Assert.IsTrue(result == 2);
            }
        }
Ejemplo n.º 18
0
        public async Task ReturnGenreCountOfDb_Correctly()
        {
            //Arrange
            var options = Utils.GetOptions(nameof(ReturnGenreCountOfDb_Correctly));
            var plMock  = new Mock <IPlaylistService>();

            var genres = new List <Genre>()
            {
                new Genre()
                {
                    Id   = 1,
                    Name = "Metal"
                },
                new Genre()
                {
                    Id   = 2,
                    Name = "Rock"
                }
            };

            //Act
            using (var arrangeContext = new RidePalDbContext(options))
            {
                await arrangeContext.Genres.AddRangeAsync(genres);

                await arrangeContext.SaveChangesAsync();
            }

            //Assert
            using (var assertContext = new RidePalDbContext(options))
            {
                var sut    = new StatisticsService(assertContext, plMock.Object);
                int result = await sut.GenreCount();

                Assert.IsTrue(result == 2);
            }
        }