public async void Create_InvalidModel(PrivateSongBM model) { MyUser user = fixture.Authenticate_User("Miguel"); //ACT string responseContentString = await Create_Act(model, HttpStatusCode.BadRequest); //ASSERT: Correct Response Object Assert.Equal("", responseContentString); }
public async void Create_ValidModel_Unauthorized() { //ARRANGE: Set a valid model PrivateSongBM model = new PrivateSongBM("MyUnauthorized", "MyArtistName", "MyAlbumName", "aP6orw0M-bY", "00:00:05", "00:03:56"); //ACT string responseContentString = await Create_Act(model, HttpStatusCode.Unauthorized); //ASSERT: Correct Response Object Assert.Equal("", responseContentString); }
public async Task <IActionResult> Edit(string privateSongIdString, [FromBody] PrivateSongBM privateSongModel) { //validate the new info if (!ModelState.IsValid) { return(BadRequest()); } //validate provided private song id as a valid GUID id if (privateSongIdString == null || !Guid.TryParse(privateSongIdString, out Guid privateSongId)) { return(NotFound()); } MyUser user = _context.Users.Include(x => x.PrivateSongs).ThenInclude(x => x.Song).ThenInclude(s => s.Video).FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name); if (user == null) { return(NotFound()); } //try to find private song in authenticated user private songs //(do not search outside current user, as any user is only allowed to change his own songs) PrivateSong privateSong = user.PrivateSongs.FirstOrDefault(x => x.PrivateSongId == privateSongId); if (privateSong == null) { return(NotFound()); } //Edit every field, every field must be provided even if unchanged privateSong.ArtistName = privateSongModel.ArtistName; privateSong.AlbumName = privateSongModel.AlbumName; privateSong.Name = privateSongModel.Name; privateSong.Song.Video.VideoUrl = privateSongModel.VideoUrl; privateSong.Song.Video.StartSec = Video.GetTimeInSeconds(privateSongModel.StartAt); privateSong.Song.Video.EndSec = Video.GetTimeInSeconds(privateSongModel.EndAt); privateSong.Song.Video.Duration = Video.GetDuration(privateSong.Song.Video.StartSec, privateSong.Song.Video.EndSec); if (privateSong.Song.Video.Duration == null) { return(BadRequest()); //invalid video duration } await _context.SaveChangesAsync(); PrivateSongBasicVM privateSongVM = new PrivateSongBasicVM(privateSong); return(Ok(privateSongVM)); }
public async void Edit_Valid_Unauthorized() { //ARRANGE: Get a valid and existing privateSongId string privateSongId = MySqlHelpers.GetRandomUserPrivateSongId("Anabela"); //ARRANGE: Set a privateSong model with the new info for the song PrivateSongBM model = ValidModelForEdit; //ACT: string responseContentString = await Edit_Act(privateSongId, model, HttpStatusCode.Unauthorized); //ASSERT: Correct response object Assert.Equal("", responseContentString); }
public async void Edit_InvalidModel(PrivateSongBM model) { //ARRANGE: authenticate user MyUser user = fixture.Authenticate_User("Anabela"); //ARRANGE: Get a valid and existing privateSongId string privateSongId = MySqlHelpers.GetRandomUserPrivateSongId(user.UserName); //ACT: string responseContentString = await Edit_Act(privateSongId, model, HttpStatusCode.BadRequest); //ASSERT: Correct response object Assert.Equal("", responseContentString); }
public async void Edit_InvalidGuid_NotFound(string privateSongId) { //ARRANGE: authenticate user MyUser user = fixture.Authenticate_User("Anabela"); //ARRANGE: Set a privateSong model with the new info for the song PrivateSongBM model = ValidModelForEdit; //ACT: string responseContentString = await Edit_Act(privateSongId, model, HttpStatusCode.NotFound); //ASSERT: Correct response object Assert.Equal("", responseContentString); }
/// <summary> /// Auxiliary method that sets request content, executes request, verifies response's http status code for /// request to '/api/musicasprivadas/editar' [Action: 'Edit' , Controller: 'PrivateSongController'] /// </summary> /// <param name="privateSongId">PrivateSongId of the song to be edited</param> /// <param name="model">model with new info to be sent as content in POST request</param> /// <param name="expectedHttpCode">expected response's http status code </param> /// <returns>Response's content as JSON string</returns> public async Task <string> Edit_Act(string privateSongId, PrivateSongBM model, HttpStatusCode expectedHttpCode) { //ARRANGE: POST request path and content string requestPath = $"/api/musicasprivadas/editar/{privateSongId}"; string jsonContent = JsonConvert.SerializeObject(model); StringContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); //ACT: var response = await fixture._client.PostAsync(requestPath, content); //ASSERT: Correct Http Status Code Assert.Equal(expectedHttpCode, response.StatusCode); return(await response.Content.ReadAsStringAsync()); }
/// <summary> /// List of Valid Models of PrivateSongBM for the creation of new private songs. /// These models should not raise any error in controller. /// </summary> public static IEnumerable <object[]> PrivateSong_ListOfValidModels() { PrivateSongBM model; //Valid Model: basic model = new PrivateSongBM("MySongName", "MyArtistName", "MyAlbumName", "aP6orw0M-bY", "00:00:05", "00:03:56"); yield return(new object[] { model }); //Valid Model: album name is null model = new PrivateSongBM("MySongName", "MyArtistName", null, "aP6orw0M-bY", "00:00:05", "00:03:56"); yield return(new object[] { model }); //Valid Model: video duration longer than 1 hour model = new PrivateSongBM("MySongName", "MyArtistName", "MyAlbumName", "aP6orw0M-bY", "00:00:02", "01:03:56"); yield return(new object[] { model }); }
public async void GetInfo_Authenticated() { //ARRANGE: authenticate user MyUser user = fixture.Authenticate_User("Miguel"); //ARRANGE: Get a valid and existing privateSongId string privateSongId = MySqlHelpers.GetRandomUserPrivateSongId(user.UserName); //ACT: string responseContentString = await GetInfo_Act(privateSongId, HttpStatusCode.OK); //ASSERT: Response object matches database state PrivateSongBM responseObject = JsonConvert.DeserializeObject <PrivateSongBM>(responseContentString); Assert.True(MySqlHelpers.CheckIfPrivateSongBmMatchesDb(privateSongId, responseObject)); }
public async void Edit_Valid() { //ARRANGE: authenticate user MyUser user = fixture.Authenticate_User("Anabela"); //ARRANGE: Get a valid and existing privateSongId string privateSongId = MySqlHelpers.GetRandomUserPrivateSongId(user.UserName); //ARRANGE: Set a privateSong model with the new info for the song PrivateSongBM model = ValidModelForEdit; //ACT: string responseContentString = await Edit_Act(privateSongId, model, HttpStatusCode.OK); //ASSERT: Correct response object PrivateSongBasicVM responseObject = JsonConvert.DeserializeObject <PrivateSongBasicVM>(responseContentString); Assert.Equal(model.Name, responseObject.Name); Assert.Equal(privateSongId, responseObject.PrivateSongId); Assert.True(Guid.TryParse(responseObject.SongId, out Guid auxSongId)); //ASSERT: Correct database state Song expected = new Song(); expected.SongId = auxSongId; expected.PrivateSong = new PrivateSong(); expected.PrivateSong.PrivateSongId = Guid.Parse(privateSongId); expected.PrivateSong.Name = model.Name; expected.PrivateSong.ArtistName = model.ArtistName; expected.PrivateSong.AlbumName = model.AlbumName; expected.PrivateSong.MyUser = user; expected.Video = new Video(); expected.Video.VideoUrl = model.VideoUrl; expected.Video.StartSec = Video.GetTimeInSeconds(model.StartAt); expected.Video.EndSec = Video.GetTimeInSeconds(model.EndAt); expected.Video.Duration = Video.GetDuration(expected.Video.StartSec, expected.Video.EndSec); Assert.True(MySqlHelpers.CheckIfPrivateSongWasCreated(expected, true)); }
public IActionResult GetInfo(string privateSongIdString) { // Validate string id as GUID id if (privateSongIdString == null || !Guid.TryParse(privateSongIdString, out Guid privateSongId)) { return(NotFound()); } MyUser user = _context.Users .Include(x => x.PrivateSongs) .ThenInclude(x => x.Song) .ThenInclude(s => s.Video) .FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name); if (user == null) { return(NotFound()); } PrivateSong pvs = user.PrivateSongs.FirstOrDefault(x => x.PrivateSongId == privateSongId); if (pvs == null) { return(NotFound()); //private song id not found in authenticated user } //Build object with all info about the requested private song PrivateSongBM PrivateSongInfo = new PrivateSongBM(); PrivateSongInfo.ArtistName = pvs.ArtistName; PrivateSongInfo.AlbumName = pvs.AlbumName; PrivateSongInfo.Name = pvs.Name; PrivateSongInfo.VideoUrl = pvs.Song.Video.VideoUrl; PrivateSongInfo.StartAt = Video.GetTimeInString(pvs.Song.Video.StartSec); PrivateSongInfo.EndAt = Video.GetTimeInString(pvs.Song.Video.EndSec);; return(Ok(PrivateSongInfo)); }
public async void Create_ValidModel(PrivateSongBM model) { //ARRANGE: authenticate user MyUser user = fixture.Authenticate_User("Miguel"); //ACT string responseContentString = await Create_Act(model, HttpStatusCode.OK); //ASSERT: Correct Response Object PrivateSongBasicVM responseObject = JsonConvert.DeserializeObject <PrivateSongBasicVM>(responseContentString); Assert.Equal(model.Name, responseObject.Name); Assert.True(Guid.TryParse(responseObject.PrivateSongId, out Guid auxPrivateSongId)); Assert.True(Guid.TryParse(responseObject.SongId, out Guid auxSongId)); //ASSERT: Correct Database State Song expected = new Song(); expected.SongId = auxSongId; expected.PrivateSong = new PrivateSong(); expected.PrivateSong.PrivateSongId = auxPrivateSongId; expected.PrivateSong.Name = model.Name; expected.PrivateSong.ArtistName = model.ArtistName; expected.PrivateSong.AlbumName = model.AlbumName; expected.PrivateSong.MyUser = user; expected.Video = new Video(); expected.Video.VideoUrl = model.VideoUrl; expected.Video.StartSec = Video.GetTimeInSeconds(model.StartAt); expected.Video.EndSec = Video.GetTimeInSeconds(model.EndAt); expected.Video.Duration = Video.GetDuration(expected.Video.StartSec, expected.Video.EndSec); Assert.True(MySqlHelpers.CheckIfPrivateSongWasCreated(expected)); }
internal static bool CheckIfPrivateSongBmMatchesDb(string requestPrivateSongId, PrivateSongBM responseObject) { return(CompareResultWithDB <PrivateSongBM>($"execute GetPrivateSongBM '{requestPrivateSongId}'", responseObject)); }
public async Task <IActionResult> Create([FromBody] PrivateSongBM privateSongModel) { //automatically validate all input with model binding if (!ModelState.IsValid) { return(BadRequest()); } //get authenticated user MyUser user = _context.Users.FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name); if (user == null) { return(NotFound()); } _context.Entry(user).Reference(x => x.PrivateSongPlaylist).Load(); // Create private song object: PrivateSong pvs = new PrivateSong(); pvs.PrivateSongId = Guid.NewGuid(); pvs.MyUser = user; pvs.ArtistName = privateSongModel.ArtistName; pvs.AlbumName = privateSongModel.AlbumName; pvs.Name = privateSongModel.Name; Video v = new Video(); v.VideoId = Guid.NewGuid(); v.VideoUrl = privateSongModel.VideoUrl; v.IsLive = false; //not implemented yet, default to false v.StartSec = Video.GetTimeInSeconds(privateSongModel.StartAt); v.EndSec = Video.GetTimeInSeconds(privateSongModel.EndAt); v.Duration = Video.GetDuration(v.StartSec, v.EndSec); if (v.Duration == null) { return(BadRequest()); //invalid video duration } Song song = new Song(); song.SongId = Guid.NewGuid(); song.PrivateSong = pvs; song.PrivateSongId = pvs.PrivateSongId; v.Song = song; song.Video = v; //Add private song to private song's playlist: Playable p = _context.Playables.FirstOrDefault(y => y.PlaylistId == user.PrivateSongPlaylist.PlaylistId); PlayableSong ps = new PlayableSong(); ps.PlayableId = p.PlayableId; ps.Playable = p; ps.SongId = song.SongId; ps.Song = song; ps.Position = _context.Entry(p).Collection(x => x.PlayableSongs).Query().Count(); //Add to data context and save: _context.Songs.Add(song); _context.Videos.Add(v); _context.PrivateSongs.Add(pvs); _context.PlayableSongs.Add(ps); await _context.SaveChangesAsync(); PrivateSongBasicVM privateSongVM = new PrivateSongBasicVM(pvs); return(Ok(privateSongVM)); }
/// <summary> /// List of Invalid Models of PrivateSongBM for the creation of new private songs. /// These models should should not result in the creation of a private song. /// Instead, they should result in a a BadRequest response. /// </summary> public static IEnumerable <object[]> PrivateSong_ListOfInvalidModels() { PrivateSongBM model = new PrivateSongBM(); model.Name = "MyPrivateSong"; model.ArtistName = "TestArtist"; model.AlbumName = "album1"; model.VideoUrl = "aP6orw0M-bY"; model.StartAt = "00:00:05"; model.EndAt = "00:03:56"; //Name: string aux = model.Name; model.Name = null; yield return(new object[] { model.GetCopy() }); //null model.Name = ""; yield return(new object[] { model.GetCopy() }); //empty model.Name = "sdafsjfdhasdudfhuhwquheruwhewu sdfsakfuw dsjfnuwqh djfna fwuqdsjfuiqhweu sddjfn hqfsdfqliuhjsdafh"; yield return(new object[] { model.GetCopy() }); //tooLong model.Name = aux; //ArtistName: aux = model.ArtistName; model.ArtistName = null; yield return(new object[] { model.GetCopy() }); //null model.ArtistName = ""; yield return(new object[] { model.GetCopy() }); //empty model.ArtistName = "sdafsjfdhasdudfhuhwquheruwhewu sdfsakfuw dsjfnuwqh djfna fwuqdsjfuiqhweu sddjfn hqfsdfqliuhjsdafh"; yield return(new object[] { model.GetCopy() }); //tooLong model.ArtistName = aux; //AlbumName: aux = model.AlbumName; model.AlbumName = "sdafsjfdhasdudfhuhwquheruwhewu sdfsakfuw dsjfnuwqh djfna fwuqdsjfuiqhweu sddjfn hqfsdfqliuhjsdafh"; yield return(new object[] { model.GetCopy() }); //tooLong model.AlbumName = aux; //VideoUrl aux = model.VideoUrl; model.VideoUrl = null; yield return(new object[] { model.GetCopy() }); //null model.VideoUrl = ""; yield return(new object[] { model.GetCopy() }); //empty model.VideoUrl = "sdafsjfdhasdudfhuhwquheruwhewu"; yield return(new object[] { model.GetCopy() }); //tooLong model.VideoUrl = aux; //StartAt aux = model.StartAt; model.StartAt = null; yield return(new object[] { model.GetCopy() }); //null model.StartAt = ""; yield return(new object[] { model.GetCopy() }); //empty model.StartAt = "0:0:5"; yield return(new object[] { model.GetCopy() }); //wrong model.StartAt = aux; //EndAt aux = model.EndAt; model.EndAt = null; yield return(new object[] { model.GetCopy() }); //null model.EndAt = ""; yield return(new object[] { model.GetCopy() }); //empty model.EndAt = "0:0:5"; yield return(new object[] { model.GetCopy() }); //wrong model.EndAt = aux; //Incorrect Duration - zero seconds model.StartAt = "00:03:23"; model.EndAt = "00:03:23"; yield return(new object[] { model.GetCopy() }); //Incorrect Duration - negative seconds model.StartAt = "00:03:23"; model.EndAt = "00:00:00"; yield return(new object[] { model.GetCopy() }); }