/shared/AppInsightsCommon/Tests/AppInsightsCommon.tests.ts

https://github.com/Microsoft/ApplicationInsights-JS · TypeScript · 294 lines · 191 code · 67 blank · 36 comment · 0 complexity · 8585b371dbf0db22883cfc7b836d58d5 MD5 · raw file

  1. /// <reference path="./TestFramework/Common.ts" />
  2. import {
  3. ITelemetryItem, AppInsightsCore,
  4. IPlugin, IConfiguration, DiagnosticLogger
  5. } from "@microsoft/applicationinsights-core-js";
  6. import { DataSanitizer } from "../src/Telemetry/Common/DataSanitizer";
  7. export class ApplicationInsightsTests extends TestClass {
  8. logger = new DiagnosticLogger();
  9. public testInitialize() {
  10. this.clock.reset();
  11. }
  12. public testCleanup() {
  13. }
  14. public registerTests() {
  15. // test cases for sanitizeMessage function
  16. this.testCase({
  17. name: 'DataSanitizerTests: messages are well processed.',
  18. test: () => {
  19. // const define
  20. const MAX_MESSAGE_LENGTH = DataSanitizer.MAX_MESSAGE_LENGTH;
  21. // use cases
  22. const messageShort: String = "hi";
  23. const messageLong = new Array(MAX_MESSAGE_LENGTH + 1).join('abc');
  24. // Assert
  25. Assert.equal(messageShort.length, DataSanitizer.sanitizeMessage(this.logger, messageShort).length);
  26. Assert.notEqual(messageLong.length, DataSanitizer.sanitizeMessage(this.logger, messageLong).length);
  27. Assert.equal(MAX_MESSAGE_LENGTH, DataSanitizer.sanitizeMessage(this.logger, messageLong).length);
  28. }
  29. });
  30. this.testCase({
  31. name: 'DataSanitizerTests: throwInternal function is called correctly in sanitizeMessage function',
  32. test: () => {
  33. // const define
  34. const loggerStub = this.sandbox.stub(this.logger , "throwInternal");
  35. const MAX_MESSAGE_LENGTH = DataSanitizer.MAX_MESSAGE_LENGTH;
  36. // use cases
  37. const messageShort: String = "hi";
  38. const messageLong = new Array(MAX_MESSAGE_LENGTH + 2).join('a');
  39. // Act
  40. DataSanitizer.sanitizeMessage(this.logger, messageShort);
  41. Assert.ok(loggerStub.notCalled);
  42. Assert.equal(0, loggerStub.callCount);
  43. DataSanitizer.sanitizeMessage(this.logger, messageLong);
  44. Assert.ok(loggerStub.calledOnce);
  45. Assert.equal(1, loggerStub.callCount);
  46. DataSanitizer.sanitizeMessage(this.logger, messageLong);
  47. Assert.ok(loggerStub.calledTwice);
  48. Assert.equal(2, loggerStub.callCount);
  49. loggerStub.restore();
  50. }
  51. });
  52. this.testCase({
  53. name: 'DataSanitizerTests: messages are fully logged through console',
  54. test: () => {
  55. const saniMsgSpy = this.sandbox.spy(DataSanitizer, "sanitizeMessage");
  56. // const define
  57. const MAX_MESSAGE_LENGTH = DataSanitizer.MAX_MESSAGE_LENGTH;
  58. // use cases
  59. const messageShort: String = "hi";
  60. const messageLong = new Array(MAX_MESSAGE_LENGTH + 1).join('a');
  61. const msgShortResult = DataSanitizer.sanitizeMessage(this.logger, messageShort);
  62. Assert.ok(saniMsgSpy.returned(msgShortResult));
  63. const msgLongResult = DataSanitizer.sanitizeMessage(this.logger, messageLong);
  64. Assert.ok(saniMsgSpy.returned(msgLongResult));
  65. saniMsgSpy.restore();
  66. }
  67. });
  68. // test cases for sanitizeString function
  69. this.testCase({
  70. name: 'DataSanitizerTest: strings are well processed',
  71. test: () => {
  72. // const define
  73. const MAX_STRING_LENGTH = DataSanitizer.MAX_STRING_LENGTH;
  74. // use cases
  75. const strShort: String = "hi";
  76. const strLong = new Array(MAX_STRING_LENGTH + 2).join('a');
  77. // Assert
  78. Assert.equal(strShort.length, DataSanitizer.sanitizeString(this.logger, strShort).length);
  79. Assert.notEqual(strLong.length, DataSanitizer.sanitizeString(this.logger, strLong).length);
  80. Assert.equal(MAX_STRING_LENGTH, DataSanitizer.sanitizeString(this.logger, strLong).length);
  81. }
  82. });
  83. this.testCase({
  84. name: 'DataSanitizerTests: throrwInternal function is called correctly in sanitizeString function',
  85. test: () => {
  86. // const define
  87. const loggerStub = this.sandbox.stub(this.logger , "throwInternal");
  88. const MAX_STRING_LENGTH = DataSanitizer.MAX_STRING_LENGTH;
  89. // use cases
  90. const strShort: String = "hi";
  91. const strLong = new Array(MAX_STRING_LENGTH + 2).join('a');
  92. // Act
  93. DataSanitizer.sanitizeString(this.logger, strShort);
  94. Assert.ok(loggerStub.notCalled);
  95. Assert.equal(0, loggerStub.callCount);
  96. DataSanitizer.sanitizeString(this.logger, strLong);
  97. Assert.ok(loggerStub.calledOnce);
  98. Assert.equal(1, loggerStub.callCount);
  99. DataSanitizer.sanitizeString(this.logger, strLong);
  100. Assert.ok(loggerStub.calledTwice);
  101. Assert.equal(2, loggerStub.callCount);
  102. loggerStub.restore();
  103. }
  104. });
  105. this.testCase({
  106. name: 'DataSanitizerTests: strings are fully logged through console',
  107. test: () => {
  108. const saniStrSpy = this.sandbox.spy(DataSanitizer, "sanitizeString");
  109. // const define
  110. const MAX_STRING_LENGTH = DataSanitizer.MAX_STRING_LENGTH;
  111. // use cases
  112. const strShort: String = "hi";
  113. const strLong = new Array(MAX_STRING_LENGTH + 2).join('a');
  114. const strShortResult = DataSanitizer.sanitizeString(this.logger, strShort);
  115. Assert.ok(saniStrSpy.returned(strShortResult));
  116. const strLongResult = DataSanitizer.sanitizeString(this.logger, strLong);
  117. Assert.ok(saniStrSpy.returned(strLongResult));
  118. saniStrSpy.restore();
  119. }
  120. });
  121. // test cases for sanitizeKey function
  122. this.testCase({
  123. name: 'DataSanitizerTest: names are well processed',
  124. test: () => {
  125. // const define
  126. const MAX_NAME_LENGTH = DataSanitizer.MAX_NAME_LENGTH;
  127. // use cases
  128. const nameShort: String = "hi";
  129. const nameLong = new Array(MAX_NAME_LENGTH + 2).join('a');
  130. // Assert
  131. Assert.equal(nameShort.length, DataSanitizer.sanitizeKey(this.logger, nameShort).length);
  132. Assert.notEqual(nameLong.length, DataSanitizer.sanitizeKey(this.logger, nameLong).length);
  133. Assert.equal(MAX_NAME_LENGTH, DataSanitizer.sanitizeKey(this.logger, nameLong).length);
  134. }
  135. });
  136. this.testCase({
  137. name: 'DataSanitizerTests: throrwInternal function is called correctly in sanitizeKey function',
  138. test: () => {
  139. // const define
  140. const loggerStub = this.sandbox.stub(this.logger , "throwInternal");
  141. const MAX_NAME_LENGTH = DataSanitizer.MAX_NAME_LENGTH;
  142. // use cases
  143. const nameShort: String = "hi";
  144. const nameLong = new Array(MAX_NAME_LENGTH + 2).join('a');
  145. // Act
  146. DataSanitizer.sanitizeKey(this.logger, nameShort);
  147. Assert.ok(loggerStub.notCalled);
  148. Assert.equal(0, loggerStub.callCount);
  149. DataSanitizer.sanitizeKey(this.logger, nameLong);
  150. Assert.ok(loggerStub.calledOnce);
  151. Assert.equal(1, loggerStub.callCount);
  152. DataSanitizer.sanitizeKey(this.logger, nameLong);
  153. Assert.ok(loggerStub.calledTwice);
  154. Assert.equal(2, loggerStub.callCount);
  155. loggerStub.restore();
  156. }
  157. });
  158. this.testCase({
  159. name: 'DataSanitizerTests: names are fully logged through console',
  160. test: () => {
  161. const saniStrSpy = this.sandbox.spy(DataSanitizer, "sanitizeKey");
  162. // const define
  163. const MAX_NAME_LENGTH = DataSanitizer.MAX_NAME_LENGTH;
  164. // use cases
  165. const nameShort: String = "hi";
  166. const nameLong = new Array(MAX_NAME_LENGTH + 2).join('a');
  167. const nameShortResult = DataSanitizer.sanitizeKey(this.logger, nameShort);
  168. Assert.ok(saniStrSpy.returned(nameShortResult));
  169. const nameLongResult = DataSanitizer.sanitizeKey(this.logger, nameLong);
  170. Assert.ok(saniStrSpy.returned(nameLongResult));
  171. saniStrSpy.restore();
  172. }
  173. });
  174. // test cases for sanitizeInput function
  175. this.testCase({
  176. name: 'DataSanitizerTest: inputs are well processed',
  177. test: () => {
  178. const MAX_INPUT_LENGTH = 1024;
  179. // use cases
  180. const inputShort: String = "hi";
  181. const inputLong = new Array(MAX_INPUT_LENGTH + 2).join('a');
  182. // Assert
  183. Assert.equal(inputShort.length, DataSanitizer.sanitizeInput(this.logger, inputShort, MAX_INPUT_LENGTH, 0).length);
  184. Assert.notEqual(inputLong.length, DataSanitizer.sanitizeInput(this.logger, inputLong, MAX_INPUT_LENGTH, 0).length);
  185. Assert.equal(MAX_INPUT_LENGTH, DataSanitizer.sanitizeInput(this.logger, inputLong, MAX_INPUT_LENGTH, 0).length);
  186. }
  187. });
  188. this.testCase({
  189. name: 'DataSanitizerTests: throrwInternal function is called correctly in sanitizeInput function',
  190. test: () => {
  191. // const define
  192. const loggerStub = this.sandbox.stub(this.logger , "throwInternal");
  193. const MAX_INPUT_LENGTH = 1024;
  194. // use cases
  195. const inputShort: String = "hi";
  196. const inputLong = new Array(MAX_INPUT_LENGTH + 2).join('a');
  197. // Act
  198. DataSanitizer.sanitizeInput(this.logger, inputShort, MAX_INPUT_LENGTH, 0);
  199. Assert.ok(loggerStub.notCalled);
  200. Assert.equal(0, loggerStub.callCount);
  201. DataSanitizer.sanitizeInput(this.logger, inputLong, MAX_INPUT_LENGTH, 0);
  202. Assert.ok(loggerStub.calledOnce);
  203. Assert.equal(1, loggerStub.callCount);
  204. DataSanitizer.sanitizeInput(this.logger, inputLong, MAX_INPUT_LENGTH, 0);
  205. Assert.ok(loggerStub.calledTwice);
  206. Assert.equal(2, loggerStub.callCount);
  207. loggerStub.restore();
  208. }
  209. });
  210. this.testCase({
  211. name: 'DataSanitizerTests: inputs are fully logged through console',
  212. test: () => {
  213. const saniStrSpy = this.sandbox.spy(DataSanitizer, "sanitizeInput");
  214. // const define
  215. const MAX_INPUT_LENGTH = 1024;
  216. // use cases
  217. const inputShort: String = "hi";
  218. const inputLong = new Array(MAX_INPUT_LENGTH + 2).join('a');
  219. const inputShortResult = DataSanitizer.sanitizeInput(this.logger, inputShort, MAX_INPUT_LENGTH, 0);
  220. Assert.ok(saniStrSpy.returned(inputShortResult));
  221. const inputLongResult = DataSanitizer.sanitizeInput(this.logger, inputLong, MAX_INPUT_LENGTH, 0);
  222. Assert.ok(saniStrSpy.returned(inputLongResult));
  223. saniStrSpy.restore();
  224. }
  225. });
  226. }
  227. }