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.

Tests.TokenUtils.cs 5.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Xunit;
  5. namespace Discord
  6. {
  7. public class TokenUtilsTests
  8. {
  9. /// <summary>
  10. /// Tests the usage of <see cref="TokenUtils.ValidateToken(TokenType, string)"/>
  11. /// to see that when a null, empty or whitespace-only string is passed as the token,
  12. /// it will throw an ArgumentNullException.
  13. /// </summary>
  14. [Theory]
  15. [InlineData(null)]
  16. [InlineData("")] // string.Empty isn't a constant type
  17. [InlineData(" ")]
  18. [InlineData(" ")]
  19. [InlineData("\t")]
  20. public void TestNullOrWhitespaceToken(string token)
  21. {
  22. // an ArgumentNullException should be thrown, regardless of the TokenType
  23. Assert.Throws<ArgumentNullException>(() => TokenUtils.ValidateToken(TokenType.Bearer, token));
  24. Assert.Throws<ArgumentNullException>(() => TokenUtils.ValidateToken(TokenType.Bot, token));
  25. Assert.Throws<ArgumentNullException>(() => TokenUtils.ValidateToken(TokenType.Webhook, token));
  26. }
  27. /// <summary>
  28. /// Tests the behavior of <see cref="TokenUtils.ValidateToken(TokenType, string)"/>
  29. /// to see that valid Webhook tokens do not throw Exceptions.
  30. /// </summary>
  31. /// <param name="token"></param>
  32. [Theory]
  33. [InlineData("123123123")]
  34. // bot token
  35. [InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWs")]
  36. // bearer token taken from discord docs
  37. [InlineData("6qrZcUqja7812RVdnEKjpzOL4CvHBFG")]
  38. // client secret
  39. [InlineData("937it3ow87i4ery69876wqire")]
  40. public void TestWebhookTokenDoesNotThrowExceptions(string token)
  41. {
  42. TokenUtils.ValidateToken(TokenType.Webhook, token);
  43. }
  44. // No tests for invalid webhook token behavior, because there is nothing there yet.
  45. /// <summary>
  46. /// Tests the behavior of <see cref="TokenUtils.ValidateToken(TokenType, string)"/>
  47. /// to see that valid Webhook tokens do not throw Exceptions.
  48. /// </summary>
  49. /// <param name="token"></param>
  50. [Theory]
  51. [InlineData("123123123")]
  52. // bot token
  53. [InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWs")]
  54. // bearer token taken from discord docs
  55. [InlineData("6qrZcUqja7812RVdnEKjpzOL4CvHBFG")]
  56. // client secret
  57. [InlineData("937it3ow87i4ery69876wqire")]
  58. public void TestBearerTokenDoesNotThrowExceptions(string token)
  59. {
  60. TokenUtils.ValidateToken(TokenType.Bearer, token);
  61. }
  62. // No tests for invalid bearer token behavior, because there is nothing there yet.
  63. /// <summary>
  64. /// Tests the behavior of <see cref="TokenUtils.ValidateToken(TokenType, string)"/>
  65. /// to see that valid Bot tokens do not throw Exceptions.
  66. /// Valid Bot tokens can be strings of length 58 or above.
  67. /// </summary>
  68. [Theory]
  69. // missing a single character from the end, 58 char. still should be valid
  70. [InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKW")]
  71. // 59 char token
  72. [InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWs")]
  73. [InlineData("This appears to be completely invalid, however the current validation rules are not very strict.")]
  74. [InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWss")]
  75. [InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWsMTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWs")]
  76. public void TestBotTokenDoesNotThrowExceptions(string token)
  77. {
  78. // This example token is pulled from the Discord Docs
  79. // https://discordapp.com/developers/docs/reference#authentication-example-bot-token-authorization-header
  80. // should not throw any exception
  81. TokenUtils.ValidateToken(TokenType.Bot, token);
  82. }
  83. /// <summary>
  84. /// Tests the usage of <see cref="TokenUtils.ValidateToken(TokenType, string)"/> with
  85. /// a Bot token that is invalid.
  86. /// </summary>
  87. [Theory]
  88. [InlineData("This is invalid")]
  89. // bearer token
  90. [InlineData("6qrZcUqja7812RVdnEKjpzOL4CvHBFG")]
  91. // client secret
  92. [InlineData("937it3ow87i4ery69876wqire")]
  93. // 57 char bot token
  94. [InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kK")]
  95. public void TestBotTokenInvalidThrowsArgumentException(string token)
  96. {
  97. Assert.Throws<ArgumentException>(() => TokenUtils.ValidateToken(TokenType.Bot, token));
  98. }
  99. /// <summary>
  100. /// Tests the behavior of <see cref="TokenUtils.ValidateToken(TokenType, string)"/>
  101. /// to see that an <see cref="ArgumentException"/> is thrown when an invalid
  102. /// <see cref="TokenType"/> is supplied as a parameter.
  103. /// </summary>
  104. /// <remarks>
  105. /// The <see cref="TokenType.User"/> type is treated as an invalid <see cref="TokenType"/>.
  106. /// </remarks>
  107. [Theory]
  108. // TokenType.User
  109. [InlineData(0)]
  110. // out of range TokenType
  111. [InlineData(-1)]
  112. [InlineData(4)]
  113. [InlineData(7)]
  114. public void TestUnrecognizedTokenType(int type)
  115. {
  116. Assert.Throws<ArgumentException>(() =>
  117. TokenUtils.ValidateToken((TokenType)type, "MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWs"));
  118. }
  119. }
  120. }