OLD | NEW |
(Empty) | |
| 1 /* |
| 2 Copyright 2013 Google Inc |
| 3 |
| 4 Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 you may not use this file except in compliance with the License. |
| 6 You may obtain a copy of the License at |
| 7 |
| 8 http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
| 10 Unless required by applicable law or agreed to in writing, software |
| 11 distributed under the License is distributed on an "AS IS" BASIS, |
| 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 See the License for the specific language governing permissions and |
| 14 limitations under the License. |
| 15 */ |
| 16 |
| 17 using System; |
| 18 using System.Collections.Generic; |
| 19 using System.Linq; |
| 20 using System.Text; |
| 21 |
| 22 using NUnit.Framework; |
| 23 using Newtonsoft.Json; |
| 24 |
| 25 using Google.Apis.Tests; |
| 26 |
| 27 namespace Google.Apis.Auth.OAuth2.Responses |
| 28 { |
| 29 /// <summary> |
| 30 /// Tests for <seealso cref="Google.Apis.Auth.OAuth2.Responses.TokenResponse
"/>. |
| 31 /// </summary> |
| 32 [TestFixture] |
| 33 public class TokenResponseTests |
| 34 { |
| 35 [Test] |
| 36 public void TestConstructor() |
| 37 { |
| 38 var response = new TokenResponse(); |
| 39 Assert.Null(response.AccessToken); |
| 40 Assert.Null(response.ExpiresInSeconds); |
| 41 Assert.Null(response.RefreshToken); |
| 42 Assert.Null(response.Scope); |
| 43 Assert.AreEqual(DateTime.MinValue, response.Issued); |
| 44 } |
| 45 |
| 46 [Test] |
| 47 public void TestSerializer() |
| 48 { |
| 49 var tokenResponse = JsonConvert.DeserializeObject<TokenResponse>( |
| 50 @"{ |
| 51 'access_token': '123', |
| 52 'expires_in': 1000, |
| 53 'refresh_token': '456', |
| 54 'scope': '789' |
| 55 }"); |
| 56 Assert.That(tokenResponse.AccessToken, Is.EqualTo("123")); |
| 57 Assert.That(tokenResponse.RefreshToken, Is.EqualTo("456")); |
| 58 Assert.That(tokenResponse.Scope, Is.EqualTo("789")); |
| 59 Assert.That(tokenResponse.ExpiresInSeconds, Is.EqualTo(1000)); |
| 60 } |
| 61 |
| 62 [Test] |
| 63 public void IsExpired_True() |
| 64 { |
| 65 var issued = DateTime.Now; |
| 66 var newNow = DateTime.Now.AddSeconds(200); |
| 67 |
| 68 var mockClock = new MockClock() |
| 69 { |
| 70 Now = newNow |
| 71 }; |
| 72 |
| 73 // Issued not set. |
| 74 var response = new TokenResponse(); |
| 75 Assert.True(response.IsExpired(mockClock)); |
| 76 |
| 77 // ExpiresInSeconds is not set. |
| 78 response = new TokenResponse() { Issued = issued }; |
| 79 Assert.True(response.IsExpired(mockClock)); |
| 80 |
| 81 response = new TokenResponse() { ExpiresInSeconds = 1, Issued = issu
ed }; |
| 82 Assert.True(response.IsExpired(mockClock)); |
| 83 |
| 84 response = new TokenResponse() { ExpiresInSeconds = 100, Issued = is
sued }; |
| 85 Assert.True(response.IsExpired(mockClock)); |
| 86 |
| 87 response = new TokenResponse() { ExpiresInSeconds = 140, Issued = is
sued }; |
| 88 Assert.True(response.IsExpired(mockClock)); |
| 89 } |
| 90 |
| 91 [Test] |
| 92 public void IsExpired_False() |
| 93 { |
| 94 var issued = DateTime.Now; |
| 95 var newNow = DateTime.Now.AddSeconds(200); |
| 96 |
| 97 var mockClock = new MockClock() |
| 98 { |
| 99 Now = newNow |
| 100 }; |
| 101 |
| 102 var response = new TokenResponse() { AccessToken = "a", ExpiresInSec
onds = 141, Issued = issued }; |
| 103 Assert.False(response.IsExpired(mockClock)); |
| 104 |
| 105 response = new TokenResponse() { AccessToken = "a", ExpiresInSeconds
= 142, Issued = issued }; |
| 106 Assert.False(response.IsExpired(mockClock)); |
| 107 } |
| 108 } |
| 109 } |
OLD | NEW |