PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/oryol/devicehive-.net
C# | 142 lines | 114 code | 24 blank | 4 comment | 0 complexity | 52469b79b6367717b1c3a6ff18465c5b 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 DeviceNotificationTest : 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 DeviceNotificationTest()
  17. : base("/device/" + DeviceGUID + "/notification")
  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: Admin);
  38. }
  39. [Test]
  40. public void GetAll_Filter()
  41. {
  42. var resource = Create(new { notification = "_ut" }, auth: Device(DeviceGUID, "key"));
  43. Expect(Get(new Dictionary<string, string> { { "start", DateTime.UtcNow.AddHours(-1).ToString("yyyy-MM-ddTHH:mm:ss.ffffff") } }, auth: Admin).Count, Is.GreaterThan(0));
  44. Expect(Get(new Dictionary<string, string> { { "start", DateTime.UtcNow.AddHours(1).ToString("yyyy-MM-ddTHH:mm:ss.ffffff") } }, auth: Admin).Count, Is.EqualTo(0));
  45. Expect(Get(new Dictionary<string, string> { { "end", DateTime.UtcNow.AddHours(-1).ToString("yyyy-MM-ddTHH:mm:ss.ffffff") } }, auth: Admin).Count, Is.EqualTo(0));
  46. Expect(Get(new Dictionary<string, string> { { "end", DateTime.UtcNow.AddHours(1).ToString("yyyy-MM-ddTHH:mm:ss.ffffff") } }, auth: Admin).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 { notification = "_ut" }, auth: Device(DeviceGUID, "key"));
  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: Admin);
  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 { notification = "_ut2" }));
  69. });
  70. // create resource, start poll, wait, then create another resource
  71. var resource1 = Create(new { notification = "_ut1" }, auth: Device(DeviceGUID, "key"));
  72. poll.Start();
  73. Thread.Sleep(100);
  74. var resource2 = Create(new { notification = "_ut2" }, auth: Device(DeviceGUID, "key"));
  75. Expect(poll.Wait(2000), Is.True); // task should complete
  76. }
  77. [Test]
  78. public void Create()
  79. {
  80. var resource = Create(new { notification = "_ut" }, auth: Device(DeviceGUID, "key"));
  81. Expect(resource, Matches(new { notification = "_ut", parameters = (string)null, timestamp = ResponseMatchesContraint.Timestamp }));
  82. Expect(Get(resource, auth: Admin), Matches(new { notification = "_ut", parameters = (string)null, timestamp = ResponseMatchesContraint.Timestamp }));
  83. }
  84. [Test]
  85. public void Update()
  86. {
  87. var resource = Create(new { notification = "_ut" }, auth: Device(DeviceGUID, "key"));
  88. Expect(() => Update(resource, new { notification = "_ut2", parameters = new { a = "b" } }, auth: Admin), FailsWith(405));
  89. }
  90. [Test]
  91. public void Delete()
  92. {
  93. var resource = Create(new { notification = "_ut" }, auth: Device(DeviceGUID, "key"));
  94. Expect(() => { Delete(resource, auth: Admin); return false; }, FailsWith(405));
  95. }
  96. [Test]
  97. public void BadRequest()
  98. {
  99. Expect(() => Create(new { notification2 = "_ut" }, auth: Device(DeviceGUID, "key")), FailsWith(400));
  100. }
  101. [Test]
  102. public void Unauthorized()
  103. {
  104. // no authorization
  105. Expect(() => Get(), FailsWith(401));
  106. Expect(() => Get(UnexistingResourceID), FailsWith(401));
  107. Expect(() => Create(new { notification = "_ut" }), FailsWith(401));
  108. // user authorization
  109. var user = CreateUser(1, NetworkID);
  110. Expect(() => Create(new { notification = "_ut" }, auth: user), FailsWith(401));
  111. }
  112. [Test]
  113. public void NotFound()
  114. {
  115. Expect(() => Get(UnexistingResourceID, auth: Admin), FailsWith(404));
  116. }
  117. }
  118. }