PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Server/DeviceHive.Test/ApiTest/DeviceCommandTest.cs

https://github.com/oryol/devicehive-.net
C# | 156 lines | 126 code | 26 blank | 4 comment | 0 complexity | 2ee92ba3fb3a1f378212771f4d5a74ad MD5 | raw file
Possible License(s): MIT
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json.Linq;
  7. using NUnit.Framework;
  8. namespace DeviceHive.Test.ApiTest
  9. {
  10. [TestFixture]
  11. public class DeviceCommandTest : ResourceTest
  12. {
  13. private static readonly string DeviceGUID = "a97266f4-6e8a-4008-8242-022b49ea484f";
  14. private int? NetworkID { get; set; }
  15. private int? DeviceClassID { get; set; }
  16. public DeviceCommandTest()
  17. : base("/device/" + DeviceGUID + "/command")
  18. {
  19. }
  20. protected override void OnCreateDependencies()
  21. {
  22. var networkResponse = Client.Post("/network", new { name = "_ut_n" }, auth: Admin);
  23. Assert.That(networkResponse.Status, Is.EqualTo(ExpectedCreatedStatus));
  24. NetworkID = (int)networkResponse.Json["id"];
  25. RegisterForDeletion("/network/" + NetworkID);
  26. var deviceClassResponse = Client.Post("/device/class", new { name = "_ut_dc", version = "1" }, auth: Admin);
  27. Assert.That(deviceClassResponse.Status, Is.EqualTo(ExpectedCreatedStatus));
  28. DeviceClassID = (int)deviceClassResponse.Json["id"];
  29. RegisterForDeletion("/device/class/" + DeviceClassID);
  30. var deviceResponse = Client.Put("/device/" + DeviceGUID, new { key = "key", name = "_ut_dc", network = NetworkID, deviceClass = DeviceClassID });
  31. Assert.That(deviceResponse.Status, Is.EqualTo(200));
  32. RegisterForDeletion("/device/" + DeviceGUID);
  33. }
  34. [Test]
  35. public void GetAll()
  36. {
  37. Get(auth: Device(DeviceGUID, "key"));
  38. }
  39. [Test]
  40. public void GetAll_Filter()
  41. {
  42. var resource = Create(new { command = "_ut" }, auth: Admin);
  43. Expect(Get(new Dictionary<string, string> { { "start", DateTime.UtcNow.AddHours(-1).ToString("yyyy-MM-ddTHH:mm:ss.ffffff") } }, auth: Device(DeviceGUID, "key")).Count, Is.GreaterThan(0));
  44. Expect(Get(new Dictionary<string, string> { { "start", DateTime.UtcNow.AddHours(1).ToString("yyyy-MM-ddTHH:mm:ss.ffffff") } }, auth: Device(DeviceGUID, "key")).Count, Is.EqualTo(0));
  45. Expect(Get(new Dictionary<string, string> { { "end", DateTime.UtcNow.AddHours(-1).ToString("yyyy-MM-ddTHH:mm:ss.ffffff") } }, auth: Device(DeviceGUID, "key")).Count, Is.EqualTo(0));
  46. Expect(Get(new Dictionary<string, string> { { "end", DateTime.UtcNow.AddHours(1).ToString("yyyy-MM-ddTHH:mm:ss.ffffff") } }, auth: Device(DeviceGUID, "key")).Count, Is.GreaterThan(0));
  47. }
  48. [Test]
  49. public void Get_Client()
  50. {
  51. var user1 = CreateUser(1);
  52. var user2 = CreateUser(1, NetworkID);
  53. var resource = Create(new { command = "_ut" }, auth: Admin);
  54. Expect(() => Get(resource, auth: user1), FailsWith(404)); // should fail with 404
  55. Get(resource, auth: user2); // should succeed
  56. }
  57. [Test]
  58. public void Poll()
  59. {
  60. // task to poll new resources
  61. var poll = new Task(() =>
  62. {
  63. var response = Client.Get(ResourceUri + "/poll", auth: Device(DeviceGUID, "key"));
  64. Expect(response.Status, Is.EqualTo(200));
  65. Expect(response.Json, Is.InstanceOf<JArray>());
  66. var result = (JArray)response.Json;
  67. Expect(result.Count, Is.EqualTo(1));
  68. Expect(result[0], Matches(new { command = "_ut2" }));
  69. });
  70. // create resource, start poll, wait, then create another resource
  71. var resource1 = Create(new { command = "_ut1" }, auth: Admin);
  72. poll.Start();
  73. Thread.Sleep(100);
  74. var resource2 = Create(new { command = "_ut2" }, auth: Admin);
  75. Expect(poll.Wait(2000), Is.True); // task should complete
  76. }
  77. [Test]
  78. public void Create()
  79. {
  80. var resource = Create(new { command = "_ut" }, auth: Admin);
  81. Expect(resource, Matches(new { command = "_ut", parameters = (string)null, status = (string)null, result = (string)null, timestamp = ResponseMatchesContraint.Timestamp }));
  82. Expect(Get(resource, auth: Admin), Matches(new { command = "_ut", parameters = (string)null, status = (string)null, result = (string)null, timestamp = ResponseMatchesContraint.Timestamp }));
  83. }
  84. [Test]
  85. public void Update()
  86. {
  87. var resource = Create(new { command = "_ut" }, auth: Admin);
  88. var update = Update(resource, new { command = "_ut2", parameters = new { a = "b" }, status = "OK", result = "Success" }, auth: Device(DeviceGUID, "key"));
  89. Expect(update, Matches(new { command = "_ut2", parameters = new { a = "b" }, status = "OK", result = "Success", timestamp = ResponseMatchesContraint.Timestamp }));
  90. Expect(Get(resource, auth: Admin), Matches(new { command = "_ut2", parameters = new { a = "b" }, status = "OK", result = "Success", timestamp = ResponseMatchesContraint.Timestamp }));
  91. }
  92. [Test]
  93. public void Update_Partial()
  94. {
  95. var resource = Create(new { command = "_ut", parameters = new { a = "b" } }, auth: Admin);
  96. var update = Update(resource, new { parameters = new { a = "b2" } }, auth: Device(DeviceGUID, "key"));
  97. Expect(update, Matches(new { command = "_ut", parameters = new { a = "b2" }, timestamp = ResponseMatchesContraint.Timestamp }));
  98. Expect(Get(resource, auth: Admin), Matches(new { command = "_ut", parameters = new { a = "b2" }, timestamp = ResponseMatchesContraint.Timestamp }));
  99. }
  100. [Test]
  101. public void Delete()
  102. {
  103. var resource = Create(new { command = "_ut" }, auth: Admin);
  104. Expect(() => { Delete(resource, auth: Admin); return false; }, FailsWith(405));
  105. }
  106. [Test]
  107. public void BadRequest()
  108. {
  109. Expect(() => Create(new { command2 = "_ut" }, auth: Admin), FailsWith(400));
  110. }
  111. [Test]
  112. public void Unauthorized()
  113. {
  114. // no authorization
  115. Expect(() => Get(), FailsWith(401));
  116. Expect(() => Get(UnexistingResourceID), FailsWith(401));
  117. Expect(() => Create(new { command = "_ut" }), FailsWith(401));
  118. Expect(() => Update(UnexistingResourceID, new { command = "_ut" }), FailsWith(401));
  119. // user authorization
  120. var user = CreateUser(1, NetworkID);
  121. Expect(() => Update(UnexistingResourceID, new { notification = "_ut" }, auth: user), FailsWith(401));
  122. }
  123. [Test]
  124. public void NotFound()
  125. {
  126. Expect(() => Get(UnexistingResourceID, auth: Admin), FailsWith(404));
  127. Expect(() => Update(UnexistingResourceID, new { command = "_ut" }, auth: Admin), FailsWith(404));
  128. }
  129. }
  130. }