public void When_add_tracks_success() { var httpWebRequest = MockedHttpWebRequest .Create() .SuccessTracksResponse() .Build(); var httpWebBuilder = new HttpWebBuilder(httpWebRequest); var deezer = new HttpDeezer(httpWebBuilder); var tracks = deezer.AddTracks(1, "accessToken", "12345", new[] { "123", "456" }); Assert.IsTrue(tracks.Success); }
public void When_add_playlist_with_null_response() { var httpWebRequest = MockedHttpWebRequest .Create() .NullResponse() .Build(); var httpWebBuilder = new HttpWebBuilder(httpWebRequest); var deezer = new HttpDeezer(httpWebBuilder); var playlist = deezer.AddPlaylist(56789, "accessToken", "MagicPlaylist"); Assert.IsNull(playlist); }
public void When_add_playlist_success() { var httpWebRequest = MockedHttpWebRequest .Create() .SuccessPlaylistResponse() .Build(); var httpWebBuilder = new HttpWebBuilder(httpWebRequest); var deezer = new HttpDeezer(httpWebBuilder); var playlist = deezer.AddPlaylist(56789, "accessToken", "MagicPlaylist"); Assert.AreEqual("12345", playlist.Id); Assert.AreEqual("https://www.deezer.com/playlist/12345", playlist.PlaylistUrl); }
public void When_add_tracks_invalid_playlist_id() { var httpWebRequest = MockedHttpWebRequest .Create() .ErrorTracksInvalidPlaylistIdResponse() .Build(); var httpWebBuilder = new HttpWebBuilder(httpWebRequest); var deezer = new HttpDeezer(httpWebBuilder); var tracks = deezer.AddTracks(1, "accessToken", "12345", new[] { "123", "456" }); Assert.IsTrue(tracks.HasError); Assert.AreEqual("DataException", tracks.Error.Type); Assert.AreEqual("no data", tracks.Error.Message); Assert.AreEqual(800, tracks.Error.Code); }
public void When_add_playlist_with_invalid_response() { var httpWebRequest = MockedHttpWebRequest .Create() .ErrorPlaylistResponse() .Build(); var httpWebBuilder = new HttpWebBuilder(httpWebRequest); var deezer = new HttpDeezer(httpWebBuilder); var playlist = deezer.AddPlaylist(56789, "accessToken", "MagicPlaylist"); Assert.IsTrue(playlist.HasError); Assert.AreEqual("ParameterException", playlist.Error.Type); Assert.AreEqual("Wrong parameter: playlistid", playlist.Error.Message); Assert.AreEqual(500, playlist.Error.Code); }
public HomeModule(IRadioGateway radioGateway, IMagicPlaylistGateway magicPlaylistGateway, HttpDeezer httpDeezer) { Get["/"] = _ => { using (var smartTimer = new SmartTimer((x, u) => ModuleLoggerInfo("Exit Home", x.Elapsed))) { ModuleLoggerInfo("Exit Home"); return View["index.html"]; } }; Get["/channel"] = _ => { using (var smartTimer = new SmartTimer((x, u) => ModuleLoggerInfo("Exit Channel", x.Elapsed))) { ModuleLoggerInfo("Enter Channel"); return View["channel.html"]; } }; Post["/playlist"] = parameters => { try { using (var smartTimer = new SmartTimer((x, u) => ModuleLoggerInfo("Exit Playlist", u, x.Elapsed))) { ModuleLoggerInfo("Enter Playlist"); // Manage user var user = this.Bind<UserModel>(); if (user == null || user.Id == 0) throw new MagicPlaylistException("UserId is null or empty"); smartTimer.SetUserId(user.Id); if (string.IsNullOrEmpty(user.AccessToken)) throw new MagicPlaylistException(user.Id, "AccessToken is null or empty"); if (!magicPlaylistGateway.CanAddPlaylist(user.Id)) throw new MagicPlaylistException(user.Id, "User can't add playlist"); magicPlaylistGateway.AddOrUpdateUser(user); // Get tracks var tracks = radioGateway.GetRandomTracks(user.Id); if (tracks == null || !tracks.Any()) throw new MagicPlaylistException(user.Id, "DbTracks is null or empty"); // Add playlist var deezerPlaylist = httpDeezer.AddPlaylist(user.Id, user.AccessToken, "MagicPlaylist"); if (deezerPlaylist == null) throw new MagicPlaylistException(user.Id, "DeezerPlaylist is null"); else if (deezerPlaylist.HasError) throw new MagicPlaylistException(user.Id, string.Format("DeezerPlaylist error:[{0}][{1}][{2}]", deezerPlaylist.Error.Type, deezerPlaylist.Error.Message, deezerPlaylist.Error.Code)); // Add tracks var deezerTracks = httpDeezer.AddTracks(user.Id, user.AccessToken, deezerPlaylist.Id, tracks); if (deezerTracks == null) throw new MagicPlaylistException(user.Id, "DeezerTracks is null"); else if (deezerTracks.HasError) throw new MagicPlaylistException(user.Id, string.Format("DeezerTracks error:[{0}][{1}][{2}]", deezerTracks.Error.Type, deezerTracks.Error.Message, deezerTracks.Error.Code)); return Success(deezerPlaylist.PlaylistUrl); } } catch (Exception ex) { logger.Error(ex); var error = new ErrorModel(ex.GetType().Name, ex.Message, ex.StackTrace); magicPlaylistGateway.AddError(error); return Fail(); } }; }
public void When_post_playlist_with_error_tracks_must_be_failed() { var httpWebRequest = MockedHttpWebRequest .Create() .SuccessPlaylistResponse() .ErrorTracksInvalidTrackIdResponse() .Build(); var httpWebBuilder = new HttpWebBuilder(httpWebRequest); var httpDeezer = new HttpDeezer(httpWebBuilder); var mockedMagicPlaylistGateway = MockedMagicPlaylistGateway .Create() .CanAddPlaylist(true); var browser = new Browser(cfg => { cfg.Module<HomeModule>(); cfg.Dependency(BuildSuccessRadioGateway()); cfg.Dependency(mockedMagicPlaylistGateway.Build()); cfg.Dependency(httpDeezer); }); var response = browser.Post("/playlist", (with) => { with.HttpRequest(); with.FormValue("id", "1"); with.FormValue("accessToken", "abcde"); with.FormValue("firstname", "Nicolas"); with.FormValue("lastname", "Delfour"); with.FormValue("email", "*****@*****.**"); with.FormValue("gender", "M"); with.FormValue("name", "Nico"); with.FormValue("country", "FR"); with.FormValue("lang", "FR"); with.FormValue("birthday", "1980-02-25"); }); var logTable = mockedMagicPlaylistGateway._logTable; var error = logTable.SingleOrDefault(); Assert.AreEqual(1, logTable.Count); Assert.IsNotNull(error); Assert.AreEqual("MagicPlaylistException", error.errorType); Assert.AreEqual("DeezerTracks error:[ParameterException][Wrong parameter: songs][500]", error.message); Assert.IsNotEmpty(error.stackTrace); Assert.AreEqual(1, mockedMagicPlaylistGateway._userTable.Count); Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode); Assert.AreEqual("application/json; charset=utf-8", response.ContentType); Assert.AreEqual("{\"success\":false}", response.Body.AsString()); }
public void When_add_tracks_invalid_tracks_id() { var httpWebRequest = MockedHttpWebRequest .Create() .ErrorTracksInvalidTrackIdResponse() .Build(); var httpWebBuilder = new HttpWebBuilder(httpWebRequest); var deezer = new HttpDeezer(httpWebBuilder); var tracks = deezer.AddTracks(1, "accessToken", "12345", new[] { "123", "456" }); Assert.IsTrue(tracks.HasError); Assert.AreEqual("ParameterException", tracks.Error.Type); Assert.AreEqual("Wrong parameter: songs", tracks.Error.Message); Assert.AreEqual(500, tracks.Error.Code); }
public void When_add_tracks_with_null_response() { var httpWebRequest = MockedHttpWebRequest .Create() .NullResponse() .Build(); var httpWebBuilder = new HttpWebBuilder(httpWebRequest); var deezer = new HttpDeezer(httpWebBuilder); var tracks = deezer.AddTracks(1, "accessToken", "12345", new[] { "123", "456" }); Assert.IsNull(tracks); }