api_access_test.rb 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class ApiAccessTest < Minitest::Test
  4. def test_write_is_the_same_access_as_read_write_on_the_same_resource
  5. read_write_orders = ShopifyAPI::ApiAccess.new(%w(read_orders write_orders))
  6. write_orders = ShopifyAPI::ApiAccess.new(%w(write_orders))
  7. assert_equal write_orders, read_write_orders
  8. end
  9. def test_write_is_the_same_access_as_read_write_on_the_same_unauthenticated_resource
  10. unauthenticated_read_write_orders = ShopifyAPI::ApiAccess.new(%w(unauthenticated_read_orders unauthenticated_write_orders))
  11. unauthenticated_write_orders = ShopifyAPI::ApiAccess.new(%w(unauthenticated_write_orders))
  12. assert_equal unauthenticated_write_orders, unauthenticated_read_write_orders
  13. end
  14. def test_read_is_not_the_same_as_read_write_on_the_same_resource
  15. read_orders = ShopifyAPI::ApiAccess.new(%w(read_orders))
  16. read_write_orders = ShopifyAPI::ApiAccess.new(%w(write_orders read_orders))
  17. refute_equal read_write_orders, read_orders
  18. end
  19. def test_two_different_resources_are_not_equal
  20. read_orders = ShopifyAPI::ApiAccess.new(%w(read_orders))
  21. read_products = ShopifyAPI::ApiAccess.new(%w(read_products))
  22. refute_equal read_orders, read_products
  23. end
  24. def test_two_identical_scopes_are_equal
  25. read_orders = ShopifyAPI::ApiAccess.new(%w(read_orders))
  26. read_orders_identical = ShopifyAPI::ApiAccess.new(%w(read_orders))
  27. assert_equal read_orders_identical, read_orders
  28. end
  29. def test_unauthenticated_is_not_implied_by_authenticated_access
  30. unauthenticated_orders = ShopifyAPI::ApiAccess.new(%w(unauthenticated_read_orders))
  31. authenticated_read_orders = ShopifyAPI::ApiAccess.new(%w(read_orders))
  32. authenticated_write_orders = ShopifyAPI::ApiAccess.new(%w(write_orders))
  33. refute_equal unauthenticated_orders, authenticated_read_orders
  34. refute_equal unauthenticated_orders, authenticated_write_orders
  35. end
  36. def test_scopes_covers_is_truthy_for_same_scopes
  37. read_orders = ShopifyAPI::ApiAccess.new(%w(read_orders))
  38. read_orders_identical = ShopifyAPI::ApiAccess.new(%w(read_orders))
  39. assert read_orders.covers?(read_orders_identical)
  40. end
  41. def test_covers_is_falsy_for_different_scopes
  42. read_orders = ShopifyAPI::ApiAccess.new(%w(read_orders))
  43. read_products = ShopifyAPI::ApiAccess.new(%w(read_products))
  44. refute read_orders.covers?(read_products)
  45. end
  46. def test_covers_is_truthy_for_read_when_the_set_has_read_write
  47. write_products = ShopifyAPI::ApiAccess.new(%w(write_products))
  48. read_products = ShopifyAPI::ApiAccess.new(%w(read_products))
  49. assert write_products.covers?(read_products)
  50. end
  51. def test_covers_is_truthy_for_read_when_the_set_has_read_write_for_that_resource_and_others
  52. write_products_and_orders = ShopifyAPI::ApiAccess.new(%w(write_products, write_orders))
  53. read_orders = ShopifyAPI::ApiAccess.new(%w(read_orders))
  54. assert write_products_and_orders.covers?(read_orders)
  55. end
  56. def test_covers_is_truthy_for_write_when_the_set_has_read_write_for_that_resource_and_others
  57. write_products_and_orders = ShopifyAPI::ApiAccess.new(%w(write_products, write_orders))
  58. write_orders = ShopifyAPI::ApiAccess.new(%w(write_orders))
  59. assert write_products_and_orders.covers?(write_orders)
  60. end
  61. def test_covers_is_truthy_for_subset_of_scopes
  62. write_products_orders_customers = ShopifyAPI::ApiAccess.new(%w(write_products write_orders write_customers))
  63. write_orders_products = ShopifyAPI::ApiAccess.new(%w(write_orders read_products))
  64. assert write_products_orders_customers.covers?(write_orders_products)
  65. end
  66. def test_covers_is_falsy_for_sets_of_scopes_that_have_no_common_elements
  67. write_products_orders_customers = ShopifyAPI::ApiAccess.new(%w(write_products write_orders write_customers))
  68. write_images_read_content = ShopifyAPI::ApiAccess.new(%w(write_images read_content))
  69. refute write_products_orders_customers.covers?(write_images_read_content)
  70. end
  71. def test_covers_is_falsy_for_sets_of_scopes_that_have_only_some_common_access
  72. write_products_orders_customers = ShopifyAPI::ApiAccess.new(%w(write_products write_orders write_customers))
  73. write_products_read_content = ShopifyAPI::ApiAccess.new(%w(write_products read_content))
  74. refute write_products_orders_customers.covers?(write_products_read_content)
  75. end
  76. def test_duplicate_scopes_resolve_to_one_scope
  77. read_orders_duplicated = ShopifyAPI::ApiAccess.new(%w(read_orders read_orders read_orders read_orders))
  78. read_orders = ShopifyAPI::ApiAccess.new(%w(read_orders))
  79. assert_equal read_orders, read_orders_duplicated
  80. end
  81. def test_to_s_outputs_scopes_as_a_comma_separated_list_without_implied_read_scopes
  82. serialized_read_products_write_orders = "read_products,write_orders"
  83. read_products_write_orders = ShopifyAPI::ApiAccess.new(%w(read_products read_orders write_orders))
  84. assert_equal read_products_write_orders.to_s, serialized_read_products_write_orders
  85. end
  86. def test_to_a_outputs_scopes_as_an_array_of_strings_without_implied_read_scopes
  87. serialized_read_products_write_orders = %w(write_orders read_products)
  88. read_products_write_orders = ShopifyAPI::ApiAccess.new(%w(read_products read_orders write_orders))
  89. assert_equal read_products_write_orders.to_a.sort, serialized_read_products_write_orders.sort
  90. end
  91. def test_creating_scopes_removes_extra_whitespace_from_scope_name_and_blank_scope_names
  92. deserialized_read_products_write_orders = ShopifyAPI::ApiAccess.new([' read_products', ' ', 'write_orders '])
  93. serialized_read_products_write_orders = deserialized_read_products_write_orders.to_s
  94. expected_read_products_write_orders = ShopifyAPI::ApiAccess.new(%w(read_products write_orders))
  95. assert_equal expected_read_products_write_orders, ShopifyAPI::ApiAccess.new(serialized_read_products_write_orders)
  96. end
  97. def test_creating_scopes_from_a_string_works_with_a_comma_separated_list
  98. deserialized_read_products_write_orders = ShopifyAPI::ApiAccess.new("read_products,write_orders")
  99. serialized_read_products_write_orders = deserialized_read_products_write_orders.to_s
  100. expected_read_products_write_orders = ShopifyAPI::ApiAccess.new(%w(read_products write_orders))
  101. assert_equal expected_read_products_write_orders, ShopifyAPI::ApiAccess.new(serialized_read_products_write_orders)
  102. end
  103. def test_using_to_s_from_one_scopes_to_construct_another_will_be_equal
  104. read_products_write_orders = ShopifyAPI::ApiAccess.new(%w(read_products write_orders))
  105. assert_equal read_products_write_orders, ShopifyAPI::ApiAccess.new(read_products_write_orders.to_s)
  106. end
  107. def test_using_to_a_from_one_scopes_to_construct_another_will_be_equal
  108. read_products_write_orders = ShopifyAPI::ApiAccess.new(%w(read_products write_orders))
  109. assert_equal read_products_write_orders, ShopifyAPI::ApiAccess.new(read_products_write_orders.to_a)
  110. end
  111. end