Exemple #1
0
        public async Task GetAccessToken_CacheNotNullAndItemDoesExist_CacheAddNotCalled()
        {
            // Arrange
            var mockCache = new Mock <ICache>();

            mockCache.Setup(c => c.Get("Radiostr.SpotifyWebApi.ClientCredentialsAuthorizationApi.BearerToken"))
            .Returns("jkl901");

            var mockHttp = new Mock <IHttpClient>();

            mockHttp.Setup(h => h.Post(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <AuthenticationHeaderValue>()))
            .ReturnsAsync("{\"access_token\":\"ghi678\", \"expires_in\":3600}");

            var settings = new NameValueCollection
            {
                { "SpotifyApiClientId", "abc123" },
                { "SpotifyApiClientSecret", "def345" }
            };

            var auth = new ClientCredentialsAuthorizationApi(mockHttp.Object, settings, mockCache.Object);

            // Act
            await auth.GetAccessToken();

            // Assert
            mockCache.Verify(m => m.Add(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <DateTime>()), Times.Never());
        }
Exemple #2
0
        public async Task GetAccessToken_CacheItemDoesNotExist_CacheItemExpiresBeforeOrAtSameTimeAsTokenExpires()
        {
            // Arrange
            var tokenExpires = DateTime.Now;
            var cacheExpires = DateTime.Now;
            var mockCache    = new Mock <ICache>();

            mockCache.Setup(c => c.Get("Radiostr.SpotifyWebApi.ClientCredentialsAuthorizationApi.BearerToken"))
            .Returns("jkl901");
            mockCache.Setup(c => c.Add(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <DateTime>()))
            .Callback((string k, object v, DateTime e) => cacheExpires = e);

            var mockHttp = new Mock <IHttpClient>();

            mockHttp.Setup(h => h.Post(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <AuthenticationHeaderValue>()))
            .ReturnsAsync("{\"access_token\":\"ghi678\", \"expires_in\":3600}")
            .Callback(() => tokenExpires = DateTime.Now.AddSeconds(3600));

            var settings = new NameValueCollection
            {
                { "SpotifyApiClientId", "abc123" },
                { "SpotifyApiClientSecret", "def345" }
            };

            var auth = new ClientCredentialsAuthorizationApi(mockHttp.Object, settings, mockCache.Object);

            // Act
            await auth.GetAccessToken();

            // Assert
            Assert.IsTrue(cacheExpires <= tokenExpires);
        }
Exemple #3
0
 public SpotifyService(IConfiguration configuration)
 {
     _configuration = configuration;
     _httpClient    = new HttpClient();
     _auth          = new ClientCredentialsAuthorizationApi(_httpClient, configuration);
     _api           = new ArtistsApi(_httpClient, _auth);
 }
Exemple #4
0
        private static async Task AsyncMain(string[] args)
        {
            var http = new HttpClient();
            var auth = new ClientCredentialsAuthorizationApi(http);
            var api  = new ArtistsApi(http, auth);

            // Get an artist by Spotify Artist Id
            dynamic response = await api.GetArtist("1tpXaFf2F55E7kVJON4j4G");

            Console.WriteLine(response);
        }
Exemple #5
0
        static async Task <string> Thing()
        {
            var http     = new RestHttpClient(new System.Net.Http.HttpClient());
            var memCache = new RuntimeMemoryCache(System.Runtime.Caching.MemoryCache.Default);
            var clientCredentialsAuthApi = new ClientCredentialsAuthorizationApi(http, System.Configuration.ConfigurationManager.AppSettings, memCache);
            var api = new PlaylistsApi(http, clientCredentialsAuthApi);

            var playlists = await api.GetPlaylists("davemateer");

            Trace.TraceInformation(playlists.ToString());
            return("xx");
        }
Exemple #6
0
        public async Task GetAccessToken_CacheItemDoesNotExist_PostResponseAdded()
        {
            // Arrange
            var mockCache = new Mock <ICache>();

            var mockHttp = new Mock <IHttpClient>();

            mockHttp.Setup(h => h.Post(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <AuthenticationHeaderValue>()))
            .ReturnsAsync("{\"access_token\":\"ghi678\", \"expires_in\":3600}");

            var settings = new NameValueCollection
            {
                { "SpotifyApiClientId", "abc123" },
                { "SpotifyApiClientSecret", "def345" }
            };

            var auth = new ClientCredentialsAuthorizationApi(mockHttp.Object, settings, mockCache.Object);

            // Act
            string token = await auth.GetAccessToken();

            // Assert
            Assert.AreEqual("ghi678", token);
        }