Represents the connection to the particle cloud.
Inheritance: ParticleBase, IDisposable
Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ParticleDevice"/> class.
 /// </summary>
 /// <param name="cloud">The cloud.</param>
 /// <param name="obj">The JSon object to parse</param>
 /// <exception cref="System.ArgumentNullException"></exception>
 internal protected ParticleDevice(ParticleCloud cloud, JObject obj)
 {
     if (cloud == null)
     {
         throw new ArgumentNullException(nameof(cloud));
     }
     this.cloud = cloud;
     ParseObject(obj);
 }
		public async Task AuthenticationAsyncHttpRequestExceptionTest()
		{
			using (var cloud = new ParticleCloud(new Uri("http://particletest.io")))
			{
				var result = await cloud.LoginWithUserAsync("test", "test");
				Assert.IsNotNull(result);
				Assert.IsFalse(result.Success);
				Assert.AreEqual("An error occurred while sending the request.", result.Error);
			}
		}
		public async Task AuthenticationTestAsync()
		{
			using (var cloud = new ParticleCloud())
			{
				var exc = Assert.Throws<ArgumentNullException>(() => { cloud.LoginWithUserAsync(null, "sadf").GetAwaiter().GetResult(); });
				Assert.AreEqual("username", exc.ParamName);
				exc = Assert.Throws<ArgumentNullException>(() => { cloud.LoginWithUserAsync("test", null).GetAwaiter().GetResult(); });
				Assert.AreEqual("password", exc.ParamName);

				var results = await cloud.LoginWithUserAsync("*****@*****.**", "test123");
				Assert.IsFalse(results.Success, "User some how authenticated?");
				Assert.AreEqual("User credentials are invalid", results.ErrorDescription);

				results = await cloud.LoginWithUserAsync(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"]);
				Assert.IsTrue(results.Success, "User did not authenticate");
			}
		}
		public async Task MakeGetRequestAsyncTest()
		{
			using (var cloud = new ParticleCloud())
			{
				var exc = Assert.Throws<ArgumentNullException>(() => { cloud.MakeGetRequestAsync(null).GetAwaiter().GetResult(); });
				Assert.AreEqual("method", exc.ParamName);
				Assert.Throws<ParticleAuthenticationExeption>(() => { cloud.MakeGetRequestAsync("devices").GetAwaiter().GetResult(); });
				var stats = await cloud.LoginWithUserAsync(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"]);
				Assert.IsTrue(stats.Success, "User did not authenticate");

				var results = await cloud.MakeGetRequestAsync("devices");
				Assert.AreEqual(HttpStatusCode.OK, results.StatusCode);
				var jrep = results.Response;
				Assert.AreEqual(JTokenType.Array, jrep.Type);
				JArray arr = (JArray)jrep;
				foreach (var obj in arr)
				{
					Assert.AreEqual(JTokenType.Object, obj.Type);
				}

			}
		}
		public void LastHeardDateTest()
		{
			using (var cloud = new ParticleCloud())
			{
				var expected = DateTime.Now;
				var p = new ParticleDeviceMock(cloud, JObject.Parse($"{{'id':'3', 'name': 'test','last_heard': '{expected.ToUniversalTime():o}'}}"));

				Assert.AreEqual(expected, p.LastHeard);
			}
		}
		public ParticleDeviceMock(ParticleCloud cloud, JObject obj) : base(cloud, obj)
		{

		}
		/// <summary>
		/// Initializes a new instance of the <see cref="ParticleDevice"/> class.
		/// </summary>
		/// <param name="cloud">The cloud.</param>
		/// <param name="obj">The JSon object to parse</param>
		/// <exception cref="System.ArgumentNullException"></exception>
		internal protected ParticleDevice(ParticleCloud cloud, JObject obj)
		{
			if (cloud == null)
			{
				throw new ArgumentNullException(nameof(cloud));
			}
			this.cloud = cloud;
			ParseObject(obj);
		}
		public async Task RefreshToken_Test()
		{
			using (var cloud = new ParticleCloud())
			{
				var status = await cloud.LoginWithUserAsync(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"], 1);
				Assert.IsTrue(status.Success);
				await Task.Delay(2000);

				var req = await cloud.MakeGetRequestAsync("devices");
				Assert.AreEqual(HttpStatusCode.Unauthorized, req.StatusCode);

				status = await cloud.RefreshTokenAsync();
				Assert.IsTrue(status.Success);
			}
		}
 protected internal DesignParticleDevice(ParticleCloud cloud, JObject obj)
     : base(cloud, obj)
 {
 }