can_associations_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe ApplicationModel::CanAssociations, type: :model do
  4. describe '.association_name_to_id_convert' do
  5. it 'converts has many users association by login' do
  6. user = create(:user)
  7. params = { name: 'org', members: [user.login] }
  8. converted_params = Organization.association_name_to_id_convert(params)
  9. expect(converted_params)
  10. .to eq({
  11. name: 'org',
  12. member_ids: [user.id]
  13. })
  14. end
  15. it 'converts has many users association by email' do
  16. user = create(:user)
  17. params = { name: 'org', members: [user.email] }
  18. converted_params = Organization.association_name_to_id_convert(params)
  19. expect(converted_params)
  20. .to eq({
  21. name: 'org',
  22. member_ids: [user.id]
  23. })
  24. end
  25. it 'keeps IDs for has many associations if given in non _id field' do
  26. params = { name: 'org', members: [123] }
  27. converted_params = Organization.association_name_to_id_convert(params)
  28. expect(converted_params)
  29. .to eq({
  30. name: 'org',
  31. members: [123]
  32. })
  33. end
  34. it 'raises error if has many association is given non-existant identifier' do
  35. params = { name: 'org', members: ['nonexistantstring'] }
  36. expect { Organization.association_name_to_id_convert(params) }
  37. .to raise_error Exceptions::UnprocessableEntity, %r{No lookup value found}
  38. end
  39. it 'converts non-user has many association by name' do
  40. token = create(:token)
  41. params = { email: 'email@example.org', tokens: [token.name] }
  42. converted_params = User.association_name_to_id_convert(params)
  43. expect(converted_params)
  44. .to eq({
  45. email: 'email@example.org',
  46. token_ids: [token.id]
  47. })
  48. end
  49. it 'does not convert named association if ids are given' do
  50. user = create(:user)
  51. params = { name: 'org', members: [user.login], member_ids: [512] }
  52. converted_params = Organization.association_name_to_id_convert(params)
  53. expect(converted_params).to eq(params)
  54. end
  55. it 'converts belongs to users association by login' do
  56. user = create(:user)
  57. params = { name: 'token', user: user.login }
  58. converted_params = Token.association_name_to_id_convert(params)
  59. expect(converted_params)
  60. .to eq({
  61. name: 'token',
  62. user_id: user.id
  63. })
  64. end
  65. it 'converts belongs to users association by email' do
  66. user = create(:user)
  67. params = { name: 'token', user: user.email }
  68. converted_params = Token.association_name_to_id_convert(params)
  69. expect(converted_params).to eq({
  70. name: 'token',
  71. user_id: user.id
  72. })
  73. end
  74. it 'raises error if belongs to association is given a non-existant identifier' do
  75. params = { name: 'token', user: 'nonexistantstring' }
  76. expect { Token.association_name_to_id_convert(params) }
  77. .to raise_error Exceptions::UnprocessableEntity, %r{No lookup value found}
  78. end
  79. it 'converts non-user belongs to association by name' do
  80. organization = create(:organization)
  81. params = { email: 'email@example.org', organization: organization.name }
  82. converted_params = User.association_name_to_id_convert(params)
  83. expect(converted_params)
  84. .to eq({
  85. email: 'email@example.org',
  86. organization_id: organization.id
  87. })
  88. end
  89. it 'overrides belongs to association if both name and ID are given' do
  90. organization = create(:organization)
  91. params = { email: 'email@example.org', organization: organization.name, organization_id: 123 }
  92. converted_params = User.association_name_to_id_convert(params)
  93. expect(converted_params).to eq(params)
  94. end
  95. it 'raises error if ID is given as belongs to identifier' do
  96. organization = create(:organization)
  97. params = { email: 'email@example.org', organization: organization.id }
  98. expect { User.association_name_to_id_convert(params) }
  99. .to raise_error Exceptions::UnprocessableEntity, %r{No lookup value found}
  100. end
  101. end
  102. end