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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 59 or above.
  67. /// </summary>
  68. [Theory]
  69. [InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWs")]
  70. [InlineData("This appears to be completely invalid, however the current validation rules are not very strict.")]
  71. [InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWss")]
  72. [InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWsMTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWs")]
  73. public void TestBotTokenDoesNotThrowExceptions(string token)
  74. {
  75. // This example token is pulled from the Discord Docs
  76. // https://discordapp.com/developers/docs/reference#authentication-example-bot-token-authorization-header
  77. // should not throw any exception
  78. TokenUtils.ValidateToken(TokenType.Bot, token);
  79. }
  80. /// <summary>
  81. /// Tests the usage of <see cref="TokenUtils.ValidateToken(TokenType, string)"/> with
  82. /// a Bot token that is invalid.
  83. /// </summary>
  84. [Theory]
  85. [InlineData("This is invalid")]
  86. // missing a single character from the end
  87. [InlineData("MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKW")]
  88. // bearer token
  89. [InlineData("6qrZcUqja7812RVdnEKjpzOL4CvHBFG")]
  90. // client secret
  91. [InlineData("937it3ow87i4ery69876wqire")]
  92. public void TestBotTokenInvalidThrowsArgumentException(string token)
  93. {
  94. Assert.Throws<ArgumentException>(() => TokenUtils.ValidateToken(TokenType.Bot, token));
  95. }
  96. /// <summary>
  97. /// Tests the behavior of <see cref="TokenUtils.ValidateToken(TokenType, string)"/>
  98. /// to see that an <see cref="ArgumentException"/> is thrown when an invalid
  99. /// <see cref="TokenType"/> is supplied as a parameter.
  100. /// </summary>
  101. /// <remarks>
  102. /// The <see cref="TokenType.User"/> type is treated as an invalid <see cref="TokenType"/>.
  103. /// </remarks>
  104. [Theory]
  105. // TokenType.User
  106. [InlineData(0)]
  107. // out of range TokenType
  108. [InlineData(4)]
  109. [InlineData(7)]
  110. public void TestUnrecognizedTokenType(int type)
  111. {
  112. Assert.Throws<ArgumentException>(() =>
  113. TokenUtils.ValidateToken((TokenType)type, "MTk4NjIyNDgzNDcxOTI1MjQ4.Cl2FMQ.ZnCjm1XVW7vRze4b7Cq4se7kKWs"));
  114. }
  115. }
  116. }