2018-08-24 13:10:13 +01:00
|
|
|
|
using System;
|
2017-05-31 16:25:56 +01:00
|
|
|
|
using System.Collections.Generic;
|
2015-10-23 20:25:13 +01:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2018-08-24 13:10:13 +01:00
|
|
|
|
using Moq;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using SpotifyAPI.Web.Models;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
|
2018-08-24 13:10:13 +01:00
|
|
|
|
namespace SpotifyAPI.Web.Tests
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2018-04-01 17:58:26 +01:00
|
|
|
|
public class SpotifyWebAPITest
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
2018-08-24 13:10:13 +01:00
|
|
|
|
private static readonly string FixtureDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../fixtures/");
|
2016-03-05 16:02:34 +00:00
|
|
|
|
|
2015-07-07 17:11:11 +01:00
|
|
|
|
private Mock<IClient> _mock;
|
|
|
|
|
private SpotifyWebAPI _spotify;
|
|
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
|
public void SetUp()
|
|
|
|
|
{
|
|
|
|
|
_mock = new Mock<IClient>();
|
2019-03-18 20:24:09 +00:00
|
|
|
|
_spotify = new SpotifyWebAPI
|
2015-07-07 17:11:11 +01:00
|
|
|
|
{
|
|
|
|
|
WebClient = _mock.Object,
|
|
|
|
|
UseAuth = false
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static T GetFixture<T>(string file)
|
|
|
|
|
{
|
2016-03-05 16:02:34 +00:00
|
|
|
|
return JsonConvert.DeserializeObject<T>(File.ReadAllText(Path.Combine(FixtureDir, file)));
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool ContainsValues(string str, params string[] values)
|
|
|
|
|
{
|
|
|
|
|
return values.All(str.Contains);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ShouldGetPrivateProfile_WithoutAuth()
|
|
|
|
|
{
|
|
|
|
|
_spotify.UseAuth = false;
|
2016-03-05 15:24:54 +00:00
|
|
|
|
|
|
|
|
|
Assert.Throws<InvalidOperationException>(() => _spotify.GetPrivateProfile());
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ShouldGetPrivateProfile_WithAuth()
|
|
|
|
|
{
|
|
|
|
|
PrivateProfile profile = GetFixture<PrivateProfile>("private-user.json");
|
2017-05-31 16:25:56 +01:00
|
|
|
|
_mock.Setup(client => client.DownloadJson<PrivateProfile>(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()))
|
2016-07-07 20:38:07 +01:00
|
|
|
|
.Returns(new Tuple<ResponseInfo, PrivateProfile>(ResponseInfo.Empty, profile));
|
2015-07-07 17:11:11 +01:00
|
|
|
|
|
|
|
|
|
_spotify.UseAuth = true;
|
|
|
|
|
Assert.AreEqual(profile, _spotify.GetPrivateProfile());
|
2017-05-31 16:25:56 +01:00
|
|
|
|
_mock.Verify(client => client.DownloadJson<PrivateProfile>(
|
|
|
|
|
It.Is<string>(str => ContainsValues(str, "/me")),
|
|
|
|
|
It.IsNotNull<Dictionary<string, string>>()), Times.Exactly(1));
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void ShouldGetPublicProfile()
|
|
|
|
|
{
|
|
|
|
|
PublicProfile profile = GetFixture<PublicProfile>("public-user.json");
|
2017-05-31 16:25:56 +01:00
|
|
|
|
_mock.Setup(client => client.DownloadJson<PublicProfile>(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()))
|
2016-07-07 20:38:07 +01:00
|
|
|
|
.Returns(new Tuple<ResponseInfo, PublicProfile>(ResponseInfo.Empty, profile));
|
2015-07-07 17:11:11 +01:00
|
|
|
|
|
2017-05-31 16:25:56 +01:00
|
|
|
|
|
|
|
|
|
_spotify.UseAuth = false;
|
2015-07-07 17:11:11 +01:00
|
|
|
|
Assert.AreEqual(profile, _spotify.GetPublicProfile("wizzler"));
|
2017-05-31 16:25:56 +01:00
|
|
|
|
_mock.Verify(client => client.DownloadJson<PublicProfile>(
|
|
|
|
|
It.Is<string>(str => ContainsValues(str, "/users/wizzler")),
|
|
|
|
|
It.Is<Dictionary<string, string>>(headers => headers.Count == 0)), Times.Exactly(1));
|
2015-07-07 17:11:11 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Will add more tests once I decided if this is worth the effort (propably not?)
|
|
|
|
|
}
|
2015-10-23 20:25:13 +01:00
|
|
|
|
}
|