You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChannelPermissionsTests.cs 12 kB

test: Split Unit and Integration tests into separate projects (#1290) * Squashed commit of test rewrite changes fix missing priority speaker flag rewrite the TestChannelPermissionModify test add test for GuildPermission modify separate unit and integration tests, start writing channel and guild permission tests copy over the color tests copy over the emote tests copy the token utils tests make the mocked entities sealed classes copy the TypeReaderTests class properly dispose the CommandService in the TypeReaderTests start writing tests for EmbedBuilder and related classes test that properties throw ArgumentException when invalid add tests for the embed length property add withFooter tests finish adding tests to EmbedBuilder fix bug in value validation of EmbedFieldBuilder hey, these tests actually found a bug! add tests for the MentionUtils class add tests for the Format util class remove all of the old tests add analyzer tests (copied from old tests) add tests for the SnowflakeUtils class add integration tests these get around the issue of state persisting between tests by creating and deleting a guild for each set of tests. these shouldn't be run excessively because of the rate limits, but should be fine every now and then remove unnecessary launchSettings.json update outdated string don't create a new guild each time, as that can result in errors this can happen if a bot creates too many guilds without properly deleting them add some tests that show that guild can be modified await async assert add more measures that created channels are deleted when done remove "Test" prefix from test method names I think that this prefix when already displayed under a class with a suffix of "Tests" is redundant Remove mention of old test project fix an issue from forgetting to await Assert.ThrowsAsync explicitly disable parallelization on integration tests add test for GuildPermission modify separate unit and integration tests, start writing channel and guild permission tests copy over the color tests copy over the emote tests make the mocked entities sealed classes properly dispose the CommandService in the TypeReaderTests fix bug in value validation of EmbedFieldBuilder hey, these tests actually found a bug! add tests for the MentionUtils class add tests for the Format util class remove all of the old tests add analyzer tests (copied from old tests) add tests for the SnowflakeUtils class add integration tests these get around the issue of state persisting between tests by creating and deleting a guild for each set of tests. these shouldn't be run excessively because of the rate limits, but should be fine every now and then remove unnecessary launchSettings.json update outdated string don't create a new guild each time, as that can result in errors this can happen if a bot creates too many guilds without properly deleting them add more measures that created channels are deleted when done remove "Test" prefix from test method names I think that this prefix when already displayed under a class with a suffix of "Tests" is redundant Remove mention of old test project fix an issue from forgetting to await Assert.ThrowsAsync explicitly disable parallelization on integration tests update the azure CI build script separate execution of test projects so that if one fails the other will not pass one of the unit tests failed, but the analzyer tests passed fix test that would break in different timezones enable the integration tests (only on dev branch) * Squashed commit of test rewrite changes fix missing priority speaker flag rewrite the TestChannelPermissionModify test add test for GuildPermission modify separate unit and integration tests, start writing channel and guild permission tests copy over the color tests copy over the emote tests copy the token utils tests make the mocked entities sealed classes copy the TypeReaderTests class properly dispose the CommandService in the TypeReaderTests start writing tests for EmbedBuilder and related classes test that properties throw ArgumentException when invalid add tests for the embed length property add withFooter tests finish adding tests to EmbedBuilder fix bug in value validation of EmbedFieldBuilder hey, these tests actually found a bug! add tests for the MentionUtils class add tests for the Format util class remove all of the old tests add analyzer tests (copied from old tests) add tests for the SnowflakeUtils class add integration tests these get around the issue of state persisting between tests by creating and deleting a guild for each set of tests. these shouldn't be run excessively because of the rate limits, but should be fine every now and then remove unnecessary launchSettings.json update outdated string don't create a new guild each time, as that can result in errors this can happen if a bot creates too many guilds without properly deleting them add some tests that show that guild can be modified await async assert add more measures that created channels are deleted when done remove "Test" prefix from test method names I think that this prefix when already displayed under a class with a suffix of "Tests" is redundant Remove mention of old test project fix an issue from forgetting to await Assert.ThrowsAsync explicitly disable parallelization on integration tests add test for GuildPermission modify separate unit and integration tests, start writing channel and guild permission tests copy over the color tests copy over the emote tests make the mocked entities sealed classes properly dispose the CommandService in the TypeReaderTests fix bug in value validation of EmbedFieldBuilder hey, these tests actually found a bug! add tests for the MentionUtils class add tests for the Format util class remove all of the old tests add analyzer tests (copied from old tests) add tests for the SnowflakeUtils class add integration tests these get around the issue of state persisting between tests by creating and deleting a guild for each set of tests. these shouldn't be run excessively because of the rate limits, but should be fine every now and then remove unnecessary launchSettings.json update outdated string don't create a new guild each time, as that can result in errors this can happen if a bot creates too many guilds without properly deleting them add more measures that created channels are deleted when done remove "Test" prefix from test method names I think that this prefix when already displayed under a class with a suffix of "Tests" is redundant Remove mention of old test project fix an issue from forgetting to await Assert.ThrowsAsync explicitly disable parallelization on integration tests update the azure CI build script separate execution of test projects so that if one fails the other will not pass one of the unit tests failed, but the analzyer tests passed fix test that would break in different timezones enable the integration tests (only on dev branch) * Update mocked channels for changed SendFileAsync signature * comment out the integration tests from the build script no bot token is provided to this script, and use of integration tests in CI is questionable here * force rebuild because Azure linux build broke
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Collections.Generic;
  3. using Xunit;
  4. namespace Discord
  5. {
  6. /// <summary>
  7. /// Tests the behavior of the <see cref="Discord.ChannelPermissions"/> type and related functions.
  8. /// </summary>
  9. public class ChannelPermissionsTests
  10. {
  11. /// <summary>
  12. /// Tests the default value of the <see cref="Discord.ChannelPermissions"/> constructor.
  13. /// </summary>
  14. [Fact]
  15. public void DefaultConstructor()
  16. {
  17. var permission = new ChannelPermissions();
  18. Assert.Equal((ulong)0, permission.RawValue);
  19. Assert.Equal(ChannelPermissions.None.RawValue, permission.RawValue);
  20. }
  21. /// <summary>
  22. /// Tests the behavior of the <see cref="Discord.ChannelPermission"/> raw value constructor.
  23. /// </summary>
  24. [Fact]
  25. public void RawValueConstructor()
  26. {
  27. // returns all of the values that will be tested
  28. // a Theory cannot be used here, because these values are not all constants
  29. IEnumerable<ulong> GetTestValues()
  30. {
  31. yield return 0;
  32. yield return ChannelPermissions.Category.RawValue;
  33. yield return ChannelPermissions.DM.RawValue;
  34. yield return ChannelPermissions.Group.RawValue;
  35. yield return ChannelPermissions.None.RawValue;
  36. yield return ChannelPermissions.Text.RawValue;
  37. yield return ChannelPermissions.Voice.RawValue;
  38. };
  39. foreach (var rawValue in GetTestValues())
  40. {
  41. var p = new ChannelPermissions(rawValue);
  42. Assert.Equal(rawValue, p.RawValue);
  43. }
  44. }
  45. /// <summary>
  46. /// Tests the behavior of the <see cref="Discord.ChannelPermissions"/> constructor for each
  47. /// of it's flags.
  48. /// </summary>
  49. [Fact]
  50. public void FlagsConstructor()
  51. {
  52. // util method for asserting that the constructor sets the given flag
  53. void AssertFlag(Func<ChannelPermissions> cstr, ChannelPermission flag)
  54. {
  55. var p = cstr();
  56. // ensure that this flag is set to true
  57. Assert.True(p.Has(flag));
  58. // ensure that only this flag is set
  59. Assert.Equal((ulong)flag, p.RawValue);
  60. }
  61. AssertFlag(() => new ChannelPermissions(createInstantInvite: true), ChannelPermission.CreateInstantInvite);
  62. AssertFlag(() => new ChannelPermissions(manageChannel: true), ChannelPermission.ManageChannels);
  63. AssertFlag(() => new ChannelPermissions(addReactions: true), ChannelPermission.AddReactions);
  64. AssertFlag(() => new ChannelPermissions(viewChannel: true), ChannelPermission.ViewChannel);
  65. AssertFlag(() => new ChannelPermissions(sendMessages: true), ChannelPermission.SendMessages);
  66. AssertFlag(() => new ChannelPermissions(sendTTSMessages: true), ChannelPermission.SendTTSMessages);
  67. AssertFlag(() => new ChannelPermissions(manageMessages: true), ChannelPermission.ManageMessages);
  68. AssertFlag(() => new ChannelPermissions(embedLinks: true), ChannelPermission.EmbedLinks);
  69. AssertFlag(() => new ChannelPermissions(attachFiles: true), ChannelPermission.AttachFiles);
  70. AssertFlag(() => new ChannelPermissions(readMessageHistory: true), ChannelPermission.ReadMessageHistory);
  71. AssertFlag(() => new ChannelPermissions(mentionEveryone: true), ChannelPermission.MentionEveryone);
  72. AssertFlag(() => new ChannelPermissions(useExternalEmojis: true), ChannelPermission.UseExternalEmojis);
  73. AssertFlag(() => new ChannelPermissions(connect: true), ChannelPermission.Connect);
  74. AssertFlag(() => new ChannelPermissions(speak: true), ChannelPermission.Speak);
  75. AssertFlag(() => new ChannelPermissions(muteMembers: true), ChannelPermission.MuteMembers);
  76. AssertFlag(() => new ChannelPermissions(deafenMembers: true), ChannelPermission.DeafenMembers);
  77. AssertFlag(() => new ChannelPermissions(moveMembers: true), ChannelPermission.MoveMembers);
  78. AssertFlag(() => new ChannelPermissions(useVoiceActivation: true), ChannelPermission.UseVAD);
  79. AssertFlag(() => new ChannelPermissions(prioritySpeaker: true), ChannelPermission.PrioritySpeaker);
  80. AssertFlag(() => new ChannelPermissions(manageRoles: true), ChannelPermission.ManageRoles);
  81. AssertFlag(() => new ChannelPermissions(manageWebhooks: true), ChannelPermission.ManageWebhooks);
  82. }
  83. /// <summary>
  84. /// Tests the behavior of <see cref="Discord.ChannelPermissions.Modify(bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?)"/>
  85. /// with each of the parameters.
  86. /// </summary>
  87. [Fact]
  88. public void Modify()
  89. {
  90. // asserts that a channel permission flag value can be checked
  91. // and that modify can set and unset each flag
  92. // and that ToList performs as expected
  93. void AssertUtil(ChannelPermission permission,
  94. Func<ChannelPermissions, bool> has,
  95. Func<ChannelPermissions, bool, ChannelPermissions> modify)
  96. {
  97. var perm = new ChannelPermissions();
  98. // ensure permission initially false
  99. // use both the function and Has to ensure that the GetPermission
  100. // function is working
  101. Assert.False(has(perm));
  102. Assert.False(perm.Has(permission));
  103. // enable it, and ensure that it gets set
  104. perm = modify(perm, true);
  105. Assert.True(has(perm));
  106. Assert.True(perm.Has(permission));
  107. // check ToList behavior
  108. var list = perm.ToList();
  109. Assert.Contains(permission, list);
  110. Assert.Single(list);
  111. // set it false again
  112. perm = modify(perm, false);
  113. Assert.False(has(perm));
  114. Assert.False(perm.Has(permission));
  115. // ensure that no perms are set now
  116. Assert.Equal(ChannelPermissions.None.RawValue, perm.RawValue);
  117. }
  118. AssertUtil(ChannelPermission.CreateInstantInvite, x => x.CreateInstantInvite, (p, enable) => p.Modify(createInstantInvite: enable));
  119. AssertUtil(ChannelPermission.ManageChannels, x => x.ManageChannel, (p, enable) => p.Modify(manageChannel: enable));
  120. AssertUtil(ChannelPermission.AddReactions, x => x.AddReactions, (p, enable) => p.Modify(addReactions: enable));
  121. AssertUtil(ChannelPermission.ViewChannel, x => x.ViewChannel, (p, enable) => p.Modify(viewChannel: enable));
  122. AssertUtil(ChannelPermission.SendMessages, x => x.SendMessages, (p, enable) => p.Modify(sendMessages: enable));
  123. AssertUtil(ChannelPermission.SendTTSMessages, x => x.SendTTSMessages, (p, enable) => p.Modify(sendTTSMessages: enable));
  124. AssertUtil(ChannelPermission.ManageMessages, x => x.ManageMessages, (p, enable) => p.Modify(manageMessages: enable));
  125. AssertUtil(ChannelPermission.EmbedLinks, x => x.EmbedLinks, (p, enable) => p.Modify(embedLinks: enable));
  126. AssertUtil(ChannelPermission.AttachFiles, x => x.AttachFiles, (p, enable) => p.Modify(attachFiles: enable));
  127. AssertUtil(ChannelPermission.ReadMessageHistory, x => x.ReadMessageHistory, (p, enable) => p.Modify(readMessageHistory: enable));
  128. AssertUtil(ChannelPermission.MentionEveryone, x => x.MentionEveryone, (p, enable) => p.Modify(mentionEveryone: enable));
  129. AssertUtil(ChannelPermission.UseExternalEmojis, x => x.UseExternalEmojis, (p, enable) => p.Modify(useExternalEmojis: enable));
  130. AssertUtil(ChannelPermission.Connect, x => x.Connect, (p, enable) => p.Modify(connect: enable));
  131. AssertUtil(ChannelPermission.Speak, x => x.Speak, (p, enable) => p.Modify(speak: enable));
  132. AssertUtil(ChannelPermission.MuteMembers, x => x.MuteMembers, (p, enable) => p.Modify(muteMembers: enable));
  133. AssertUtil(ChannelPermission.DeafenMembers, x => x.DeafenMembers, (p, enable) => p.Modify(deafenMembers: enable));
  134. AssertUtil(ChannelPermission.MoveMembers, x => x.MoveMembers, (p, enable) => p.Modify(moveMembers: enable));
  135. AssertUtil(ChannelPermission.UseVAD, x => x.UseVAD, (p, enable) => p.Modify(useVoiceActivation: enable));
  136. AssertUtil(ChannelPermission.ManageRoles, x => x.ManageRoles, (p, enable) => p.Modify(manageRoles: enable));
  137. AssertUtil(ChannelPermission.ManageWebhooks, x => x.ManageWebhooks, (p, enable) => p.Modify(manageWebhooks: enable));
  138. AssertUtil(ChannelPermission.PrioritySpeaker, x => x.PrioritySpeaker, (p, enable) => p.Modify(prioritySpeaker: enable));
  139. }
  140. /// <summary>
  141. /// Tests that <see cref="ChannelPermissions.All(IChannel)"/> for a null channel will throw an <see cref="ArgumentException"/>.
  142. /// </summary>
  143. [Fact]
  144. public void ChannelTypeResolution_Null()
  145. {
  146. Assert.Throws<ArgumentException>(() =>
  147. {
  148. ChannelPermissions.All(null);
  149. });
  150. }
  151. /// <summary>
  152. /// Tests that <see cref="ChannelPermissions.All(IChannel)"/> for an <see cref="ITextChannel"/> will return a value
  153. /// equivalent to <see cref="ChannelPermissions.Text"/>.
  154. /// </summary>
  155. [Fact]
  156. public void ChannelTypeResolution_Text()
  157. {
  158. Assert.Equal(ChannelPermissions.Text.RawValue, ChannelPermissions.All(new MockedTextChannel()).RawValue);
  159. }
  160. /// <summary>
  161. /// Tests that <see cref="ChannelPermissions.All(IChannel)"/> for an <see cref="IVoiceChannel"/> will return a value
  162. /// equivalent to <see cref="ChannelPermissions.Voice"/>.
  163. /// </summary>
  164. [Fact]
  165. public void ChannelTypeResolution_Voice()
  166. {
  167. Assert.Equal(ChannelPermissions.Voice.RawValue, ChannelPermissions.All(new MockedVoiceChannel()).RawValue);
  168. }
  169. /// <summary>
  170. /// Tests that <see cref="ChannelPermissions.All(IChannel)"/> for an <see cref="ICategoryChannel"/> will return a value
  171. /// equivalent to <see cref="ChannelPermissions.Category"/>.
  172. /// </summary>
  173. [Fact]
  174. public void ChannelTypeResolution_Category()
  175. {
  176. Assert.Equal(ChannelPermissions.Category.RawValue, ChannelPermissions.All(new MockedCategoryChannel()).RawValue);
  177. }
  178. /// <summary>
  179. /// Tests that <see cref="ChannelPermissions.All(IChannel)"/> for an <see cref="IDMChannel"/> will return a value
  180. /// equivalent to <see cref="ChannelPermissions.DM"/>.
  181. /// </summary>
  182. [Fact]
  183. public void ChannelTypeResolution_DM()
  184. {
  185. Assert.Equal(ChannelPermissions.DM.RawValue, ChannelPermissions.All(new MockedDMChannel()).RawValue);
  186. }
  187. /// <summary>
  188. /// Tests that <see cref="ChannelPermissions.All(IChannel)"/> for an <see cref="IGroupChannel"/> will return a value
  189. /// equivalent to <see cref="ChannelPermissions.Group"/>.
  190. /// </summary>
  191. [Fact]
  192. public void ChannelTypeResolution_Group()
  193. {
  194. Assert.Equal(ChannelPermissions.Group.RawValue, ChannelPermissions.All(new MockedGroupChannel()).RawValue);
  195. }
  196. /// <summary>
  197. /// Tests that <see cref="ChannelPermissions.All(IChannel)"/> for an invalid channel will throw an <see cref="ArgumentException"/>.
  198. /// </summary>
  199. [Fact]
  200. public void ChannelTypeResolution_Invalid()
  201. {
  202. Assert.Throws<ArgumentException>(() => ChannelPermissions.All(new MockedInvalidChannel()));
  203. }
  204. }
  205. }