data_object_storage_registry.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #![cfg(test)]
  2. use super::mock::*;
  3. #[test]
  4. fn initial_state() {
  5. with_default_mock_builder(|| {
  6. assert_eq!(
  7. TestDataObjectStorageRegistry::first_relationship_id(),
  8. TEST_FIRST_RELATIONSHIP_ID
  9. );
  10. });
  11. }
  12. #[test]
  13. fn add_relationship_fails_with_invalid_authorization() {
  14. with_default_mock_builder(|| {
  15. let (account_id, storage_provider_id) = (2, 2);
  16. // The content needs to exist - in our mock, that's with the content ID TEST_MOCK_EXISTING_CID
  17. let res = TestDataObjectStorageRegistry::add_relationship(
  18. Origin::signed(account_id),
  19. storage_provider_id,
  20. TEST_MOCK_EXISTING_CID,
  21. );
  22. assert_eq!(res, Err(working_group::Error::<Test, crate::StorageWorkingGroupInstance>::WorkerDoesNotExist.into()));
  23. });
  24. }
  25. #[test]
  26. fn set_relationship_ready_fails_with_invalid_authorization() {
  27. with_default_mock_builder(|| {
  28. let (account_id, storage_provider_id) = hire_storage_provider();
  29. // The content needs to exist - in our mock, that's with the content ID TEST_MOCK_EXISTING_CID
  30. let res = TestDataObjectStorageRegistry::add_relationship(
  31. Origin::signed(account_id),
  32. storage_provider_id,
  33. TEST_MOCK_EXISTING_CID,
  34. );
  35. assert!(res.is_ok());
  36. let (invalid_account_id, invalid_storage_provider_id) = (2, 2);
  37. let res = TestDataObjectStorageRegistry::set_relationship_ready(
  38. Origin::signed(invalid_account_id),
  39. invalid_storage_provider_id,
  40. TEST_MOCK_EXISTING_CID,
  41. );
  42. assert_eq!(res, Err(working_group::Error::<Test, crate::StorageWorkingGroupInstance>::WorkerDoesNotExist.into()));
  43. });
  44. }
  45. #[test]
  46. fn unset_relationship_ready_fails_with_invalid_authorization() {
  47. with_default_mock_builder(|| {
  48. let (account_id, storage_provider_id) = hire_storage_provider();
  49. // The content needs to exist - in our mock, that's with the content ID TEST_MOCK_EXISTING_CID
  50. let res = TestDataObjectStorageRegistry::add_relationship(
  51. Origin::signed(account_id),
  52. storage_provider_id,
  53. TEST_MOCK_EXISTING_CID,
  54. );
  55. assert!(res.is_ok());
  56. let (invalid_account_id, invalid_storage_provider_id) = (2, 2);
  57. let res = TestDataObjectStorageRegistry::unset_relationship_ready(
  58. Origin::signed(invalid_account_id),
  59. invalid_storage_provider_id,
  60. TEST_MOCK_EXISTING_CID,
  61. );
  62. assert_eq!(res, Err(working_group::Error::<Test, crate::StorageWorkingGroupInstance>::WorkerDoesNotExist.into()));
  63. });
  64. }
  65. #[test]
  66. fn test_add_relationship() {
  67. with_default_mock_builder(|| {
  68. let (account_id, storage_provider_id) = hire_storage_provider();
  69. // The content needs to exist - in our mock, that's with the content ID TEST_MOCK_EXISTING_CID
  70. let res = TestDataObjectStorageRegistry::add_relationship(
  71. Origin::signed(account_id),
  72. storage_provider_id,
  73. TEST_MOCK_EXISTING_CID,
  74. );
  75. assert_eq!(res, Ok(()));
  76. });
  77. }
  78. #[test]
  79. fn test_fail_adding_relationship_with_bad_content() {
  80. with_default_mock_builder(|| {
  81. let (account_id, storage_provider_id) = hire_storage_provider();
  82. let res = TestDataObjectStorageRegistry::add_relationship(
  83. Origin::signed(account_id),
  84. storage_provider_id,
  85. 24,
  86. );
  87. assert!(res.is_err());
  88. });
  89. }
  90. #[test]
  91. fn test_toggle_ready() {
  92. with_default_mock_builder(|| {
  93. /*
  94. Events are not emitted on block 0.
  95. So any dispatchable calls made during genesis block formation will have no events emitted.
  96. https://substrate.dev/recipes/2-appetizers/4-events.html
  97. */
  98. run_to_block(1);
  99. let (account_id, storage_provider_id) = hire_storage_provider();
  100. // Create a DOSR
  101. let res = TestDataObjectStorageRegistry::add_relationship(
  102. Origin::signed(account_id),
  103. storage_provider_id,
  104. TEST_MOCK_EXISTING_CID,
  105. );
  106. assert!(res.is_ok());
  107. // Grab DOSR ID from event
  108. let dosr_id = match System::events().last().unwrap().event {
  109. MetaEvent::data_object_storage_registry(
  110. data_object_storage_registry::RawEvent::DataObjectStorageRelationshipAdded(
  111. dosr_id,
  112. _content_id,
  113. _account_id,
  114. ),
  115. ) => dosr_id,
  116. _ => 0xdeadbeefu64, // invalid value, unlikely to match
  117. };
  118. assert_ne!(dosr_id, 0xdeadbeefu64);
  119. // Toggling from a different account should fail
  120. let res = TestDataObjectStorageRegistry::set_relationship_ready(
  121. Origin::signed(2),
  122. storage_provider_id,
  123. dosr_id,
  124. );
  125. assert!(res.is_err());
  126. // Toggling with the wrong ID should fail.
  127. let res = TestDataObjectStorageRegistry::set_relationship_ready(
  128. Origin::signed(account_id),
  129. storage_provider_id,
  130. dosr_id + 1,
  131. );
  132. assert!(res.is_err());
  133. // Toggling with the correct ID and origin should succeed
  134. let res = TestDataObjectStorageRegistry::set_relationship_ready(
  135. Origin::signed(account_id),
  136. storage_provider_id,
  137. dosr_id,
  138. );
  139. assert!(res.is_ok());
  140. assert_eq!(
  141. System::events().last().unwrap().event,
  142. MetaEvent::data_object_storage_registry(
  143. data_object_storage_registry::RawEvent::DataObjectStorageRelationshipReadyUpdated(
  144. dosr_id, true,
  145. )
  146. )
  147. );
  148. });
  149. }