Ejemplo n.º 1
0
            public static void AddPlaylistSongsAsync_InvalidTrackId_ThrowsDataNotFoundError()
            {
                var dbConnection = OpenSqliteDatabase();

                var dbContextOptionsBuilder = new DbContextOptionsBuilder <MediaInfoContext>()
                                              .DisableClientSideEvaluation()
                                              .UseSqlite(dbConnection);

                using (var dbContext = new MediaInfoContext(dbContextOptionsBuilder.Options))
                {
                    var random    = new RandomPopulator(dbContext);
                    var user      = random.AddUser();
                    var library   = random.AddLibrary();
                    var artist    = random.AddArtist();
                    var album     = random.AddAlbum(artist);
                    var directory = random.AddDirectory(library);
                    var file      = random.AddFile(directory);
                    var track     = random.AddTrack(file, artist, album);
                    var playlist  = random.AddPlaylist(user);
                    dbContext.SaveChanges();

                    int[] trackIds = new[] { track.TrackId, track.TrackId + 1 };
                    var   ex       = Assert.Throws <RestApiErrorException>(() => RestApiQueries.AddPlaylistSongsAsync(dbContext, user.UserId, playlist.PlaylistId, trackIds, CancellationToken.None).GetAwaiter().GetResult());

                    var expectedException = RestApiErrorException.DataNotFoundError();
                    Assert.Equal(expectedException.Message, ex.Message);
                    Assert.Equal(expectedException.Code, ex.Code);
                }
            }
            public static void GetAlbumList2OrderedByAlbumTitleAsync_LibraryDoesNotExist_ThrowsDataNotFoundError()
            {
                var dbConnection = OpenSqliteDatabase();

                var dbContextOptionsBuilder = new DbContextOptionsBuilder <MediaInfoContext>()
                                              .DisableClientSideEvaluation()
                                              .UseSqlite(dbConnection);

                using (var dbContext = new MediaInfoContext(dbContextOptionsBuilder.Options))
                {
                    var random    = new RandomPopulator(dbContext);
                    var user      = random.AddUser();
                    var library   = random.AddLibrary();
                    var artist    = random.AddArtist();
                    var album     = random.AddAlbum(artist);
                    var directory = random.AddDirectory(library);
                    var file      = random.AddFile(directory);
                    var track     = random.AddTrack(file, artist, album);
                    dbContext.SaveChanges();

                    var ex = Assert.Throws <RestApiErrorException>(() => RestApiQueries.GetAlbumList2OrderedByAlbumTitleAsync(dbContext, user.UserId, library.LibraryId + 1, 0, 10, CancellationToken.None).GetAwaiter().GetResult());

                    var expectedException = RestApiErrorException.DataNotFoundError();
                    Assert.Equal(expectedException.Message, ex.Message);
                    Assert.Equal(expectedException.Code, ex.Code);
                }
            }
            public static void GetAlbumAsync_NoAccessibleTrack_ThrowsDataNotFoundError()
            {
                var dbConnection = OpenSqliteDatabase();

                var dbContextOptionsBuilder = new DbContextOptionsBuilder <MediaInfoContext>()
                                              .DisableClientSideEvaluation()
                                              .UseSqlite(dbConnection);

                using (var dbContext = new MediaInfoContext(dbContextOptionsBuilder.Options))
                {
                    var random    = new RandomPopulator(dbContext);
                    var user      = random.AddUser();
                    var library   = random.AddLibrary(accessControlled: true);
                    var artist    = random.AddArtist();
                    var album     = random.AddAlbum(artist);
                    var directory = random.AddDirectory(library);
                    var file      = random.AddFile(directory);
                    var track     = random.AddTrack(file, artist, album);
                    dbContext.SaveChanges();

                    string transcodedSuffix = "mp3";
                    var    ex = Assert.Throws <RestApiErrorException>(() => RestApiQueries.GetAlbumAsync(dbContext, user.UserId, album.AlbumId, transcodedSuffix, CancellationToken.None).GetAwaiter().GetResult());

                    var expectedException = RestApiErrorException.DataNotFoundError();
                    Assert.Equal(expectedException.Message, ex.Message);
                    Assert.Equal(expectedException.Code, ex.Code);
                }
            }
Ejemplo n.º 4
0
        public void Constructing_from_ErrorData_correctly_sets_properties_when_there_is_no_body()
        {
            ErrorData err = ErrorData.From((HttpStatusCode)480, "wat", null);

            var exception = new RestApiErrorException(err);

            Assert.AreEqual((HttpStatusCode)480, exception.StatusCode);
            Assert.AreEqual("wat", exception.ReasonPhrase);
            Assert.IsNull(exception.ErrorCode);
            Assert.IsNull(exception.RawErrorDetails);
        }
Ejemplo n.º 5
0
        public void Constructing_from_ErrorData_correctly_sets_properties_when_there_are_no_errorDetails()
        {
            ErrorData err = ErrorData.From((HttpStatusCode)480, "wat", @"{""errorCode"":""ServerCaughtFire""}");

            var exception = new RestApiErrorException(err);

            Assert.AreEqual((HttpStatusCode)480, exception.StatusCode);
            Assert.AreEqual("wat", exception.ReasonPhrase);
            Assert.AreEqual("ServerCaughtFire", exception.ErrorCode);
            Assert.IsNull(exception.RawErrorDetails);
        }
Ejemplo n.º 6
0
        public void Constructing_from_ErrorData_correctly_sets_properties()
        {
            ErrorData err = ErrorData.From((HttpStatusCode)480, "wat", @"{""errorCode"":""ServerCaughtFire"",""errorDetails"":{""in"":""body"",""at"":""foo""}}");

            var exception = new RestApiErrorException(err);

            Assert.AreEqual((HttpStatusCode)480, exception.StatusCode);
            Assert.AreEqual("wat", exception.ReasonPhrase);
            Assert.AreEqual("ServerCaughtFire", exception.ErrorCode);
            Assert.AreEqual(JValue.Parse(@"{""in"":""body"",""at"":""foo""}").ToString(Formatting.Indented), exception.RawErrorDetails);
        }
Ejemplo n.º 7
0
        public void When_constructing_with_null_message_and_ErrorData_the_ErrorData_DefaultExceptionMessage_is_used_as_the_message()
        {
            var exception = new RestApiErrorException(
                null,
                new ErrorData(
                    "Default exception message",
                    (HttpStatusCode)480,
                    "Reason Phrase",
                    @"{""errorCode"":""wat"",""errorDetails"":{""firstName"":""Bob""},""results"":[{""errorCode"":""innerProblem"",""errorDetails"":{""more"":""something bad""}}]}",
                    "wat",
                    @"{""firstName"":""Bob""}",
                    new List <InnerErrorData> {
                new InnerErrorData("someProblem", @"{""more"":""something bad""}")
            }));

            Assert.AreEqual(exception.Message, "Default exception message");
            Assert.AreEqual(exception.StatusCode, (HttpStatusCode)480);
            Assert.AreEqual(exception.ReasonPhrase, "Reason Phrase");
            Assert.AreEqual(exception.ErrorCode, "wat");
            Assert.AreEqual(exception.RawErrorDetails, @"{""firstName"":""Bob""}");
        }
            public static void DeletePlaylistAsync_PlaylistDoesNotExist_ThrowsDataNotFoundError(bool canDeleteAllPublicPlaylists)
            {
                var dbConnection = OpenSqliteDatabase();

                var dbContextOptionsBuilder = new DbContextOptionsBuilder <MediaInfoContext>()
                                              .DisableClientSideEvaluation()
                                              .UseSqlite(dbConnection);

                using (var dbContext = new MediaInfoContext(dbContextOptionsBuilder.Options))
                {
                    var random   = new RandomPopulator(dbContext);
                    var user     = random.AddUser();
                    var playlist = random.AddPlaylist(user);
                    dbContext.SaveChanges();

                    var ex = Assert.Throws <RestApiErrorException>(() => RestApiQueries.DeletePlaylistAsync(dbContext, user.UserId, canDeleteAllPublicPlaylists, playlist.PlaylistId + 1, CancellationToken.None).GetAwaiter().GetResult());

                    var expectedException = RestApiErrorException.DataNotFoundError();
                    Assert.Equal(expectedException.Message, ex.Message);
                    Assert.Equal(expectedException.Code, ex.Code);
                }
            }
            public static void GetCoverArtStreamInfoAsync_PictureHasInaccessibleFile_ThrowsDataNotFoundError()
            {
                var dbConnection = OpenSqliteDatabase();

                var dbContextOptionsBuilder = new DbContextOptionsBuilder <MediaInfoContext>()
                                              .DisableClientSideEvaluation()
                                              .UseSqlite(dbConnection);

                using (var dbContext = new MediaInfoContext(dbContextOptionsBuilder.Options))
                {
                    var random    = new RandomPopulator(dbContext);
                    var user      = random.AddUser();
                    var library   = random.AddLibrary(accessControlled: true);
                    var directory = random.AddDirectory(library);
                    var file      = random.AddFile(directory);
                    var picture   = random.AddPicture(file);
                    dbContext.SaveChanges();

                    var ex = Assert.Throws <RestApiErrorException>(() => RestApiQueries.GetCoverArtStreamInfoAsync(dbContext, user.UserId, picture.StreamHash, CancellationToken.None).GetAwaiter().GetResult());
                    Assert.Equal(RestApiErrorException.DataNotFoundError().Message, ex.Message);
                }
            }
            public static void CreateUserAsync_UsernameAlreadyExists_ThrowsGenericError()
            {
                var dbConnection = OpenSqliteDatabase();

                var dbContextOptionsBuilder = new DbContextOptionsBuilder <MediaInfoContext>()
                                              .DisableClientSideEvaluation()
                                              .UseSqlite(dbConnection);

                using (var dbContext = new MediaInfoContext(dbContextOptionsBuilder.Options))
                {
                    var random = new RandomPopulator(dbContext);
                    var user   = random.AddUser();
                    dbContext.SaveChanges();

                    var ex = Assert.Throws <RestApiErrorException>(() =>
                    {
                        return(RestApiQueries.CreateUserAsync(dbContext, user.Name, "password", false, false, false, CancellationToken.None).GetAwaiter().GetResult());
                    });

                    var expectedException = RestApiErrorException.GenericError("User already exists.");
                    Assert.Equal(expectedException.Message, ex.Message);
                    Assert.Equal(expectedException.Code, ex.Code);
                }
            }
            public static void DeletePlaylistAsync_PublicPlaylistOwnedByOtherUserAndNotCanDeleteAllPublicPlaylists_ThrowsUserNotAuthorizedError()
            {
                var dbConnection = OpenSqliteDatabase();

                var dbContextOptionsBuilder = new DbContextOptionsBuilder <MediaInfoContext>()
                                              .DisableClientSideEvaluation()
                                              .UseSqlite(dbConnection);

                using (var dbContext = new MediaInfoContext(dbContextOptionsBuilder.Options))
                {
                    var random   = new RandomPopulator(dbContext);
                    var user1    = random.AddUser();
                    var user2    = random.AddUser();
                    var playlist = random.AddPlaylist(user1, @public: true);
                    dbContext.SaveChanges();

                    bool canDeleteAllPublicPlaylists = false;
                    var  ex = Assert.Throws <RestApiErrorException>(() => RestApiQueries.DeletePlaylistAsync(dbContext, user2.UserId, canDeleteAllPublicPlaylists, playlist.PlaylistId, CancellationToken.None).GetAwaiter().GetResult());

                    var expectedException = RestApiErrorException.UserNotAuthorizedError();
                    Assert.Equal(expectedException.Message, ex.Message);
                    Assert.Equal(expectedException.Code, ex.Code);
                }
            }
Ejemplo n.º 12
0
 public void Can_construct_with_message_and_inner_exception()
 {
     _ = new RestApiErrorException("Some message", new InvalidOperationException());
 }
Ejemplo n.º 13
0
 public void Can_construct_without_any_arguments()
 {
     _ = new RestApiErrorException();
 }