sintfacilis 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (461) hide show
  1. package/.env.example +2 -0
  2. package/.eslintrc.js +20 -0
  3. package/.github/workflows/deploy-docs.yml +27 -0
  4. package/.github/workflows/pre-release-tests.yml +45 -0
  5. package/.github/workflows/pull-request-tests.yml +45 -0
  6. package/.prettierignore +2 -0
  7. package/.prettierrc +19 -0
  8. package/.vscode/launch.json +24 -0
  9. package/.vscode/settings.json +3 -0
  10. package/codecov.yml +7 -0
  11. package/demos/.eslintrc.js +13 -0
  12. package/demos/ago-node-cli/README.md +29 -0
  13. package/demos/ago-node-cli/ago.js +33 -0
  14. package/demos/ago-node-cli/index.js +11 -0
  15. package/demos/ago-node-cli/lib/item-export-command.js +48 -0
  16. package/demos/ago-node-cli/lib/item-search-command.js +35 -0
  17. package/demos/ago-node-cli/package-lock.json +430 -0
  18. package/demos/ago-node-cli/package.json +30 -0
  19. package/demos/attachments/README.md +5 -0
  20. package/demos/attachments/index.html +165 -0
  21. package/demos/attachments/package-lock.json +543 -0
  22. package/demos/attachments/package.json +18 -0
  23. package/demos/batch-geocoder-node/NYC_Restaurant_Inspection_Results.csv +100 -0
  24. package/demos/batch-geocoder-node/README.md +15 -0
  25. package/demos/batch-geocoder-node/batch-geocode.js +115 -0
  26. package/demos/batch-geocoder-node/config-template.js +18 -0
  27. package/demos/batch-geocoder-node/package-lock.json +336 -0
  28. package/demos/batch-geocoder-node/package.json +37 -0
  29. package/demos/express/README.md +15 -0
  30. package/demos/express/config.json.template +3 -0
  31. package/demos/express/package-lock.json +1008 -0
  32. package/demos/express/package.json +18 -0
  33. package/demos/express/server.js +33 -0
  34. package/demos/feature-service-browser/README.md +6 -0
  35. package/demos/feature-service-browser/index.html +122 -0
  36. package/demos/feature-service-browser/package-lock.json +543 -0
  37. package/demos/feature-service-browser/package.json +18 -0
  38. package/demos/geocoder-browser/README.md +10 -0
  39. package/demos/geocoder-browser/config.js.template +1 -0
  40. package/demos/geocoder-browser/index.html +131 -0
  41. package/demos/geocoder-browser/package-lock.json +610 -0
  42. package/demos/geocoder-browser/package.json +19 -0
  43. package/demos/geocoder-browser/post-sign-in.html +25 -0
  44. package/demos/jsapi-integration/README.md +25 -0
  45. package/demos/jsapi-integration/config.js +6 -0
  46. package/demos/jsapi-integration/index.html +93 -0
  47. package/demos/jsapi-integration/package-lock.json +247 -0
  48. package/demos/jsapi-integration/package.json +19 -0
  49. package/demos/node-cli-item-management/README.md +10 -0
  50. package/demos/node-cli-item-management/index.js +238 -0
  51. package/demos/node-cli-item-management/package-lock.json +432 -0
  52. package/demos/node-cli-item-management/package.json +27 -0
  53. package/demos/node-cli-item-management/screenshot.png +0 -0
  54. package/demos/oauth2-browser/README.md +14 -0
  55. package/demos/oauth2-browser/authenticate.html +30 -0
  56. package/demos/oauth2-browser/config.js.template +6 -0
  57. package/demos/oauth2-browser/index.html +211 -0
  58. package/demos/oauth2-browser/logo.svg +4 -0
  59. package/demos/oauth2-browser/package-lock.json +474 -0
  60. package/demos/oauth2-browser/package.json +18 -0
  61. package/demos/oauth2-browser/style.css +36 -0
  62. package/demos/oauth2-browser-retry/README.md +25 -0
  63. package/demos/oauth2-browser-retry/authenticate.html +22 -0
  64. package/demos/oauth2-browser-retry/index.html +116 -0
  65. package/demos/oauth2-browser-retry/logo.svg +4 -0
  66. package/demos/stream-response-to-file/README.md +7 -0
  67. package/demos/stream-response-to-file/index.js +36 -0
  68. package/demos/stream-response-to-file/output/.gitkeep +0 -0
  69. package/demos/stream-response-to-file/package-lock.json +227 -0
  70. package/demos/stream-response-to-file/package.json +33 -0
  71. package/demos/tree-shaking-rollup/.babelrc +3 -0
  72. package/demos/tree-shaking-rollup/README.md +9 -0
  73. package/demos/tree-shaking-rollup/index.html +11 -0
  74. package/demos/tree-shaking-rollup/package-lock.json +5646 -0
  75. package/demos/tree-shaking-rollup/package.json +25 -0
  76. package/demos/tree-shaking-rollup/rollup.config.js +17 -0
  77. package/demos/tree-shaking-rollup/src/index.js +8 -0
  78. package/demos/tree-shaking-webpack/README.md +8 -0
  79. package/demos/tree-shaking-webpack/index.html +11 -0
  80. package/demos/tree-shaking-webpack/package-lock.json +11455 -0
  81. package/demos/tree-shaking-webpack/package.json +24 -0
  82. package/demos/tree-shaking-webpack/src/index.js +10 -0
  83. package/demos/tree-shaking-webpack/webpack.config.js +27 -0
  84. package/demos/vue/.env.example +11 -0
  85. package/demos/vue/.eslintrc.js +17 -0
  86. package/demos/vue/.postcssrc.js +5 -0
  87. package/demos/vue/README.md +17 -0
  88. package/demos/vue/babel.config.js +3 -0
  89. package/demos/vue/package-lock.json +28044 -0
  90. package/demos/vue/package.json +33 -0
  91. package/demos/vue/public/favicon.ico +0 -0
  92. package/demos/vue/public/index.html +24 -0
  93. package/demos/vue/src/assets/logo.svg +29 -0
  94. package/demos/vue/src/components/App.vue +305 -0
  95. package/demos/vue/src/components/Authenticate.vue +65 -0
  96. package/demos/vue/src/components/Loader.vue +230 -0
  97. package/demos/vue/src/main.js +92 -0
  98. package/demos/webmap-checker-sapper/.env.example +5 -0
  99. package/demos/webmap-checker-sapper/README.md +123 -0
  100. package/demos/webmap-checker-sapper/appveyor.yml +18 -0
  101. package/demos/webmap-checker-sapper/cypress/fixtures/example.json +5 -0
  102. package/demos/webmap-checker-sapper/cypress/integration/spec.js +19 -0
  103. package/demos/webmap-checker-sapper/cypress/plugins/index.js +17 -0
  104. package/demos/webmap-checker-sapper/cypress/support/commands.js +25 -0
  105. package/demos/webmap-checker-sapper/cypress/support/index.js +20 -0
  106. package/demos/webmap-checker-sapper/cypress.json +4 -0
  107. package/demos/webmap-checker-sapper/package-lock.json +9622 -0
  108. package/demos/webmap-checker-sapper/package.json +50 -0
  109. package/demos/webmap-checker-sapper/rollup.config.js +87 -0
  110. package/demos/webmap-checker-sapper/src/client.js +20 -0
  111. package/demos/webmap-checker-sapper/src/components/LayerStatus.html +108 -0
  112. package/demos/webmap-checker-sapper/src/components/Nav.html +21 -0
  113. package/demos/webmap-checker-sapper/src/components/WebMap.html +62 -0
  114. package/demos/webmap-checker-sapper/src/routes/_error.html +41 -0
  115. package/demos/webmap-checker-sapper/src/routes/_layout.html +21 -0
  116. package/demos/webmap-checker-sapper/src/routes/auth/authorize.js +18 -0
  117. package/demos/webmap-checker-sapper/src/routes/auth/exchange-token.js +20 -0
  118. package/demos/webmap-checker-sapper/src/routes/auth/post-sign-in.js +24 -0
  119. package/demos/webmap-checker-sapper/src/routes/auth/sign-out.js +10 -0
  120. package/demos/webmap-checker-sapper/src/routes/index.html +20 -0
  121. package/demos/webmap-checker-sapper/src/routes/webmaps/[webmapId].html +83 -0
  122. package/demos/webmap-checker-sapper/src/routes/webmaps/index.html +59 -0
  123. package/demos/webmap-checker-sapper/src/server.js +101 -0
  124. package/demos/webmap-checker-sapper/src/service-worker.js +82 -0
  125. package/demos/webmap-checker-sapper/src/template.html +33 -0
  126. package/demos/webmap-checker-sapper/src/userInfoMiddleware.js +21 -0
  127. package/demos/webmap-checker-sapper/src/utils.js +33 -0
  128. package/demos/webmap-checker-sapper/static/favicon.png +0 -0
  129. package/demos/webmap-checker-sapper/static/global.css +36 -0
  130. package/demos/webmap-checker-sapper/static/manifest.json +20 -0
  131. package/demos/webmap-checker-sapper/static/svelte-logo-192.png +0 -0
  132. package/demos/webmap-checker-sapper/static/svelte-logo-512.png +0 -0
  133. package/docs/.eslintrc.js +12 -0
  134. package/docs/FAQ.md +48 -0
  135. package/docs/HISTORY.md +62 -0
  136. package/docs/acetate.config.js +262 -0
  137. package/docs/build-typedoc.js +434 -0
  138. package/docs/generate-srihashes.js +53 -0
  139. package/docs/src/_layout.html +86 -0
  140. package/docs/src/api/_declaration.html +600 -0
  141. package/docs/src/api/_layout.html +204 -0
  142. package/docs/src/api/_package.html +38 -0
  143. package/docs/src/api/index.html +16 -0
  144. package/docs/src/guides/_layout.html +24 -0
  145. package/docs/src/guides/amd-requirejs-dojo.md +40 -0
  146. package/docs/src/guides/browser-authentication.md +39 -0
  147. package/docs/src/guides/bundlers.md +52 -0
  148. package/docs/src/guides/cli-authentication.md +9 -0
  149. package/docs/src/guides/client-server-authentication.md +9 -0
  150. package/docs/src/guides/embedded-apps.md +106 -0
  151. package/docs/src/guides/from-a-cdn.md +38 -0
  152. package/docs/src/guides/index.md +59 -0
  153. package/docs/src/guides/node.md +104 -0
  154. package/docs/src/guides/package-overview.md +111 -0
  155. package/docs/src/guides/server-authentication.md +9 -0
  156. package/docs/src/guides/whats-new-v2-0.md +305 -0
  157. package/docs/src/img/icons.png +0 -0
  158. package/docs/src/img/icons@2x.png +0 -0
  159. package/docs/src/img/oauth-browser.png +0 -0
  160. package/docs/src/index.html +12 -0
  161. package/docs/src/js/api-search.js +112 -0
  162. package/docs/src/js/nav-toggle.js +41 -0
  163. package/docs/src/sass/_highlight.scss +96 -0
  164. package/docs/src/sass/_icons.scss +157 -0
  165. package/docs/src/sass/style.scss +242 -0
  166. package/docs/src/srihashes.json +12 -0
  167. package/jasmine.json +7 -0
  168. package/jasmine.live.json +7 -0
  169. package/karma.conf.js +107 -0
  170. package/lerna.json +8 -0
  171. package/notes/README.md +68 -0
  172. package/package.json +141 -0
  173. package/packages/arcgis-rest-auth/README.md +71 -0
  174. package/packages/arcgis-rest-auth/package.json +69 -0
  175. package/packages/arcgis-rest-auth/post-message-auth-spec.md +70 -0
  176. package/packages/arcgis-rest-auth/src/ApiKey.ts +41 -0
  177. package/packages/arcgis-rest-auth/src/ApplicationSession.ts +122 -0
  178. package/packages/arcgis-rest-auth/src/UserSession.ts +1355 -0
  179. package/packages/arcgis-rest-auth/src/app-tokens.ts +131 -0
  180. package/packages/arcgis-rest-auth/src/authenticated-request-options.ts +24 -0
  181. package/packages/arcgis-rest-auth/src/federation-utils.ts +85 -0
  182. package/packages/arcgis-rest-auth/src/fetch-token.ts +50 -0
  183. package/packages/arcgis-rest-auth/src/generate-token.ts +35 -0
  184. package/packages/arcgis-rest-auth/src/index.ts +13 -0
  185. package/packages/arcgis-rest-auth/src/validate-app-access.ts +68 -0
  186. package/packages/arcgis-rest-auth/test/ApiKey.test.ts +35 -0
  187. package/packages/arcgis-rest-auth/test/ApplicationSession.test.ts +124 -0
  188. package/packages/arcgis-rest-auth/test/UserSession.test.ts +2377 -0
  189. package/packages/arcgis-rest-auth/test/app-tokens.test.ts +95 -0
  190. package/packages/arcgis-rest-auth/test/federation-utils.test.ts +323 -0
  191. package/packages/arcgis-rest-auth/test/fetchToken.test.ts +112 -0
  192. package/packages/arcgis-rest-auth/test/generateToken.test.ts +102 -0
  193. package/packages/arcgis-rest-auth/test/utils.ts +14 -0
  194. package/packages/arcgis-rest-auth/test/validate-app-access.test.ts +46 -0
  195. package/packages/arcgis-rest-auth/tsconfig.json +6 -0
  196. package/packages/arcgis-rest-demographics/README.md +75 -0
  197. package/packages/arcgis-rest-demographics/package.json +69 -0
  198. package/packages/arcgis-rest-demographics/src/getAvailableCountries.ts +113 -0
  199. package/packages/arcgis-rest-demographics/src/getAvailableDataCollections.ts +166 -0
  200. package/packages/arcgis-rest-demographics/src/getAvailableGeographyLevels.ts +88 -0
  201. package/packages/arcgis-rest-demographics/src/getGeography.ts +152 -0
  202. package/packages/arcgis-rest-demographics/src/helpers.ts +28 -0
  203. package/packages/arcgis-rest-demographics/src/index.ts +8 -0
  204. package/packages/arcgis-rest-demographics/src/queryDemographicData.ts +106 -0
  205. package/packages/arcgis-rest-demographics/test/getAvailableCountries.test.ts +92 -0
  206. package/packages/arcgis-rest-demographics/test/getAvailableDataCollections.test.ts +115 -0
  207. package/packages/arcgis-rest-demographics/test/getAvailableGeographyLevels.test.ts +72 -0
  208. package/packages/arcgis-rest-demographics/test/getGeography.test.ts +141 -0
  209. package/packages/arcgis-rest-demographics/test/mocks/responses.ts +4 -0
  210. package/packages/arcgis-rest-demographics/test/queryDemographicData.test.live.ts +42 -0
  211. package/packages/arcgis-rest-demographics/test/queryDemographicData.test.ts +113 -0
  212. package/packages/arcgis-rest-demographics/tsconfig.json +6 -0
  213. package/packages/arcgis-rest-feature-layer/README.md +77 -0
  214. package/packages/arcgis-rest-feature-layer/package.json +64 -0
  215. package/packages/arcgis-rest-feature-layer/src/add.ts +56 -0
  216. package/packages/arcgis-rest-feature-layer/src/addAttachment.ts +53 -0
  217. package/packages/arcgis-rest-feature-layer/src/applyEdits.ts +95 -0
  218. package/packages/arcgis-rest-feature-layer/src/decodeValues.ts +122 -0
  219. package/packages/arcgis-rest-feature-layer/src/delete.ts +61 -0
  220. package/packages/arcgis-rest-feature-layer/src/deleteAttachments.ts +52 -0
  221. package/packages/arcgis-rest-feature-layer/src/getAllLayersAndTables.ts +30 -0
  222. package/packages/arcgis-rest-feature-layer/src/getAttachments.ts +55 -0
  223. package/packages/arcgis-rest-feature-layer/src/getLayer.ts +24 -0
  224. package/packages/arcgis-rest-feature-layer/src/getService.ts +26 -0
  225. package/packages/arcgis-rest-feature-layer/src/helpers.ts +97 -0
  226. package/packages/arcgis-rest-feature-layer/src/index.ts +32 -0
  227. package/packages/arcgis-rest-feature-layer/src/query.ts +204 -0
  228. package/packages/arcgis-rest-feature-layer/src/queryRelated.ts +89 -0
  229. package/packages/arcgis-rest-feature-layer/src/update.ts +60 -0
  230. package/packages/arcgis-rest-feature-layer/src/updateAttachment.ts +59 -0
  231. package/packages/arcgis-rest-feature-layer/test/attachments.test.ts +200 -0
  232. package/packages/arcgis-rest-feature-layer/test/crud.test.ts +197 -0
  233. package/packages/arcgis-rest-feature-layer/test/decodeValues.test.ts +67 -0
  234. package/packages/arcgis-rest-feature-layer/test/getAllLayersAndTables.test.ts +28 -0
  235. package/packages/arcgis-rest-feature-layer/test/getLayer.test.ts +31 -0
  236. package/packages/arcgis-rest-feature-layer/test/getService.test.ts +31 -0
  237. package/packages/arcgis-rest-feature-layer/test/helpers.test.ts +25 -0
  238. package/packages/arcgis-rest-feature-layer/test/mocks/allLayersAndTablesResponse.ts +906 -0
  239. package/packages/arcgis-rest-feature-layer/test/mocks/cvdQueryResponse.ts +225 -0
  240. package/packages/arcgis-rest-feature-layer/test/mocks/feature.ts +302 -0
  241. package/packages/arcgis-rest-feature-layer/test/mocks/fields.ts +779 -0
  242. package/packages/arcgis-rest-feature-layer/test/mocks/foo.txt +1 -0
  243. package/packages/arcgis-rest-feature-layer/test/mocks/service.ts +398 -0
  244. package/packages/arcgis-rest-feature-layer/test/query.test.ts +167 -0
  245. package/packages/arcgis-rest-feature-layer/tsconfig.json +6 -0
  246. package/packages/arcgis-rest-geocoding/README.md +86 -0
  247. package/packages/arcgis-rest-geocoding/package.json +66 -0
  248. package/packages/arcgis-rest-geocoding/src/bulk.ts +104 -0
  249. package/packages/arcgis-rest-geocoding/src/geocode.ts +166 -0
  250. package/packages/arcgis-rest-geocoding/src/helpers.ts +56 -0
  251. package/packages/arcgis-rest-geocoding/src/index.ts +15 -0
  252. package/packages/arcgis-rest-geocoding/src/reverse.ts +84 -0
  253. package/packages/arcgis-rest-geocoding/src/suggest.ts +45 -0
  254. package/packages/arcgis-rest-geocoding/test/bulk.test.ts +194 -0
  255. package/packages/arcgis-rest-geocoding/test/geocode.test.ts +253 -0
  256. package/packages/arcgis-rest-geocoding/test/helpers.test.ts +85 -0
  257. package/packages/arcgis-rest-geocoding/test/mocks/responses.ts +591 -0
  258. package/packages/arcgis-rest-geocoding/test/reverse.test.ts +126 -0
  259. package/packages/arcgis-rest-geocoding/test/suggest.test.ts +53 -0
  260. package/packages/arcgis-rest-geocoding/tsconfig.json +6 -0
  261. package/packages/arcgis-rest-portal/README.md +73 -0
  262. package/packages/arcgis-rest-portal/package.json +64 -0
  263. package/packages/arcgis-rest-portal/src/groups/add-users.ts +140 -0
  264. package/packages/arcgis-rest-portal/src/groups/create.ts +43 -0
  265. package/packages/arcgis-rest-portal/src/groups/get.ts +184 -0
  266. package/packages/arcgis-rest-portal/src/groups/helpers.ts +14 -0
  267. package/packages/arcgis-rest-portal/src/groups/invite-users.ts +127 -0
  268. package/packages/arcgis-rest-portal/src/groups/join.ts +57 -0
  269. package/packages/arcgis-rest-portal/src/groups/notification.ts +77 -0
  270. package/packages/arcgis-rest-portal/src/groups/protect.ts +56 -0
  271. package/packages/arcgis-rest-portal/src/groups/remove-users.ts +76 -0
  272. package/packages/arcgis-rest-portal/src/groups/remove.ts +32 -0
  273. package/packages/arcgis-rest-portal/src/groups/search.ts +47 -0
  274. package/packages/arcgis-rest-portal/src/groups/update-user-membership.ts +63 -0
  275. package/packages/arcgis-rest-portal/src/groups/update.ts +39 -0
  276. package/packages/arcgis-rest-portal/src/index.ts +69 -0
  277. package/packages/arcgis-rest-portal/src/items/add.ts +138 -0
  278. package/packages/arcgis-rest-portal/src/items/content.ts +67 -0
  279. package/packages/arcgis-rest-portal/src/items/create.ts +150 -0
  280. package/packages/arcgis-rest-portal/src/items/export.ts +80 -0
  281. package/packages/arcgis-rest-portal/src/items/get.ts +437 -0
  282. package/packages/arcgis-rest-portal/src/items/helpers.ts +292 -0
  283. package/packages/arcgis-rest-portal/src/items/protect.ts +41 -0
  284. package/packages/arcgis-rest-portal/src/items/reassign.ts +61 -0
  285. package/packages/arcgis-rest-portal/src/items/remove.ts +141 -0
  286. package/packages/arcgis-rest-portal/src/items/search.ts +25 -0
  287. package/packages/arcgis-rest-portal/src/items/update.ts +185 -0
  288. package/packages/arcgis-rest-portal/src/items/upload.ts +125 -0
  289. package/packages/arcgis-rest-portal/src/orgs/notification.ts +131 -0
  290. package/packages/arcgis-rest-portal/src/services/get-unique-service-name.ts +35 -0
  291. package/packages/arcgis-rest-portal/src/services/is-service-name-available.ts +30 -0
  292. package/packages/arcgis-rest-portal/src/sharing/access.ts +84 -0
  293. package/packages/arcgis-rest-portal/src/sharing/helpers.ts +81 -0
  294. package/packages/arcgis-rest-portal/src/sharing/is-item-shared-with-group.ts +42 -0
  295. package/packages/arcgis-rest-portal/src/sharing/share-item-with-group.ts +336 -0
  296. package/packages/arcgis-rest-portal/src/sharing/unshare-item-with-group.ts +105 -0
  297. package/packages/arcgis-rest-portal/src/users/get-user-tags.ts +52 -0
  298. package/packages/arcgis-rest-portal/src/users/get-user-url.ts +18 -0
  299. package/packages/arcgis-rest-portal/src/users/get-user.ts +58 -0
  300. package/packages/arcgis-rest-portal/src/users/invitation.ts +156 -0
  301. package/packages/arcgis-rest-portal/src/users/notification.ts +68 -0
  302. package/packages/arcgis-rest-portal/src/users/search-users.ts +37 -0
  303. package/packages/arcgis-rest-portal/src/users/update.ts +66 -0
  304. package/packages/arcgis-rest-portal/src/util/SearchQueryBuilder.ts +391 -0
  305. package/packages/arcgis-rest-portal/src/util/array.ts +16 -0
  306. package/packages/arcgis-rest-portal/src/util/generic-search.ts +99 -0
  307. package/packages/arcgis-rest-portal/src/util/get-portal-settings.ts +45 -0
  308. package/packages/arcgis-rest-portal/src/util/get-portal-url.ts +28 -0
  309. package/packages/arcgis-rest-portal/src/util/get-portal.ts +53 -0
  310. package/packages/arcgis-rest-portal/src/util/get-subscription-info.ts +43 -0
  311. package/packages/arcgis-rest-portal/src/util/scrub-control-chars.ts +13 -0
  312. package/packages/arcgis-rest-portal/src/util/search.ts +42 -0
  313. package/packages/arcgis-rest-portal/test/groups/add-users.test.ts +239 -0
  314. package/packages/arcgis-rest-portal/test/groups/crud.test.ts +180 -0
  315. package/packages/arcgis-rest-portal/test/groups/get.test.ts +176 -0
  316. package/packages/arcgis-rest-portal/test/groups/invite-users.test.ts +146 -0
  317. package/packages/arcgis-rest-portal/test/groups/join.test.ts +72 -0
  318. package/packages/arcgis-rest-portal/test/groups/notification.test.ts +112 -0
  319. package/packages/arcgis-rest-portal/test/groups/protect.test.ts +72 -0
  320. package/packages/arcgis-rest-portal/test/groups/remove-users.test.ts +140 -0
  321. package/packages/arcgis-rest-portal/test/groups/search.test.ts +151 -0
  322. package/packages/arcgis-rest-portal/test/groups/update-user-membership.test.ts +62 -0
  323. package/packages/arcgis-rest-portal/test/items/add.test.ts +323 -0
  324. package/packages/arcgis-rest-portal/test/items/content.test.ts +156 -0
  325. package/packages/arcgis-rest-portal/test/items/create.test.ts +400 -0
  326. package/packages/arcgis-rest-portal/test/items/export.test.ts +122 -0
  327. package/packages/arcgis-rest-portal/test/items/get.test.ts +583 -0
  328. package/packages/arcgis-rest-portal/test/items/helpers.test.ts +60 -0
  329. package/packages/arcgis-rest-portal/test/items/protect.test.ts +122 -0
  330. package/packages/arcgis-rest-portal/test/items/reassign.test.ts +131 -0
  331. package/packages/arcgis-rest-portal/test/items/remove.test.ts +261 -0
  332. package/packages/arcgis-rest-portal/test/items/search.test.ts +275 -0
  333. package/packages/arcgis-rest-portal/test/items/update.test.ts +556 -0
  334. package/packages/arcgis-rest-portal/test/items/upload.test.ts +282 -0
  335. package/packages/arcgis-rest-portal/test/mocks/groups/responses.ts +208 -0
  336. package/packages/arcgis-rest-portal/test/mocks/items/foo.zip +0 -0
  337. package/packages/arcgis-rest-portal/test/mocks/items/item.ts +526 -0
  338. package/packages/arcgis-rest-portal/test/mocks/items/resources.ts +38 -0
  339. package/packages/arcgis-rest-portal/test/mocks/items/search.ts +121 -0
  340. package/packages/arcgis-rest-portal/test/mocks/portal/response.ts +126 -0
  341. package/packages/arcgis-rest-portal/test/mocks/portal/settings-response.ts +56 -0
  342. package/packages/arcgis-rest-portal/test/mocks/sharing/sharing.ts +18 -0
  343. package/packages/arcgis-rest-portal/test/mocks/users/invitation.ts +70 -0
  344. package/packages/arcgis-rest-portal/test/mocks/users/notification.ts +34 -0
  345. package/packages/arcgis-rest-portal/test/mocks/users/user-search.ts +388 -0
  346. package/packages/arcgis-rest-portal/test/mocks/users/user-tags.ts +5 -0
  347. package/packages/arcgis-rest-portal/test/mocks/users/user.ts +174 -0
  348. package/packages/arcgis-rest-portal/test/orgs/notification.test.ts +144 -0
  349. package/packages/arcgis-rest-portal/test/services/get-unique-service-name.test.ts +59 -0
  350. package/packages/arcgis-rest-portal/test/services/is-service-name-available.test.ts +46 -0
  351. package/packages/arcgis-rest-portal/test/sharing/access.test.ts +162 -0
  352. package/packages/arcgis-rest-portal/test/sharing/helpers.test.ts +55 -0
  353. package/packages/arcgis-rest-portal/test/sharing/share-item-with-group.test.ts +1328 -0
  354. package/packages/arcgis-rest-portal/test/sharing/unshare-item-with-group.test.ts +288 -0
  355. package/packages/arcgis-rest-portal/test/users/get-user-tags.test.ts +71 -0
  356. package/packages/arcgis-rest-portal/test/users/get-user-url.test.ts +40 -0
  357. package/packages/arcgis-rest-portal/test/users/get-user.test.ts +90 -0
  358. package/packages/arcgis-rest-portal/test/users/invitation.test.ts +127 -0
  359. package/packages/arcgis-rest-portal/test/users/notification.test.ts +77 -0
  360. package/packages/arcgis-rest-portal/test/users/search.test.ts +42 -0
  361. package/packages/arcgis-rest-portal/test/users/update.test.ts +151 -0
  362. package/packages/arcgis-rest-portal/test/util/SearchQueryBuilder.test.ts +340 -0
  363. package/packages/arcgis-rest-portal/test/util/array.test.ts +30 -0
  364. package/packages/arcgis-rest-portal/test/util/get-portal-settings.test.ts +68 -0
  365. package/packages/arcgis-rest-portal/test/util/get-portal-url.test.ts +37 -0
  366. package/packages/arcgis-rest-portal/test/util/portal.test.ts +148 -0
  367. package/packages/arcgis-rest-portal/test/util/scrub-control-chars.test.ts +22 -0
  368. package/packages/arcgis-rest-portal/tsconfig.json +6 -0
  369. package/packages/arcgis-rest-request/README.md +72 -0
  370. package/packages/arcgis-rest-request/package.json +60 -0
  371. package/packages/arcgis-rest-request/src/index.ts +25 -0
  372. package/packages/arcgis-rest-request/src/request.ts +433 -0
  373. package/packages/arcgis-rest-request/src/utils/ArcGISRequestError.ts +76 -0
  374. package/packages/arcgis-rest-request/src/utils/ErrorTypes.ts +29 -0
  375. package/packages/arcgis-rest-request/src/utils/GrantTypes.ts +5 -0
  376. package/packages/arcgis-rest-request/src/utils/HTTPMethods.ts +6 -0
  377. package/packages/arcgis-rest-request/src/utils/IAuthenticationManager.ts +22 -0
  378. package/packages/arcgis-rest-request/src/utils/IFetchTokenParams.ts +11 -0
  379. package/packages/arcgis-rest-request/src/utils/IGenerateTokenParams.ts +9 -0
  380. package/packages/arcgis-rest-request/src/utils/IParamBuilder.ts +3 -0
  381. package/packages/arcgis-rest-request/src/utils/IParams.ts +6 -0
  382. package/packages/arcgis-rest-request/src/utils/IParamsBuilder.ts +5 -0
  383. package/packages/arcgis-rest-request/src/utils/IRequestOptions.ts +54 -0
  384. package/packages/arcgis-rest-request/src/utils/ITokenRequestOptions.ts +9 -0
  385. package/packages/arcgis-rest-request/src/utils/ResponseFormats.ts +10 -0
  386. package/packages/arcgis-rest-request/src/utils/append-custom-params.ts +49 -0
  387. package/packages/arcgis-rest-request/src/utils/clean-url.ts +20 -0
  388. package/packages/arcgis-rest-request/src/utils/decode-query-string.ts +27 -0
  389. package/packages/arcgis-rest-request/src/utils/encode-form-data.ts +38 -0
  390. package/packages/arcgis-rest-request/src/utils/encode-query-string.ts +35 -0
  391. package/packages/arcgis-rest-request/src/utils/process-params.ts +109 -0
  392. package/packages/arcgis-rest-request/src/utils/retryAuthError.ts +10 -0
  393. package/packages/arcgis-rest-request/src/utils/warn.ts +11 -0
  394. package/packages/arcgis-rest-request/src/utils/with-options.ts +48 -0
  395. package/packages/arcgis-rest-request/test/mocks/errors.ts +76 -0
  396. package/packages/arcgis-rest-request/test/mocks/geojson-feature-collection.ts +13 -0
  397. package/packages/arcgis-rest-request/test/mocks/param-builder.ts +7 -0
  398. package/packages/arcgis-rest-request/test/mocks/sharing-rest-info.ts +41 -0
  399. package/packages/arcgis-rest-request/test/mocks/webmap.ts +41 -0
  400. package/packages/arcgis-rest-request/test/request.test.ts +621 -0
  401. package/packages/arcgis-rest-request/test/utils/ArcGISAuthError.test.ts +191 -0
  402. package/packages/arcgis-rest-request/test/utils/ArcGISRequestError.test.ts +51 -0
  403. package/packages/arcgis-rest-request/test/utils/check-for-errors.test.ts +111 -0
  404. package/packages/arcgis-rest-request/test/utils/clean-url.test.ts +50 -0
  405. package/packages/arcgis-rest-request/test/utils/encode-form-data.test.ts +133 -0
  406. package/packages/arcgis-rest-request/test/utils/encode-query-string.test.ts +18 -0
  407. package/packages/arcgis-rest-request/test/utils/process-params.test.ts +205 -0
  408. package/packages/arcgis-rest-request/test/utils/with-options.test.ts +133 -0
  409. package/packages/arcgis-rest-request/tsconfig.json +4 -0
  410. package/packages/arcgis-rest-routing/README.md +75 -0
  411. package/packages/arcgis-rest-routing/package.json +65 -0
  412. package/packages/arcgis-rest-routing/src/closestFacility.ts +225 -0
  413. package/packages/arcgis-rest-routing/src/helpers.ts +104 -0
  414. package/packages/arcgis-rest-routing/src/index.ts +14 -0
  415. package/packages/arcgis-rest-routing/src/originDestinationMatrix.ts +223 -0
  416. package/packages/arcgis-rest-routing/src/serviceArea.ts +173 -0
  417. package/packages/arcgis-rest-routing/src/solveRoute.ts +180 -0
  418. package/packages/arcgis-rest-routing/test/closestFacility.test.ts +683 -0
  419. package/packages/arcgis-rest-routing/test/mocks/inputs.ts +132 -0
  420. package/packages/arcgis-rest-routing/test/mocks/responses.ts +13316 -0
  421. package/packages/arcgis-rest-routing/test/originDestinationMatrix.test.ts +795 -0
  422. package/packages/arcgis-rest-routing/test/serviceArea.test.ts +601 -0
  423. package/packages/arcgis-rest-routing/test/solveRoute.test.ts +677 -0
  424. package/packages/arcgis-rest-routing/tsconfig.json +6 -0
  425. package/packages/arcgis-rest-service-admin/README.md +73 -0
  426. package/packages/arcgis-rest-service-admin/package.json +65 -0
  427. package/packages/arcgis-rest-service-admin/src/addTo.ts +70 -0
  428. package/packages/arcgis-rest-service-admin/src/create.ts +189 -0
  429. package/packages/arcgis-rest-service-admin/src/get-service-admin-info.ts +34 -0
  430. package/packages/arcgis-rest-service-admin/src/get-view-sources.ts +20 -0
  431. package/packages/arcgis-rest-service-admin/src/index.ts +14 -0
  432. package/packages/arcgis-rest-service-admin/src/update.ts +50 -0
  433. package/packages/arcgis-rest-service-admin/test/addTo.test.ts +350 -0
  434. package/packages/arcgis-rest-service-admin/test/create.test.ts +294 -0
  435. package/packages/arcgis-rest-service-admin/test/get-service-admin-info.test.ts +37 -0
  436. package/packages/arcgis-rest-service-admin/test/get-view-sources.test.ts +40 -0
  437. package/packages/arcgis-rest-service-admin/test/mocks/layerDefinition.ts +79 -0
  438. package/packages/arcgis-rest-service-admin/test/mocks/service.ts +81 -0
  439. package/packages/arcgis-rest-service-admin/test/update.test.ts +115 -0
  440. package/packages/arcgis-rest-service-admin/tsconfig.json +5 -0
  441. package/packages/arcgis-rest-types/README.md +66 -0
  442. package/packages/arcgis-rest-types/package.json +54 -0
  443. package/packages/arcgis-rest-types/src/feature.ts +42 -0
  444. package/packages/arcgis-rest-types/src/geometry.ts +272 -0
  445. package/packages/arcgis-rest-types/src/group.ts +72 -0
  446. package/packages/arcgis-rest-types/src/index.ts +9 -0
  447. package/packages/arcgis-rest-types/src/item.ts +81 -0
  448. package/packages/arcgis-rest-types/src/service.ts +156 -0
  449. package/packages/arcgis-rest-types/src/statisticDefinition.ts +33 -0
  450. package/packages/arcgis-rest-types/src/symbol.ts +170 -0
  451. package/packages/arcgis-rest-types/src/user.ts +49 -0
  452. package/packages/arcgis-rest-types/src/webmap.ts +1405 -0
  453. package/packages/arcgis-rest-types/tsconfig.json +10 -0
  454. package/support/changelog.js +393 -0
  455. package/support/deploy-doc-site.js +16 -0
  456. package/support/dev.sh +6 -0
  457. package/support/publish.sh +47 -0
  458. package/support/test-helpers.js +9 -0
  459. package/tsconfig.json +63 -0
  460. package/umd-base-profile.js +81 -0
  461. package/umd-production-profile.js +13 -0
@@ -0,0 +1,2377 @@
1
+ /* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
2
+ * Apache-2.0 */
3
+
4
+ /* tslint:disable:no-empty */
5
+ import { UserSession } from "../src/index";
6
+ import { ICredential } from "../src/UserSession";
7
+
8
+ import {
9
+ request,
10
+ ArcGISRequestError,
11
+ ArcGISAuthError,
12
+ ErrorTypes,
13
+ } from "@esri/arcgis-rest-request";
14
+ import * as fetchMock from "fetch-mock";
15
+ import { YESTERDAY, TOMORROW } from "./utils";
16
+
17
+ describe("UserSession", () => {
18
+ afterEach(fetchMock.restore);
19
+
20
+ it("should serialize to and from JSON", () => {
21
+ const session = new UserSession({
22
+ clientId: "clientId",
23
+ redirectUri: "https://example-app.com/redirect-uri",
24
+ token: "token",
25
+ tokenExpires: TOMORROW,
26
+ refreshToken: "refreshToken",
27
+ refreshTokenExpires: TOMORROW,
28
+ refreshTokenTTL: 1440,
29
+ username: "c@sey",
30
+ password: "123456",
31
+ });
32
+
33
+ const session2 = UserSession.deserialize(session.serialize());
34
+
35
+ expect(session2.clientId).toEqual("clientId");
36
+ expect(session2.redirectUri).toEqual(
37
+ "https://example-app.com/redirect-uri"
38
+ );
39
+ expect(session2.ssl).toBe(undefined);
40
+ expect(session2.token).toEqual("token");
41
+ expect(session2.tokenExpires).toEqual(TOMORROW);
42
+ expect(session2.refreshToken).toEqual("refreshToken");
43
+ expect(session2.refreshTokenExpires).toEqual(TOMORROW);
44
+ expect(session2.username).toEqual("c@sey");
45
+ expect(session2.password).toEqual("123456");
46
+ expect(session2.tokenDuration).toEqual(20160);
47
+ expect(session2.refreshTokenTTL).toEqual(1440);
48
+ });
49
+
50
+ describe(".getToken()", () => {
51
+ it("should return unexpired tokens for trusted arcgis.com domains", (done) => {
52
+ const session = new UserSession({
53
+ clientId: "id",
54
+ token: "token",
55
+ tokenExpires: TOMORROW,
56
+ });
57
+
58
+ Promise.all([
59
+ session.getToken("https://www.arcgis.com/sharing/rest/portals/self"),
60
+ session.getToken(
61
+ "https://services1.arcgis.com/MOCK_ORG/arcgis/rest/services/Private_Service/FeatureServer"
62
+ ),
63
+ ])
64
+ .then(([token1, token2]) => {
65
+ expect(token1).toBe("token");
66
+ expect(token2).toBe("token");
67
+ done();
68
+ })
69
+ .catch((e) => {
70
+ fail(e);
71
+ });
72
+ });
73
+
74
+ it("should return unexpired tokens when an org url is passed", (done) => {
75
+ const session = new UserSession({
76
+ clientId: "id",
77
+ token: "token",
78
+ tokenExpires: TOMORROW,
79
+ portal: "https://custom.maps.arcgis.com/sharing/rest",
80
+ });
81
+
82
+ session
83
+ .getToken(
84
+ "https://services1.arcgis.com/MOCK_ORG/arcgis/rest/services/Private_Service/FeatureServer"
85
+ )
86
+ .then((token) => {
87
+ expect(token).toBe("token");
88
+ done();
89
+ })
90
+ .catch((e) => {
91
+ fail(e);
92
+ });
93
+ });
94
+
95
+ it("should return unexpired tokens when an org url is passed on other ArcGIS Online environments", (done) => {
96
+ const session = new UserSession({
97
+ clientId: "id",
98
+ token: "token",
99
+ tokenExpires: TOMORROW,
100
+ portal: "https://custom.mapsdev.arcgis.com/sharing/rest",
101
+ });
102
+
103
+ session
104
+ .getToken(
105
+ "https://services1dev.arcgis.com/MOCK_ORG/arcgis/rest/services/Private_Service/FeatureServer"
106
+ )
107
+ .then((token) => {
108
+ expect(token).toBe("token");
109
+ done();
110
+ })
111
+ .catch((e) => {
112
+ fail(e);
113
+ });
114
+ });
115
+
116
+ it("should return unexpired tokens when there is an http/https mismatch", (done) => {
117
+ const session = new UserSession({
118
+ clientId: "id",
119
+ token: "token",
120
+ tokenExpires: TOMORROW,
121
+ portal: "http://custom.mapsdev.arcgis.com/sharing/rest",
122
+ });
123
+
124
+ session
125
+ .getToken(
126
+ "https://services1dev.arcgis.com/MOCK_ORG/arcgis/rest/services/Private_Service/FeatureServer"
127
+ )
128
+ .then((token) => {
129
+ expect(token).toBe("token");
130
+ done();
131
+ })
132
+ .catch((e) => {
133
+ fail(e);
134
+ });
135
+ });
136
+
137
+ it("should return unexpired tokens for the configured portal domain", (done) => {
138
+ const session = new UserSession({
139
+ clientId: "id",
140
+ token: "token",
141
+ tokenExpires: TOMORROW,
142
+ portal: "https://gis.city.gov/sharing/rest",
143
+ });
144
+
145
+ session
146
+ .getToken("https://gis.city.gov/sharing/rest/portals/self")
147
+ .then((token) => {
148
+ expect(token).toBe("token");
149
+ done();
150
+ })
151
+ .catch((e) => {
152
+ fail(e);
153
+ });
154
+ });
155
+
156
+ it("should return unexpired tokens for the configured portal domain, regardless of CASING", (done) => {
157
+ // This was a real configuration discovered on a portal instance
158
+ const session = new UserSession({
159
+ clientId: "id",
160
+ token: "token",
161
+ tokenExpires: TOMORROW,
162
+ portal: "https://pnp00035.esri.com/sharing/rest",
163
+ });
164
+
165
+ session
166
+ .getToken("https://PNP00035.esri.com/sharing/rest/portals/self")
167
+ .then((token) => {
168
+ expect(token).toBe("token");
169
+ done();
170
+ })
171
+ .catch((e) => {
172
+ fail(e);
173
+ });
174
+ });
175
+
176
+ it("should fetch token when contacting a server that is federated, even if on same domain, regardless of domain casing", (done) => {
177
+ // This was a real configuration discovered on a portal instance
178
+ // apparently when federating servers, the UI does not force the
179
+ // server url to lowercase, and this any feature service items generated
180
+ // will have the server name using the casing the admin entered.
181
+ // this is just a test to ensure that the mis-matched casing does not
182
+ // break the federation flow.
183
+ const session = new UserSession({
184
+ clientId: "id",
185
+ token: "existing-session-token",
186
+ refreshToken: "refresh",
187
+ tokenExpires: TOMORROW,
188
+ portal: "https://pnp00035.esri.com/portal/sharing/rest",
189
+ });
190
+
191
+ fetchMock.postOnce("https://pnp00035.esri.com/server/rest/info", {
192
+ currentVersion: 10.61,
193
+ fullVersion: "10.6.1",
194
+ owningSystemUrl: "https://pnp00035.esri.com/portal",
195
+ authInfo: {
196
+ isTokenBasedSecurity: true,
197
+ tokenServicesUrl:
198
+ "https://pnp00035.esri.com/portal/sharing/rest/generateToken",
199
+ },
200
+ });
201
+
202
+ fetchMock.getOnce(
203
+ "https://pnp00035.esri.com/portal/sharing/rest/portals/self?f=json&token=existing-session-token",
204
+ {
205
+ authorizedCrossOriginDomains: [],
206
+ }
207
+ );
208
+
209
+ fetchMock.postOnce("https://pnp00035.esri.com/portal/sharing/rest/info", {
210
+ owningSystemUrl: "https://pnp00035.esri.com/portal",
211
+ authInfo: {
212
+ tokenServicesUrl:
213
+ "https://pnp00035.esri.com/portal/sharing/rest/generateToken",
214
+ isTokenBasedSecurity: true,
215
+ },
216
+ });
217
+
218
+ fetchMock.postOnce(
219
+ "https://pnp00035.esri.com/portal/sharing/rest/generateToken",
220
+ {
221
+ token: "new-server-token",
222
+ expires: TOMORROW,
223
+ }
224
+ );
225
+
226
+ // request the token twice, for the same domain, but with different casing
227
+ // and we expect a single POST to generate a token once
228
+ session
229
+ .getToken(
230
+ "https://PNP00035.esri.com/server/rest/services/Hosted/perimeters_dd83/FeatureServer"
231
+ )
232
+ .then((token) => {
233
+ expect(token).toBe("new-server-token");
234
+ return session.getToken(
235
+ "https://pnp00035.esri.com/server/rest/services/Hosted/otherService/FeatureServer"
236
+ );
237
+ })
238
+ .then((token) => {
239
+ expect(token).toBe("new-server-token");
240
+ done();
241
+ })
242
+ .catch((e) => {
243
+ fail(e);
244
+ });
245
+ });
246
+
247
+ it("should fetch new tokens when tokens for trusted arcgis.com domains are expired", (done) => {
248
+ const session = new UserSession({
249
+ clientId: "id",
250
+ token: "token",
251
+ refreshToken: "refresh",
252
+ tokenExpires: YESTERDAY,
253
+ });
254
+
255
+ fetchMock.mock(
256
+ "https://www.arcgis.com/sharing/rest/oauth2/token",
257
+ {
258
+ access_token: "new",
259
+ expires_in: 1800,
260
+ username: "c@sey",
261
+ },
262
+ { repeat: 2, method: "POST" }
263
+ );
264
+
265
+ Promise.all([
266
+ session.getToken("https://www.arcgis.com/sharing/rest/portals/self"),
267
+ session.getToken(
268
+ "https://services1.arcgis.com/MOCK_ORG/arcgis/rest/services/Private_Service/FeatureServer"
269
+ ),
270
+ ])
271
+ .then(([token1, token2]) => {
272
+ expect(token1).toBe("new");
273
+ expect(token2).toBe("new");
274
+ done();
275
+ })
276
+ .catch((e) => {
277
+ fail(e);
278
+ });
279
+ });
280
+
281
+ it("should pass through a token when no token expiration is present", (done) => {
282
+ const session = new UserSession({
283
+ token: "token",
284
+ });
285
+
286
+ session
287
+ .getToken("https://www.arcgis.com/sharing/rest/portals/self")
288
+ .then((token1) => {
289
+ expect(token1).toBe("token");
290
+ done();
291
+ })
292
+ .catch((e) => {
293
+ fail(e);
294
+ });
295
+ });
296
+
297
+ it("should generate a token for an untrusted, federated server", (done) => {
298
+ const session = new UserSession({
299
+ clientId: "id",
300
+ token: "token",
301
+ refreshToken: "refresh",
302
+ tokenExpires: TOMORROW,
303
+ portal: "https://gis.city.gov/sharing/rest",
304
+ });
305
+
306
+ fetchMock.postOnce("https://gisservices.city.gov/public/rest/info", {
307
+ currentVersion: 10.51,
308
+ fullVersion: "10.5.1.120",
309
+ owningSystemUrl: "https://gis.city.gov",
310
+ authInfo: {
311
+ isTokenBasedSecurity: true,
312
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
313
+ },
314
+ });
315
+
316
+ fetchMock.getOnce(
317
+ "https://gis.city.gov/sharing/rest/portals/self?f=json&token=token",
318
+ {
319
+ authorizedCrossOriginDomains: [],
320
+ }
321
+ );
322
+
323
+ fetchMock.postOnce("https://gis.city.gov/sharing/rest/info", {
324
+ owningSystemUrl: "http://gis.city.gov",
325
+ authInfo: {
326
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
327
+ isTokenBasedSecurity: true,
328
+ },
329
+ });
330
+
331
+ fetchMock.postOnce("https://gis.city.gov/sharing/generateToken", {
332
+ token: "serverToken",
333
+ expires: TOMORROW,
334
+ });
335
+
336
+ session
337
+ .getToken(
338
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
339
+ )
340
+ .then((token) => {
341
+ expect(token).toBe("serverToken");
342
+ return session.getToken(
343
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
344
+ );
345
+ })
346
+ .then((token) => {
347
+ expect(token).toBe("serverToken");
348
+ done();
349
+ })
350
+ .catch((e) => {
351
+ fail(e);
352
+ });
353
+ });
354
+
355
+ it("should generate a token for an untrusted, federated server admin call", (done) => {
356
+ const session = new UserSession({
357
+ clientId: "id",
358
+ token: "token",
359
+ refreshToken: "refresh",
360
+ tokenExpires: TOMORROW,
361
+ portal: "https://gis.city.gov/sharing/rest",
362
+ });
363
+
364
+ fetchMock.postOnce("https://gisservices.city.gov/public/rest/info", {
365
+ currentVersion: 10.51,
366
+ fullVersion: "10.5.1.120",
367
+ owningSystemUrl: "https://gis.city.gov",
368
+ authInfo: {
369
+ isTokenBasedSecurity: true,
370
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
371
+ },
372
+ });
373
+
374
+ fetchMock.getOnce(
375
+ "https://gis.city.gov/sharing/rest/portals/self?f=json&token=token",
376
+ {
377
+ authorizedCrossOriginDomains: [],
378
+ }
379
+ );
380
+
381
+ fetchMock.postOnce("https://gis.city.gov/sharing/rest/info", {
382
+ owningSystemUrl: "http://gis.city.gov",
383
+ authInfo: {
384
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
385
+ isTokenBasedSecurity: true,
386
+ },
387
+ });
388
+
389
+ fetchMock.postOnce("https://gis.city.gov/sharing/generateToken", {
390
+ token: "serverToken",
391
+ expires: TOMORROW,
392
+ });
393
+
394
+ session
395
+ .getToken(
396
+ "https://gisservices.city.gov/public/rest/admin/services/trees/FeatureServer/addToDefinition"
397
+ )
398
+ .then((token) => {
399
+ expect(token).toBe("serverToken");
400
+ return session.getToken(
401
+ "https://gisservices.city.gov/public/rest/admin/services/trees/FeatureServer/addToDefinition"
402
+ );
403
+ })
404
+ .then((token) => {
405
+ expect(token).toBe("serverToken");
406
+ done();
407
+ })
408
+ .catch((e) => {
409
+ fail(e);
410
+ });
411
+ });
412
+
413
+ it("should generate a token for an untrusted, federated server with user credentials", (done) => {
414
+ const session = new UserSession({
415
+ username: "c@sey",
416
+ password: "jones",
417
+ portal: "https://gis.city.gov/sharing/rest",
418
+ });
419
+
420
+ fetchMock.postOnce("https://gisservices.city.gov/public/rest/info", {
421
+ currentVersion: 10.51,
422
+ fullVersion: "10.5.1.120",
423
+ owningSystemUrl: "https://gis.city.gov",
424
+ authInfo: {
425
+ isTokenBasedSecurity: true,
426
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
427
+ },
428
+ });
429
+
430
+ fetchMock.postOnce("https://gis.city.gov/sharing/rest/generateToken", {
431
+ token: "portalToken",
432
+ });
433
+
434
+ fetchMock.getOnce(
435
+ "https://gis.city.gov/sharing/rest/portals/self?f=json&token=portalToken",
436
+ {
437
+ authorizedCrossOriginDomains: [],
438
+ }
439
+ );
440
+
441
+ fetchMock.postOnce("https://gis.city.gov/sharing/rest/info", {
442
+ owningSystemUrl: "http://gis.city.gov",
443
+ authInfo: {
444
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
445
+ isTokenBasedSecurity: true,
446
+ },
447
+ });
448
+
449
+ fetchMock.postOnce("https://gis.city.gov/sharing/generateToken", {
450
+ token: "serverToken",
451
+ expires: TOMORROW,
452
+ });
453
+
454
+ session
455
+ .getToken(
456
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
457
+ )
458
+ .then((token) => {
459
+ expect(token).toBe("serverToken");
460
+ done();
461
+ })
462
+ .catch((e) => {
463
+ fail(e);
464
+ });
465
+ });
466
+
467
+ it("should only make 1 token request to an untrusted portal for similar URLs", (done) => {
468
+ const session = new UserSession({
469
+ clientId: "id",
470
+ token: "token",
471
+ refreshToken: "refresh",
472
+ tokenExpires: TOMORROW,
473
+ portal: "https://gis.city.gov/sharing/rest",
474
+ });
475
+
476
+ fetchMock.mock(
477
+ "https://gisservices.city.gov/public/rest/info",
478
+ {
479
+ currentVersion: 10.51,
480
+ fullVersion: "10.5.1.120",
481
+ owningSystemUrl: "https://gis.city.gov",
482
+ authInfo: {
483
+ isTokenBasedSecurity: true,
484
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
485
+ },
486
+ },
487
+ { repeat: 1, method: "POST" }
488
+ );
489
+
490
+ fetchMock.getOnce(
491
+ "https://gis.city.gov/sharing/rest/portals/self?f=json&token=token",
492
+ {
493
+ authorizedCrossOriginDomains: [],
494
+ }
495
+ );
496
+
497
+ fetchMock.mock(
498
+ "https://gis.city.gov/sharing/rest/info",
499
+ {
500
+ owningSystemUrl: "http://gis.city.gov",
501
+ authInfo: {
502
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
503
+ isTokenBasedSecurity: true,
504
+ },
505
+ },
506
+ { repeat: 1, method: "POST" }
507
+ );
508
+
509
+ fetchMock.mock(
510
+ "https://gis.city.gov/sharing/generateToken",
511
+ {
512
+ token: "serverToken",
513
+ expires: TOMORROW,
514
+ },
515
+ { repeat: 1, method: "POST" }
516
+ );
517
+
518
+ Promise.all([
519
+ session.getToken(
520
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
521
+ ),
522
+ session.getToken(
523
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
524
+ ),
525
+ ])
526
+ .then(([token1, token2]) => {
527
+ expect(token1).toBe("serverToken");
528
+ expect(token2).toBe("serverToken");
529
+ expect(
530
+ fetchMock.calls("https://gis.city.gov/sharing/generateToken").length
531
+ ).toBe(1);
532
+ done();
533
+ })
534
+ .catch((e) => {
535
+ fail(e);
536
+ });
537
+ });
538
+
539
+ it("should throw an ArcGISAuthError when the owning system doesn't match", (done) => {
540
+ const session = new UserSession({
541
+ clientId: "id",
542
+ token: "token",
543
+ refreshToken: "refresh",
544
+ tokenExpires: YESTERDAY,
545
+ });
546
+
547
+ // similates refreshing the token with the refresh token
548
+ fetchMock.postOnce("https://www.arcgis.com/sharing/rest/oauth2/token", {
549
+ access_token: "newToken",
550
+ expires_in: 60,
551
+ username: " c@sey",
552
+ });
553
+
554
+ fetchMock.getOnce(
555
+ "https://www.arcgis.com/sharing/rest/portals/self?f=json&token=newToken",
556
+ {
557
+ authorizedCrossOriginDomains: [],
558
+ }
559
+ );
560
+
561
+ fetchMock.post("https://gisservices.city.gov/public/rest/info", {
562
+ currentVersion: 10.51,
563
+ fullVersion: "10.5.1.120",
564
+ owningSystemUrl: "https://gis.city.gov",
565
+ authInfo: {
566
+ isTokenBasedSecurity: true,
567
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
568
+ },
569
+ });
570
+
571
+ session
572
+ .getToken(
573
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
574
+ )
575
+ .catch((e) => {
576
+ expect(e.name).toEqual(ErrorTypes.ArcGISAuthError);
577
+ expect(e.code).toEqual("NOT_FEDERATED");
578
+ expect(e.message).toEqual(
579
+ "NOT_FEDERATED: https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query is not federated with https://www.arcgis.com/sharing/rest."
580
+ );
581
+ done();
582
+ });
583
+ });
584
+
585
+ it("should throw a fully hydrated ArcGISAuthError when no owning system is advertised", (done) => {
586
+ const session = new UserSession({
587
+ clientId: "id",
588
+ token: "token",
589
+ refreshToken: "refresh",
590
+ tokenExpires: YESTERDAY,
591
+ });
592
+
593
+ fetchMock.postOnce("https://www.arcgis.com/sharing/rest/oauth2/token", {
594
+ access_token: "newToken",
595
+ expires_in: 60,
596
+ username: " c@sey",
597
+ });
598
+
599
+ fetchMock.getOnce(
600
+ "https://www.arcgis.com/sharing/rest/portals/self?f=json&token=newToken",
601
+ {
602
+ authorizedCrossOriginDomains: [],
603
+ }
604
+ );
605
+
606
+ fetchMock.post("https://gisservices.city.gov/public/rest/info", {
607
+ currentVersion: 10.51,
608
+ fullVersion: "10.5.1.120",
609
+ });
610
+
611
+ fetchMock.post(
612
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query",
613
+ {
614
+ error: {
615
+ code: 499,
616
+ message: "Token Required",
617
+ details: [],
618
+ },
619
+ }
620
+ );
621
+
622
+ request(
623
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query",
624
+ {
625
+ authentication: session,
626
+ params: {
627
+ foo: "bar",
628
+ },
629
+ }
630
+ ).catch((e) => {
631
+ expect(e.name).toEqual(ErrorTypes.ArcGISAuthError);
632
+ expect(e.code).toEqual("NOT_FEDERATED");
633
+ expect(e.message).toEqual(
634
+ "NOT_FEDERATED: https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query is not federated with any portal and is not explicitly trusted."
635
+ );
636
+ expect(e.url).toEqual(
637
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
638
+ );
639
+ expect(e.options.params.foo).toEqual("bar");
640
+ done();
641
+ });
642
+ });
643
+
644
+ it("should not throw an ArcGISAuthError when the unfederated service is public", (done) => {
645
+ const session = new UserSession({
646
+ clientId: "id",
647
+ token: "token",
648
+ refreshToken: "refresh",
649
+ tokenExpires: YESTERDAY,
650
+ });
651
+
652
+ fetchMock.post("https://gisservices.city.gov/public/rest/info", {
653
+ currentVersion: 10.51,
654
+ fullVersion: "10.5.1.120",
655
+ });
656
+
657
+ fetchMock.postOnce("https://www.arcgis.com/sharing/rest/oauth2/token", {
658
+ access_token: "newToken",
659
+ expires_in: 60,
660
+ username: " c@sey",
661
+ });
662
+
663
+ fetchMock.getOnce(
664
+ "https://www.arcgis.com/sharing/rest/portals/self?f=json&token=newToken",
665
+ {
666
+ authorizedCrossOriginDomains: [],
667
+ }
668
+ );
669
+
670
+ fetchMock.post(
671
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query",
672
+ {
673
+ count: 123,
674
+ }
675
+ );
676
+
677
+ request(
678
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query",
679
+ {
680
+ authentication: session,
681
+ params: {
682
+ returnCount: true,
683
+ },
684
+ }
685
+ )
686
+ .then((res) => {
687
+ expect(res.count).toEqual(123);
688
+ done();
689
+ })
690
+ .catch((e) => {
691
+ fail(e);
692
+ });
693
+ });
694
+ });
695
+
696
+ describe(".refreshSession()", () => {
697
+ it("should refresh with a username and password if expired", (done) => {
698
+ const session = new UserSession({
699
+ username: "c@sey",
700
+ password: "123456",
701
+ });
702
+
703
+ fetchMock.postOnce("https://www.arcgis.com/sharing/rest/generateToken", {
704
+ token: "token",
705
+ expires: TOMORROW.getTime(),
706
+ username: " c@sey",
707
+ });
708
+
709
+ session
710
+ .refreshSession()
711
+ .then((s) => {
712
+ expect(s.token).toBe("token");
713
+ expect(s.tokenExpires).toEqual(TOMORROW);
714
+ done();
715
+ })
716
+ .catch((e) => {
717
+ fail(e);
718
+ });
719
+ });
720
+
721
+ it("should refresh with an unexpired refresh token", (done) => {
722
+ const session = new UserSession({
723
+ clientId: "clientId",
724
+ token: "token",
725
+ username: "c@sey",
726
+ refreshToken: "refreshToken",
727
+ refreshTokenExpires: TOMORROW,
728
+ });
729
+
730
+ fetchMock.postOnce("https://www.arcgis.com/sharing/rest/oauth2/token", {
731
+ access_token: "newToken",
732
+ expires_in: 60,
733
+ username: " c@sey",
734
+ });
735
+
736
+ session
737
+ .refreshSession()
738
+ .then((s) => {
739
+ expect(s.token).toBe("newToken");
740
+ expect(s.tokenExpires.getTime()).toBeGreaterThan(Date.now());
741
+ done();
742
+ })
743
+ .catch((e) => {
744
+ fail(e);
745
+ });
746
+ });
747
+
748
+ it("should refresh with an expired refresh token", (done) => {
749
+ const session = new UserSession({
750
+ clientId: "clientId",
751
+ token: "token",
752
+ username: "c@sey",
753
+ refreshToken: "refreshToken",
754
+ refreshTokenExpires: YESTERDAY,
755
+ redirectUri: "https://example-app.com/redirect-uri",
756
+ });
757
+
758
+ fetchMock.postOnce("https://www.arcgis.com/sharing/rest/oauth2/token", {
759
+ access_token: "newToken",
760
+ expires_in: 60,
761
+ username: " c@sey",
762
+ refresh_token: "newRefreshToken",
763
+ });
764
+
765
+ session
766
+ .refreshSession()
767
+ .then((s) => {
768
+ expect(s.token).toBe("newToken");
769
+ expect(s.tokenExpires.getTime()).toBeGreaterThan(Date.now());
770
+ expect(s.refreshToken).toBe("newRefreshToken");
771
+ expect(s.refreshTokenExpires.getTime()).toBeGreaterThan(Date.now());
772
+ done();
773
+ })
774
+ .catch((e) => {
775
+ fail(e);
776
+ });
777
+ });
778
+
779
+ it("should reject if we cannot refresh the token", (done) => {
780
+ const session = new UserSession({
781
+ clientId: "clientId",
782
+ token: "token",
783
+ username: "c@sey",
784
+ });
785
+
786
+ session.refreshSession().catch((e) => {
787
+ expect(e instanceof ArcGISAuthError).toBeTruthy();
788
+ expect(e.name).toBe("ArcGISAuthError");
789
+ expect(e.message).toBe("Unable to refresh token.");
790
+ done();
791
+ });
792
+ });
793
+
794
+ it("should only make 1 token request to the portal for similar URLs", (done) => {
795
+ const session = new UserSession({
796
+ clientId: "id",
797
+ token: "token",
798
+ refreshToken: "refresh",
799
+ tokenExpires: YESTERDAY,
800
+ });
801
+
802
+ fetchMock.mock(
803
+ "https://www.arcgis.com/sharing/rest/oauth2/token",
804
+ {
805
+ access_token: "new",
806
+ expires_in: 1800,
807
+ username: "c@sey",
808
+ },
809
+ { repeat: 1, method: "POST" }
810
+ );
811
+
812
+ Promise.all([
813
+ session.getToken("https://www.arcgis.com/sharing/rest/portals/self"),
814
+ session.getToken("https://www.arcgis.com/sharing/rest/portals/self"),
815
+ ])
816
+ .then(([token1, token2]) => {
817
+ expect(token1).toBe("new");
818
+ expect(token2).toBe("new");
819
+ expect(
820
+ fetchMock.calls("https://www.arcgis.com/sharing/rest/oauth2/token")
821
+ .length
822
+ ).toBe(1);
823
+ done();
824
+ })
825
+ .catch((e) => {
826
+ fail(e);
827
+ });
828
+ });
829
+ });
830
+
831
+ describe(".beginOAuth2()", () => {
832
+ it("should authorize via a popup", (done) => {
833
+ const MockWindow: any = {
834
+ open: jasmine.createSpy("spy"),
835
+ };
836
+
837
+ UserSession.beginOAuth2(
838
+ {
839
+ clientId: "clientId123",
840
+ redirectUri: "http://example-app.com/redirect",
841
+ state: "abc123",
842
+ },
843
+ MockWindow
844
+ )
845
+ .then((session) => {
846
+ expect(session.token).toBe("token");
847
+ expect(session.username).toBe("c@sey");
848
+ expect(session.ssl).toBe(true);
849
+ expect(session.tokenExpires).toEqual(TOMORROW);
850
+ done();
851
+ })
852
+ .catch((e) => {
853
+ fail(e);
854
+ });
855
+
856
+ expect(MockWindow.open).toHaveBeenCalledWith(
857
+ "https://www.arcgis.com/sharing/rest/oauth2/authorize?client_id=clientId123&response_type=token&expiration=20160&redirect_uri=http%3A%2F%2Fexample-app.com%2Fredirect&state=abc123&locale=",
858
+ "oauth-window",
859
+ "height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes"
860
+ );
861
+
862
+ MockWindow.__ESRI_REST_AUTH_HANDLER_clientId123(
863
+ JSON.stringify(undefined),
864
+ JSON.stringify({
865
+ token: "token",
866
+ expires: TOMORROW,
867
+ username: "c@sey",
868
+ ssl: true,
869
+ })
870
+ );
871
+ });
872
+
873
+ it("should reject the promise if there is an error", (done) => {
874
+ const MockWindow: any = {
875
+ open: jasmine.createSpy("spy"),
876
+ };
877
+
878
+ UserSession.beginOAuth2(
879
+ {
880
+ clientId: "clientId123",
881
+ redirectUri: "http://example-app.com/redirect",
882
+ locale: "fr",
883
+ },
884
+ MockWindow
885
+ ).catch((e) => {
886
+ done();
887
+ });
888
+
889
+ expect(MockWindow.open).toHaveBeenCalledWith(
890
+ "https://www.arcgis.com/sharing/rest/oauth2/authorize?client_id=clientId123&response_type=token&expiration=20160&redirect_uri=http%3A%2F%2Fexample-app.com%2Fredirect&state=clientId123&locale=fr",
891
+ "oauth-window",
892
+ "height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes"
893
+ );
894
+
895
+ MockWindow.__ESRI_REST_AUTH_HANDLER_clientId123(
896
+ JSON.stringify({
897
+ errorMessage: "unable to sign in",
898
+ error: "SIGN_IN_FAILED",
899
+ })
900
+ );
901
+ });
902
+
903
+ it("should authorize in the same window/tab", () => {
904
+ const MockWindow: any = {
905
+ location: {
906
+ href: "",
907
+ },
908
+ };
909
+
910
+ // https://github.com/palantir/tslint/issues/3056
911
+ void UserSession.beginOAuth2(
912
+ {
913
+ clientId: "clientId123",
914
+ redirectUri: "http://example-app.com/redirect",
915
+ popup: false,
916
+ },
917
+ MockWindow
918
+ );
919
+
920
+ expect(MockWindow.location.href).toBe(
921
+ "https://www.arcgis.com/sharing/rest/oauth2/authorize?client_id=clientId123&response_type=token&expiration=20160&redirect_uri=http%3A%2F%2Fexample-app.com%2Fredirect&state=clientId123&locale="
922
+ );
923
+ });
924
+
925
+ it("should authorize using a social media provider", () => {
926
+ const MockWindow: any = {
927
+ location: {
928
+ href: "",
929
+ },
930
+ };
931
+
932
+ // https://github.com/palantir/tslint/issues/3056
933
+ void UserSession.beginOAuth2(
934
+ {
935
+ clientId: "clientId123",
936
+ redirectUri: "http://example-app.com/redirect",
937
+ popup: false,
938
+ provider: "facebook",
939
+ },
940
+ MockWindow
941
+ );
942
+
943
+ expect(MockWindow.location.href).toBe(
944
+ "https://www.arcgis.com/sharing/rest/oauth2/social/authorize?client_id=clientId123&socialLoginProviderName=facebook&autoAccountCreateForSocial=true&response_type=token&expiration=20160&redirect_uri=http%3A%2F%2Fexample-app.com%2Fredirect&state=clientId123&locale="
945
+ );
946
+ });
947
+
948
+ it("should authorize using the other social media provider", () => {
949
+ const MockWindow: any = {
950
+ location: {
951
+ href: "",
952
+ },
953
+ };
954
+
955
+ // https://github.com/palantir/tslint/issues/3056
956
+ void UserSession.beginOAuth2(
957
+ {
958
+ clientId: "clientId123",
959
+ redirectUri: "http://example-app.com/redirect",
960
+ popup: false,
961
+ provider: "google",
962
+ },
963
+ MockWindow
964
+ );
965
+
966
+ expect(MockWindow.location.href).toBe(
967
+ "https://www.arcgis.com/sharing/rest/oauth2/social/authorize?client_id=clientId123&socialLoginProviderName=google&autoAccountCreateForSocial=true&response_type=token&expiration=20160&redirect_uri=http%3A%2F%2Fexample-app.com%2Fredirect&state=clientId123&locale="
968
+ );
969
+ });
970
+
971
+ it("should pass custom expiration", () => {
972
+ const MockWindow: any = {
973
+ location: {
974
+ href: "",
975
+ },
976
+ };
977
+
978
+ // https://github.com/palantir/tslint/issues/3056
979
+ void UserSession.beginOAuth2(
980
+ {
981
+ clientId: "clientId123",
982
+ redirectUri: "http://example-app.com/redirect",
983
+ popup: false,
984
+ expiration: 9000
985
+ },
986
+ MockWindow
987
+ );
988
+
989
+ expect(MockWindow.location.href).toBe(
990
+ "https://www.arcgis.com/sharing/rest/oauth2/authorize?client_id=clientId123&response_type=token&expiration=9000&redirect_uri=http%3A%2F%2Fexample-app.com%2Fredirect&state=clientId123&locale="
991
+ );
992
+ });
993
+
994
+ it("should pass custom duration (DEPRECATED)", () => {
995
+ const MockWindow: any = {
996
+ location: {
997
+ href: "",
998
+ },
999
+ };
1000
+
1001
+ // https://github.com/palantir/tslint/issues/3056
1002
+ void UserSession.beginOAuth2(
1003
+ {
1004
+ clientId: "clientId123",
1005
+ redirectUri: "http://example-app.com/redirect",
1006
+ popup: false,
1007
+ duration: 9001
1008
+ },
1009
+ MockWindow
1010
+ );
1011
+
1012
+ expect(MockWindow.location.href).toBe(
1013
+ "https://www.arcgis.com/sharing/rest/oauth2/authorize?client_id=clientId123&response_type=token&expiration=9001&redirect_uri=http%3A%2F%2Fexample-app.com%2Fredirect&state=clientId123&locale="
1014
+ );
1015
+ });
1016
+ });
1017
+
1018
+ describe(".completeOAuth2()", () => {
1019
+ it("should return a new user session if it cannot find a valid parent", () => {
1020
+ const MockWindow = {
1021
+ location: {
1022
+ hash:
1023
+ "#access_token=token&expires_in=1209600&username=c%40sey&ssl=true&persist=true",
1024
+ },
1025
+ get parent() {
1026
+ return this;
1027
+ },
1028
+ };
1029
+
1030
+ const session = UserSession.completeOAuth2(
1031
+ {
1032
+ clientId: "clientId",
1033
+ redirectUri: "https://example-app.com/redirect-uri",
1034
+ },
1035
+ MockWindow
1036
+ );
1037
+
1038
+ expect(session.token).toBe("token");
1039
+ expect(session.tokenExpires.getTime()).toBeGreaterThan(Date.now());
1040
+ expect(session.username).toBe("c@sey");
1041
+ expect(session.ssl).toBe(true);
1042
+ });
1043
+
1044
+ it("should return a new user session with ssl as false when callback hash does not have ssl parameter", () => {
1045
+ const MockWindow = {
1046
+ location: {
1047
+ hash:
1048
+ "#access_token=token&expires_in=1209600&username=c%40sey&persist=true",
1049
+ },
1050
+ get parent() {
1051
+ return this;
1052
+ },
1053
+ };
1054
+
1055
+ const session = UserSession.completeOAuth2(
1056
+ {
1057
+ clientId: "clientId",
1058
+ redirectUri: "https://example-app.com/redirect-uri",
1059
+ },
1060
+ MockWindow
1061
+ );
1062
+ expect(session.ssl).toBe(false);
1063
+ });
1064
+
1065
+ it("should callback to create a new user session if finds a valid opener.parent", (done) => {
1066
+ const MockWindow = {
1067
+ opener: {
1068
+ parent: {
1069
+ __ESRI_REST_AUTH_HANDLER_clientId(
1070
+ errorString: string,
1071
+ oauthInfoString: string
1072
+ ) {
1073
+ const oauthInfo = JSON.parse(oauthInfoString);
1074
+ expect(oauthInfo.token).toBe("token");
1075
+ expect(oauthInfo.username).toBe("c@sey");
1076
+ expect(oauthInfo.ssl).toBe(true);
1077
+ expect(new Date(oauthInfo.expires).getTime()).toBeGreaterThan(
1078
+ Date.now()
1079
+ );
1080
+ },
1081
+ },
1082
+ },
1083
+ close() {
1084
+ done();
1085
+ },
1086
+ location: {
1087
+ hash:
1088
+ "#access_token=token&expires_in=1209600&username=c%40sey&ssl=true",
1089
+ },
1090
+ };
1091
+
1092
+ UserSession.completeOAuth2(
1093
+ {
1094
+ clientId: "clientId",
1095
+ redirectUri: "https://example-app.com/redirect-uri",
1096
+ },
1097
+ MockWindow
1098
+ );
1099
+ });
1100
+
1101
+ it("should callback to create a new user session if finds a valid opener (Iframe support)", (done) => {
1102
+ const MockWindow = {
1103
+ opener: {
1104
+ __ESRI_REST_AUTH_HANDLER_clientId(
1105
+ errorString: string,
1106
+ oauthInfoString: string
1107
+ ) {
1108
+ const oauthInfo = JSON.parse(oauthInfoString);
1109
+ expect(oauthInfo.token).toBe("token");
1110
+ expect(oauthInfo.username).toBe("c@sey");
1111
+ expect(oauthInfo.ssl).toBe(true);
1112
+ expect(new Date(oauthInfo.expires).getTime()).toBeGreaterThan(
1113
+ Date.now()
1114
+ );
1115
+ },
1116
+ },
1117
+ close() {
1118
+ done();
1119
+ },
1120
+ location: {
1121
+ hash:
1122
+ "#access_token=token&expires_in=1209600&username=c%40sey&ssl=true",
1123
+ },
1124
+ };
1125
+
1126
+ UserSession.completeOAuth2(
1127
+ {
1128
+ clientId: "clientId",
1129
+ redirectUri: "https://example-app.com/redirect-uri",
1130
+ },
1131
+ MockWindow
1132
+ );
1133
+ });
1134
+
1135
+ it("should callback to create a new user session if finds a valid parent", (done) => {
1136
+ const MockWindow = {
1137
+ parent: {
1138
+ __ESRI_REST_AUTH_HANDLER_clientId(
1139
+ errorString: string,
1140
+ oauthInfoString: string
1141
+ ) {
1142
+ const oauthInfo = JSON.parse(oauthInfoString);
1143
+ expect(oauthInfo.token).toBe("token");
1144
+ expect(oauthInfo.username).toBe("c@sey");
1145
+ expect(oauthInfo.ssl).toBe(true);
1146
+ expect(new Date(oauthInfo.expires).getTime()).toBeGreaterThan(
1147
+ Date.now()
1148
+ );
1149
+ },
1150
+ },
1151
+ close() {
1152
+ done();
1153
+ },
1154
+ location: {
1155
+ hash:
1156
+ "#access_token=token&expires_in=1209600&username=c%40sey&ssl=true",
1157
+ },
1158
+ };
1159
+
1160
+ UserSession.completeOAuth2(
1161
+ {
1162
+ clientId: "clientId",
1163
+ redirectUri: "https://example-app.com/redirect-uri",
1164
+ },
1165
+ MockWindow
1166
+ );
1167
+ });
1168
+
1169
+ it("should throw an error from the authorization window", () => {
1170
+ const MockWindow = {
1171
+ location: {
1172
+ hash: "#error=Invalid_Signin&error_description=Invalid_Signin",
1173
+ },
1174
+ get parent() {
1175
+ return this;
1176
+ },
1177
+ };
1178
+
1179
+ expect(function() {
1180
+ UserSession.completeOAuth2(
1181
+ {
1182
+ clientId: "clientId",
1183
+ redirectUri: "https://example-app.com/redirect-uri",
1184
+ },
1185
+ MockWindow
1186
+ );
1187
+ }).toThrowError(ArcGISRequestError, "Invalid_Signin: Invalid_Signin");
1188
+ });
1189
+
1190
+ it("should throw an error if the handler or parent window cannot be accessed", () => {
1191
+ const MockParent = {
1192
+ get parent() {
1193
+ throw new Error(
1194
+ "This window isn't where auth started, but was opened from somewhere else via window.open() perhaps from another domain which would cause a cross domain error when read."
1195
+ );
1196
+ },
1197
+ };
1198
+
1199
+ const MockWindow = {
1200
+ location: {
1201
+ hash: "#error=Invalid_Signin&error_description=Invalid_Signin",
1202
+ },
1203
+ get opener() {
1204
+ return MockParent;
1205
+ },
1206
+ };
1207
+
1208
+ expect(function() {
1209
+ UserSession.completeOAuth2(
1210
+ {
1211
+ clientId: "clientId",
1212
+ redirectUri: "https://example-app.com/redirect-uri",
1213
+ },
1214
+ MockWindow
1215
+ );
1216
+ }).toThrowError(ArcGISAuthError);
1217
+ });
1218
+ });
1219
+
1220
+ describe("postmessage auth :: ", () => {
1221
+ const MockWindow = {
1222
+ addEventListener: () => {},
1223
+ removeEventListener: () => {},
1224
+ parent: {
1225
+ postMessage: () => {},
1226
+ },
1227
+ };
1228
+
1229
+ const cred = {
1230
+ expires: TOMORROW.getTime(),
1231
+ server: "https://www.arcgis.com/sharing/rest",
1232
+ ssl: false,
1233
+ token: "token",
1234
+ userId: "jsmith",
1235
+ };
1236
+
1237
+ it(".disablePostMessageAuth removes event listener", () => {
1238
+ const removeSpy = spyOn(MockWindow, "removeEventListener");
1239
+ const session = UserSession.fromCredential(cred);
1240
+ session.disablePostMessageAuth(MockWindow);
1241
+ expect(removeSpy.calls.count()).toBe(
1242
+ 1,
1243
+ "should call removeEventListener"
1244
+ );
1245
+ });
1246
+ it(".enablePostMessageAuth adds event listener", () => {
1247
+ const addSpy = spyOn(MockWindow, "addEventListener");
1248
+ const session = UserSession.fromCredential(cred);
1249
+ session.enablePostMessageAuth(
1250
+ ["https://storymaps.arcgis.com"],
1251
+ MockWindow
1252
+ );
1253
+ expect(addSpy.calls.count()).toBe(1, "should call addEventListener");
1254
+ });
1255
+
1256
+ it(".enablePostMessage handler returns credential to origin in list", () => {
1257
+ // ok, this gets kinda gnarly...
1258
+
1259
+ // create a mock window object
1260
+ // that will hold the passed in event handler so we can fire it manually
1261
+ const Win = {
1262
+ _fn: (evt: any) => {},
1263
+ addEventListener(evt: any, fn: any) {
1264
+ // enablePostMessageAuth passes in the handler, which is what we're actually testing
1265
+ Win._fn = fn;
1266
+ },
1267
+ removeEventListener() {},
1268
+ };
1269
+ // Create the session
1270
+ const session = UserSession.fromCredential(cred);
1271
+ // enable postMessageAuth allowing storymaps.arcgis.com to recieve creds
1272
+ session.enablePostMessageAuth(["https://storymaps.arcgis.com"], Win);
1273
+ // create an event object, with a matching origin
1274
+ // an a source.postMessage fn that we can spy on
1275
+ const event = {
1276
+ origin: "https://storymaps.arcgis.com",
1277
+ source: {
1278
+ postMessage(msg: any, origin: string) {},
1279
+ },
1280
+ data: {
1281
+ type: "arcgis:auth:requestCredential",
1282
+ },
1283
+ };
1284
+ // create the spy
1285
+ const sourceSpy = spyOn(event.source, "postMessage");
1286
+ // Now, fire the handler, simulating what happens when a postMessage event comes
1287
+ // from an embedded iframe
1288
+ Win._fn(event);
1289
+ // Expectations...
1290
+ expect(sourceSpy.calls.count()).toBe(
1291
+ 1,
1292
+ "souce.postMessage should be called in handler"
1293
+ );
1294
+ const args = sourceSpy.calls.argsFor(0);
1295
+ expect(args[0].type).toBe(
1296
+ "arcgis:auth:credential",
1297
+ "should send credential type"
1298
+ );
1299
+ expect(args[0].credential.userId).toBe(
1300
+ "jsmith",
1301
+ "should send credential"
1302
+ );
1303
+ expect(args[0].credential.server).toBe(
1304
+ "https://www.arcgis.com",
1305
+ "sends server url without /sharing/rest"
1306
+ );
1307
+ // now the case where it's not a valid origin
1308
+ event.origin = "https://evil.com";
1309
+ Win._fn(event);
1310
+ expect(sourceSpy.calls.count()).toBe(
1311
+ 1,
1312
+ "souce.postMessage should not be called in handler for invalid origin"
1313
+ );
1314
+ });
1315
+
1316
+ it(".fromParent happy path", () => {
1317
+ // create a mock window that will fire the handler
1318
+ const Win = {
1319
+ _fn: (evt: any) => {},
1320
+ addEventListener(evt: any, fn: any) {
1321
+ Win._fn = fn;
1322
+ },
1323
+ removeEventListener() {},
1324
+ parent: {
1325
+ postMessage(msg: any, origin: string) {
1326
+ Win._fn({
1327
+ origin: "https://origin.com",
1328
+ data: { type: "arcgis:auth:credential", credential: cred },
1329
+ source: Win.parent,
1330
+ });
1331
+ },
1332
+ },
1333
+ };
1334
+
1335
+ return UserSession.fromParent("https://origin.com", Win).then(
1336
+ (session) => {
1337
+ expect(session.username).toBe(
1338
+ "jsmith",
1339
+ "should use the cred wired throu the mock window"
1340
+ );
1341
+ }
1342
+ );
1343
+ });
1344
+
1345
+ it(".fromParent ignores other messages, then intercepts the correct one", async () => {
1346
+ // create a mock window that will fire the handler
1347
+ const Win = {
1348
+ _fn: (evt: any) => {},
1349
+ addEventListener(evt: any, fn: any) {
1350
+ Win._fn = fn;
1351
+ },
1352
+ removeEventListener() {},
1353
+ parent: {
1354
+ postMessage(msg: any, origin: string) {
1355
+ // fire one we intend to ignore
1356
+ Win._fn({
1357
+ origin: "https://notorigin.com",
1358
+ data: { type: "other:random", foo: { bar: "baz" } },
1359
+ source: "Not Parent Object",
1360
+ });
1361
+ // fire a second we want to intercept
1362
+ Win._fn({
1363
+ origin: "https://origin.com",
1364
+ data: { type: "arcgis:auth:credential", credential: cred },
1365
+ source: Win.parent,
1366
+ });
1367
+ },
1368
+ },
1369
+ };
1370
+
1371
+ return UserSession.fromParent("https://origin.com", Win).then((resp) => {
1372
+ expect(resp.username).toBe(
1373
+ "jsmith",
1374
+ "should use the cred wired throu the mock window"
1375
+ );
1376
+ });
1377
+ });
1378
+
1379
+ it(".fromParent rejects if invlid cred", () => {
1380
+ // create a mock window that will fire the handler
1381
+ const Win = {
1382
+ _fn: (evt: any) => {},
1383
+ addEventListener(evt: any, fn: any) {
1384
+ Win._fn = fn;
1385
+ },
1386
+ removeEventListener() {},
1387
+ parent: {
1388
+ postMessage(msg: any, origin: string) {
1389
+ Win._fn({
1390
+ origin: "https://origin.com",
1391
+ data: {
1392
+ type: "arcgis:auth:credential",
1393
+ credential: { foo: "bar" },
1394
+ },
1395
+ source: Win.parent,
1396
+ });
1397
+ },
1398
+ },
1399
+ };
1400
+
1401
+ return UserSession.fromParent("https://origin.com", Win).catch((err) => {
1402
+ expect(err).toBeDefined("Should reject");
1403
+ });
1404
+ });
1405
+
1406
+ it(".fromParent rejects if auth error recieved", () => {
1407
+ // create a mock window that will fire the handler
1408
+ const Win = {
1409
+ _fn: (evt: any) => {},
1410
+ addEventListener(evt: any, fn: any) {
1411
+ Win._fn = fn;
1412
+ },
1413
+ removeEventListener() {},
1414
+ parent: {
1415
+ postMessage(msg: any, origin: string) {
1416
+ Win._fn({
1417
+ origin: "https://origin.com",
1418
+ data: {
1419
+ type: "arcgis:auth:error",
1420
+ error: { message: "Rejected authentication request." },
1421
+ },
1422
+ source: Win.parent,
1423
+ });
1424
+ },
1425
+ },
1426
+ };
1427
+
1428
+ return UserSession.fromParent("https://origin.com", Win).catch((err) => {
1429
+ expect(err).toBeDefined("Should reject");
1430
+ });
1431
+ });
1432
+
1433
+ it(".fromParent rejects if auth unknown message", () => {
1434
+ // create a mock window that will fire the handler
1435
+ const Win = {
1436
+ _fn: (evt: any) => {},
1437
+ addEventListener(evt: any, fn: any) {
1438
+ Win._fn = fn;
1439
+ },
1440
+ removeEventListener() {},
1441
+ parent: {
1442
+ postMessage(msg: any, origin: string) {
1443
+ Win._fn({
1444
+ origin: "https://origin.com",
1445
+ data: { type: "arcgis:auth:other" },
1446
+ source: Win.parent,
1447
+ });
1448
+ },
1449
+ },
1450
+ };
1451
+
1452
+ return UserSession.fromParent("https://origin.com", Win).catch((err) => {
1453
+ expect(err.message).toBe("Unknown message type.", "Should reject");
1454
+ });
1455
+ });
1456
+ });
1457
+
1458
+ describe("validateAppAccess: ", () => {
1459
+ it("makes a request to /oauth2/validateAppAccess passing params", () => {
1460
+ const VERIFYAPPACCESS_URL =
1461
+ "https://www.arcgis.com/sharing/rest/oauth2/validateAppAccess";
1462
+ fetchMock.postOnce(VERIFYAPPACCESS_URL, {
1463
+ valid: true,
1464
+ viewOnlyUserTypeApp: false,
1465
+ });
1466
+ const session = new UserSession({
1467
+ clientId: "clientId",
1468
+ redirectUri: "https://example-app.com/redirect-uri",
1469
+ token: "FAKE-TOKEN",
1470
+ tokenExpires: TOMORROW,
1471
+ refreshToken: "refreshToken",
1472
+ refreshTokenExpires: TOMORROW,
1473
+ refreshTokenTTL: 1440,
1474
+ username: "jsmith",
1475
+ password: "123456",
1476
+ });
1477
+ return session
1478
+ .validateAppAccess("abc123")
1479
+ .then((response) => {
1480
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall(
1481
+ VERIFYAPPACCESS_URL
1482
+ );
1483
+ expect(url).toEqual(VERIFYAPPACCESS_URL);
1484
+ expect(options.body).toContain("f=json");
1485
+ expect(options.body).toContain("token=FAKE-TOKEN");
1486
+ expect(options.body).toContain("client_id=abc123");
1487
+ expect(response.valid).toEqual(true);
1488
+ expect(response.viewOnlyUserTypeApp).toBe(false);
1489
+ })
1490
+ .catch((e) => fail(e));
1491
+ });
1492
+ });
1493
+
1494
+ it("should throw an unknown error if the url has no error or access_token", () => {
1495
+ const MockWindow = {
1496
+ location: {
1497
+ hash: "",
1498
+ },
1499
+ get opener() {
1500
+ return this;
1501
+ },
1502
+ };
1503
+
1504
+ expect(function() {
1505
+ UserSession.completeOAuth2(
1506
+ {
1507
+ clientId: "clientId",
1508
+ redirectUri: "https://example-app.com/redirect-uri",
1509
+ },
1510
+ MockWindow
1511
+ );
1512
+ }).toThrowError(ArcGISRequestError, "Unknown error");
1513
+ });
1514
+
1515
+ describe(".authorize()", () => {
1516
+ it("should redirect the request to the authorization page", (done) => {
1517
+ const spy = jasmine.createSpy("spy");
1518
+ const MockResponse: any = {
1519
+ writeHead: spy,
1520
+ end() {
1521
+ expect(spy.calls.mostRecent().args[0]).toBe(301);
1522
+ expect(spy.calls.mostRecent().args[1].Location).toBe(
1523
+ "https://arcgis.com/sharing/rest/oauth2/authorize?client_id=clientId&expiration=20160&response_type=code&redirect_uri=https%3A%2F%2Fexample-app.com%2Fredirect-uri"
1524
+ );
1525
+ done();
1526
+ },
1527
+ };
1528
+
1529
+ UserSession.authorize(
1530
+ {
1531
+ clientId: "clientId",
1532
+ redirectUri: "https://example-app.com/redirect-uri",
1533
+ },
1534
+ MockResponse
1535
+ );
1536
+ });
1537
+
1538
+ it("should redirect the request to the authorization page with custom expiration", (done) => {
1539
+ const spy = jasmine.createSpy("spy");
1540
+ const MockResponse: any = {
1541
+ writeHead: spy,
1542
+ end() {
1543
+ expect(spy.calls.mostRecent().args[0]).toBe(301);
1544
+ expect(spy.calls.mostRecent().args[1].Location).toBe(
1545
+ "https://arcgis.com/sharing/rest/oauth2/authorize?client_id=clientId&expiration=10000&response_type=code&redirect_uri=https%3A%2F%2Fexample-app.com%2Fredirect-uri"
1546
+ );
1547
+ done();
1548
+ },
1549
+ };
1550
+
1551
+ UserSession.authorize(
1552
+ {
1553
+ clientId: "clientId",
1554
+ redirectUri: "https://example-app.com/redirect-uri",
1555
+ expiration: 10000
1556
+ },
1557
+ MockResponse
1558
+ );
1559
+ });
1560
+
1561
+ it("should redirect the request to the authorization page with custom duration (DEPRECATED)", (done) => {
1562
+ const spy = jasmine.createSpy("spy");
1563
+ const MockResponse: any = {
1564
+ writeHead: spy,
1565
+ end() {
1566
+ expect(spy.calls.mostRecent().args[0]).toBe(301);
1567
+ expect(spy.calls.mostRecent().args[1].Location).toBe(
1568
+ "https://arcgis.com/sharing/rest/oauth2/authorize?client_id=clientId&expiration=10001&response_type=code&redirect_uri=https%3A%2F%2Fexample-app.com%2Fredirect-uri"
1569
+ );
1570
+ done();
1571
+ },
1572
+ };
1573
+
1574
+ UserSession.authorize(
1575
+ {
1576
+ clientId: "clientId",
1577
+ redirectUri: "https://example-app.com/redirect-uri",
1578
+ duration: 10001
1579
+ },
1580
+ MockResponse
1581
+ );
1582
+ });
1583
+ });
1584
+
1585
+ describe(".exchangeAuthorizationCode()", () => {
1586
+ let paramsSpy: jasmine.Spy;
1587
+
1588
+ beforeEach(() => {
1589
+ paramsSpy = spyOn(FormData.prototype, "append").and.callThrough();
1590
+ });
1591
+
1592
+ afterAll(() => {
1593
+ paramsSpy.calls.reset();
1594
+ });
1595
+
1596
+ it("should exchange an authorization code for a new UserSession", (done) => {
1597
+ fetchMock.postOnce("https://www.arcgis.com/sharing/rest/oauth2/token", {
1598
+ access_token: "token",
1599
+ expires_in: 1800,
1600
+ refresh_token: "refreshToken",
1601
+ username: "Casey",
1602
+ ssl: true,
1603
+ });
1604
+
1605
+ UserSession.exchangeAuthorizationCode(
1606
+ {
1607
+ clientId: "clientId",
1608
+ redirectUri: "https://example-app.com/redirect-uri",
1609
+ },
1610
+ "code"
1611
+ )
1612
+ .then((session) => {
1613
+ expect(session.token).toBe("token");
1614
+ expect(session.tokenExpires.getTime()).toBeGreaterThan(Date.now());
1615
+ expect(session.username).toBe("Casey");
1616
+ expect(session.refreshToken).toBe("refreshToken");
1617
+ expect(session.ssl).toBe(true);
1618
+ done();
1619
+ })
1620
+ .catch((e) => {
1621
+ fail(e);
1622
+ });
1623
+ });
1624
+
1625
+ it("should return a UserSession where refreshTokenExpires is 2 weeks from now (within 10 ms)", (done) => {
1626
+ fetchMock.postOnce("https://www.arcgis.com/sharing/rest/oauth2/token", {
1627
+ access_token: "token",
1628
+ refresh_token: "refreshToken",
1629
+ username: "Casey",
1630
+ ssl: true,
1631
+ });
1632
+
1633
+ UserSession.exchangeAuthorizationCode(
1634
+ {
1635
+ clientId: "clientId",
1636
+ redirectUri: "https://example-app.com/redirect-uri",
1637
+ },
1638
+ "code"
1639
+ )
1640
+ .then((session) => {
1641
+ const twoWeeksFromNow = new Date(Date.now() + (20160 - 1) * 60 * 1000);
1642
+ expect(session.refreshTokenExpires.getTime()).toBeGreaterThan(twoWeeksFromNow.getTime() - 10);
1643
+ expect(session.refreshTokenExpires.getTime()).toBeLessThan(twoWeeksFromNow.getTime() + 10);
1644
+ done();
1645
+ })
1646
+ .catch((e) => {
1647
+ fail(e);
1648
+ });
1649
+ });
1650
+ });
1651
+
1652
+ describe(".getUser()", () => {
1653
+ afterEach(fetchMock.restore);
1654
+
1655
+ it("should cache metadata about the user", (done) => {
1656
+ // we intentionally only mock one response
1657
+ fetchMock.once(
1658
+ "https://www.arcgis.com/sharing/rest/community/self?f=json&token=token",
1659
+ {
1660
+ username: "jsmith",
1661
+ fullName: "John Smith",
1662
+ role: "org_publisher",
1663
+ }
1664
+ );
1665
+
1666
+ const session = new UserSession({
1667
+ clientId: "clientId",
1668
+ redirectUri: "https://example-app.com/redirect-uri",
1669
+ token: "token",
1670
+ tokenExpires: TOMORROW,
1671
+ refreshToken: "refreshToken",
1672
+ refreshTokenExpires: TOMORROW,
1673
+ refreshTokenTTL: 1440,
1674
+ username: "jsmith",
1675
+ password: "123456",
1676
+ });
1677
+
1678
+ session
1679
+ .getUser()
1680
+ .then((response) => {
1681
+ expect(response.role).toEqual("org_publisher");
1682
+ session
1683
+ .getUser()
1684
+ .then((cachedResponse) => {
1685
+ expect(cachedResponse.fullName).toEqual("John Smith");
1686
+ done();
1687
+ })
1688
+ .catch((e) => {
1689
+ fail(e);
1690
+ });
1691
+ })
1692
+ .catch((e) => {
1693
+ fail(e);
1694
+ });
1695
+ });
1696
+
1697
+ it("should never make more then 1 request", (done) => {
1698
+ // we intentionally only mock one response
1699
+ fetchMock.once(
1700
+ "https://www.arcgis.com/sharing/rest/community/self?f=json&token=token",
1701
+ {
1702
+ username: "jsmith",
1703
+ fullName: "John Smith",
1704
+ role: "org_publisher",
1705
+ }
1706
+ );
1707
+
1708
+ const session = new UserSession({
1709
+ clientId: "clientId",
1710
+ redirectUri: "https://example-app.com/redirect-uri",
1711
+ token: "token",
1712
+ tokenExpires: TOMORROW,
1713
+ refreshToken: "refreshToken",
1714
+ refreshTokenExpires: TOMORROW,
1715
+ refreshTokenTTL: 1440,
1716
+ username: "jsmith",
1717
+ password: "123456",
1718
+ });
1719
+
1720
+ Promise.all([session.getUser(), session.getUser()])
1721
+ .then(() => {
1722
+ done();
1723
+ })
1724
+ .catch((e) => {
1725
+ fail(e);
1726
+ });
1727
+ });
1728
+ });
1729
+
1730
+ describe(".getUsername()", () => {
1731
+ afterEach(fetchMock.restore);
1732
+
1733
+ it("should fetch the username via getUser()", (done) => {
1734
+ // we intentionally only mock one response
1735
+ fetchMock.once(
1736
+ "https://www.arcgis.com/sharing/rest/community/self?f=json&token=token",
1737
+ {
1738
+ username: "jsmith",
1739
+ }
1740
+ );
1741
+
1742
+ const session = new UserSession({
1743
+ token: "token",
1744
+ });
1745
+
1746
+ session
1747
+ .getUsername()
1748
+ .then((response) => {
1749
+ expect(response).toEqual("jsmith");
1750
+
1751
+ // also test getting it from the cache.
1752
+ session
1753
+ .getUsername()
1754
+ .then((username) => {
1755
+ done();
1756
+
1757
+ expect(username).toEqual("jsmith");
1758
+ })
1759
+ .catch((e) => {
1760
+ fail(e);
1761
+ });
1762
+ })
1763
+ .catch((e) => {
1764
+ fail(e);
1765
+ });
1766
+ });
1767
+
1768
+ it("should use a username if passed in the session", (done) => {
1769
+ const session = new UserSession({
1770
+ username: "jsmith",
1771
+ });
1772
+
1773
+ session
1774
+ .getUsername()
1775
+ .then((response) => {
1776
+ expect(response).toEqual("jsmith");
1777
+ done();
1778
+ })
1779
+ .catch((e) => {
1780
+ fail(e);
1781
+ });
1782
+ });
1783
+ });
1784
+
1785
+ describe("to/fromCredential()", () => {
1786
+ const MOCK_CREDENTIAL: ICredential = {
1787
+ expires: TOMORROW.getTime(),
1788
+ server: "https://www.arcgis.com",
1789
+ ssl: false,
1790
+ token: "token",
1791
+ userId: "jsmith",
1792
+ };
1793
+
1794
+ const MOCK_USER_SESSION = new UserSession({
1795
+ clientId: "clientId",
1796
+ redirectUri: "https://example-app.com/redirect-uri",
1797
+ token: "token",
1798
+ ssl: false,
1799
+ tokenExpires: TOMORROW,
1800
+ refreshToken: "refreshToken",
1801
+ refreshTokenExpires: TOMORROW,
1802
+ refreshTokenTTL: 1440,
1803
+ username: "jsmith",
1804
+ password: "123456",
1805
+ });
1806
+
1807
+ it("should create a credential object from a session", () => {
1808
+ const creds = MOCK_USER_SESSION.toCredential();
1809
+ expect(creds.userId).toEqual("jsmith");
1810
+ expect(creds.server).toEqual("https://www.arcgis.com/sharing/rest");
1811
+ expect(creds.ssl).toEqual(false);
1812
+ expect(creds.token).toEqual("token");
1813
+ expect(creds.expires).toEqual(TOMORROW.getTime());
1814
+ });
1815
+
1816
+ it("should create a UserSession from a credential", () => {
1817
+ const session = UserSession.fromCredential(MOCK_CREDENTIAL);
1818
+ expect(session.username).toEqual("jsmith");
1819
+ expect(session.portal).toEqual("https://www.arcgis.com/sharing/rest");
1820
+ expect(session.ssl).toEqual(false);
1821
+ expect(session.token).toEqual("token");
1822
+ expect(session.tokenExpires).toEqual(new Date(TOMORROW));
1823
+ });
1824
+
1825
+ it("should create a UserSession from a credential that came from a UserSession", () => {
1826
+ const creds = MOCK_USER_SESSION.toCredential();
1827
+ const credSession = UserSession.fromCredential(creds);
1828
+ expect(credSession.username).toEqual("jsmith");
1829
+ expect(credSession.portal).toEqual("https://www.arcgis.com/sharing/rest");
1830
+ expect(credSession.ssl).toEqual(false);
1831
+ expect(credSession.token).toEqual("token");
1832
+ expect(credSession.tokenExpires).toEqual(new Date(TOMORROW));
1833
+ });
1834
+ });
1835
+
1836
+ describe("fromCredential() when credential doesn't have an expiration date or ssl", () => {
1837
+ const MOCK_CREDENTIAL: ICredential = {
1838
+ expires: undefined,
1839
+ server: "https://www.arcgis.com",
1840
+ ssl: undefined,
1841
+ token: "token",
1842
+ userId: "jsmith",
1843
+ };
1844
+
1845
+ it("should create a UserSession from a credential", () => {
1846
+ jasmine.clock().install();
1847
+ jasmine.clock().mockDate();
1848
+
1849
+ const session = UserSession.fromCredential(MOCK_CREDENTIAL);
1850
+ expect(session.username).toEqual("jsmith");
1851
+ expect(session.portal).toEqual("https://www.arcgis.com/sharing/rest");
1852
+ expect(session.ssl).toBeTruthy();
1853
+ expect(session.token).toEqual("token");
1854
+ expect(session.tokenExpires).toEqual(
1855
+ new Date(Date.now() + 7200000 /* 2 hours */)
1856
+ );
1857
+
1858
+ jasmine.clock().uninstall();
1859
+ });
1860
+ });
1861
+
1862
+ describe("getServerRootUrl()", () => {
1863
+ it("should lowercase domain names", () => {
1864
+ const session = new UserSession({
1865
+ clientId: "id",
1866
+ token: "token",
1867
+ tokenExpires: TOMORROW,
1868
+ });
1869
+
1870
+ const root = session.getServerRootUrl(
1871
+ "https://PNP00035.esri.com/server/rest/services/Hosted/perimeters_dd83/FeatureServer"
1872
+ );
1873
+ expect(root).toEqual("https://pnp00035.esri.com/server");
1874
+ });
1875
+
1876
+ it("should not lowercase path names", () => {
1877
+ const session = new UserSession({
1878
+ clientId: "id",
1879
+ token: "token",
1880
+ tokenExpires: TOMORROW,
1881
+ });
1882
+
1883
+ const root = session.getServerRootUrl(
1884
+ "https://pnp00035.esri.com/tiles/LkFyxb9zDq7vAOAm/arcgis/rest/services/NB_Stereographic/VectorTileServer"
1885
+ );
1886
+ expect(root).toEqual(
1887
+ "https://pnp00035.esri.com/tiles/LkFyxb9zDq7vAOAm/arcgis"
1888
+ );
1889
+ });
1890
+
1891
+ it("should respect the original https/http protocol", () => {
1892
+ const session = new UserSession({
1893
+ clientId: "id",
1894
+ token: "token",
1895
+ tokenExpires: TOMORROW,
1896
+ });
1897
+
1898
+ const root = session.getServerRootUrl(
1899
+ "http://pnp00035.esri.com/tiles/LkFyxb9zDq7vAOAm/arcgis/rest/services/NB_Stereographic/VectorTileServer"
1900
+ );
1901
+ expect(root).toEqual(
1902
+ "http://pnp00035.esri.com/tiles/LkFyxb9zDq7vAOAm/arcgis"
1903
+ );
1904
+ });
1905
+ });
1906
+
1907
+ describe("non-federated server", () => {
1908
+ it("shouldnt fetch a fresh token if the current one isn't expired.", (done) => {
1909
+ const MOCK_USER_SESSION = new UserSession({
1910
+ username: "c@sey",
1911
+ password: "123456",
1912
+ token: "token",
1913
+ tokenExpires: TOMORROW,
1914
+ server: "https://fakeserver.com/arcgis",
1915
+ });
1916
+
1917
+ MOCK_USER_SESSION.getToken(
1918
+ "https://fakeserver.com/arcgis/rest/services/Fake/MapServer/"
1919
+ )
1920
+ .then((token) => {
1921
+ expect(token).toBe("token");
1922
+ done();
1923
+ })
1924
+ .catch((err) => {
1925
+ fail(err);
1926
+ });
1927
+ });
1928
+
1929
+ it("should fetch a fresh token if the current one is expired.", (done) => {
1930
+ const MOCK_USER_SESSION = new UserSession({
1931
+ username: "jsmith",
1932
+ password: "123456",
1933
+ token: "token",
1934
+ tokenExpires: YESTERDAY,
1935
+ server: "https://fakeserver.com/arcgis",
1936
+ });
1937
+
1938
+ fetchMock.postOnce("https://fakeserver.com/arcgis/rest/info", {
1939
+ currentVersion: 10.61,
1940
+ fullVersion: "10.6.1",
1941
+ authInfo: {
1942
+ isTokenBasedSecurity: true,
1943
+ tokenServicesUrl: "https://fakeserver.com/arcgis/tokens/",
1944
+ },
1945
+ });
1946
+
1947
+ fetchMock.postOnce("https://fakeserver.com/arcgis/tokens/", {
1948
+ token: "fresh-token",
1949
+ expires: TOMORROW.getTime(),
1950
+ username: " jsmith",
1951
+ });
1952
+
1953
+ MOCK_USER_SESSION.getToken(
1954
+ "https://fakeserver.com/arcgis/rest/services/Fake/MapServer/"
1955
+ )
1956
+ .then((token) => {
1957
+ expect(token).toBe("fresh-token");
1958
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall(
1959
+ "https://fakeserver.com/arcgis/tokens/"
1960
+ );
1961
+ expect(options.method).toBe("POST");
1962
+ expect(options.body).toContain("f=json");
1963
+ expect(options.body).toContain("username=jsmith");
1964
+ expect(options.body).toContain("password=123456");
1965
+ expect(options.body).toContain("client=referer");
1966
+ done();
1967
+ })
1968
+ .catch((err) => {
1969
+ fail(err);
1970
+ });
1971
+ });
1972
+
1973
+ it("should trim down the server url if necessary.", (done) => {
1974
+ const MOCK_USER_SESSION = new UserSession({
1975
+ username: "jsmith",
1976
+ password: "123456",
1977
+ token: "token",
1978
+ tokenExpires: YESTERDAY,
1979
+ server: "https://fakeserver.com/arcgis/rest/services/blah/",
1980
+ });
1981
+
1982
+ fetchMock.postOnce("https://fakeserver.com/arcgis/rest/info", {
1983
+ currentVersion: 10.61,
1984
+ fullVersion: "10.6.1",
1985
+ authInfo: {
1986
+ isTokenBasedSecurity: true,
1987
+ tokenServicesUrl: "https://fakeserver.com/arcgis/tokens/",
1988
+ },
1989
+ });
1990
+
1991
+ fetchMock.postOnce("https://fakeserver.com/arcgis/tokens/", {
1992
+ token: "fresh-token",
1993
+ expires: TOMORROW.getTime(),
1994
+ username: " jsmith",
1995
+ });
1996
+
1997
+ MOCK_USER_SESSION.getToken(
1998
+ "https://fakeserver.com/arcgis/rest/services/Fake/MapServer/"
1999
+ )
2000
+ .then((token) => {
2001
+ expect(token).toBe("fresh-token");
2002
+ done();
2003
+ })
2004
+ .catch((err) => {
2005
+ fail(err);
2006
+ });
2007
+ });
2008
+
2009
+ it("should throw an error if the server isnt trusted.", (done) => {
2010
+ fetchMock.postOnce("https://fakeserver2.com/arcgis/rest/info", {
2011
+ currentVersion: 10.61,
2012
+ fullVersion: "10.6.1",
2013
+ authInfo: {
2014
+ isTokenBasedSecurity: true,
2015
+ tokenServicesUrl: "https://fakeserver2.com/arcgis/tokens/",
2016
+ },
2017
+ });
2018
+ const MOCK_USER_SESSION = new UserSession({
2019
+ username: "c@sey",
2020
+ password: "123456",
2021
+ token: "token",
2022
+ tokenExpires: TOMORROW,
2023
+ server: "https://fakeserver.com/arcgis",
2024
+ });
2025
+
2026
+ MOCK_USER_SESSION.getToken(
2027
+ "https://fakeserver2.com/arcgis/rest/services/Fake/MapServer/"
2028
+ )
2029
+ .then((token) => {
2030
+ fail(token);
2031
+ })
2032
+ .catch((err) => {
2033
+ expect(err.code).toBe("NOT_FEDERATED");
2034
+ expect(err.originalMessage).toEqual(
2035
+ "https://fakeserver2.com/arcgis/rest/services/Fake/MapServer/ is not federated with any portal and is not explicitly trusted."
2036
+ );
2037
+ done();
2038
+ });
2039
+ });
2040
+ });
2041
+
2042
+ describe(".getPortal()", () => {
2043
+ afterEach(fetchMock.restore);
2044
+
2045
+ it("should cache metadata about the portal", (done) => {
2046
+ // we intentionally only mock one response
2047
+ fetchMock.once(
2048
+ "https://www.arcgis.com/sharing/rest/portals/self?f=json&token=token",
2049
+ {
2050
+ authorizedCrossOriginDomains: ["gis.city.com"],
2051
+ }
2052
+ );
2053
+
2054
+ const session = new UserSession({
2055
+ clientId: "clientId",
2056
+ redirectUri: "https://example-app.com/redirect-uri",
2057
+ token: "token",
2058
+ tokenExpires: TOMORROW,
2059
+ refreshToken: "refreshToken",
2060
+ refreshTokenExpires: TOMORROW,
2061
+ refreshTokenTTL: 1440,
2062
+ username: "jsmith",
2063
+ password: "123456",
2064
+ });
2065
+
2066
+ session
2067
+ .getPortal()
2068
+ .then((response) => {
2069
+ expect(response.authorizedCrossOriginDomains).toEqual([
2070
+ "gis.city.com",
2071
+ ]);
2072
+ session
2073
+ .getPortal()
2074
+ .then((cachedResponse) => {
2075
+ expect(cachedResponse.authorizedCrossOriginDomains).toEqual([
2076
+ "gis.city.com",
2077
+ ]);
2078
+ done();
2079
+ })
2080
+ .catch((e) => {
2081
+ fail(e);
2082
+ });
2083
+ })
2084
+ .catch((e) => {
2085
+ fail(e);
2086
+ });
2087
+ });
2088
+
2089
+ it("should never make more then 1 request", (done) => {
2090
+ // we intentionally only mock one response
2091
+ fetchMock.once(
2092
+ "https://www.arcgis.com/sharing/rest/portals/self?f=json&token=token",
2093
+ {
2094
+ authorizedCrossOriginDomains: ["gis.city.com"],
2095
+ }
2096
+ );
2097
+
2098
+ const session = new UserSession({
2099
+ clientId: "clientId",
2100
+ redirectUri: "https://example-app.com/redirect-uri",
2101
+ token: "token",
2102
+ tokenExpires: TOMORROW,
2103
+ refreshToken: "refreshToken",
2104
+ refreshTokenExpires: TOMORROW,
2105
+ refreshTokenTTL: 1440,
2106
+ username: "jsmith",
2107
+ password: "123456",
2108
+ });
2109
+
2110
+ Promise.all([session.getPortal(), session.getPortal()])
2111
+ .then(() => {
2112
+ done();
2113
+ })
2114
+ .catch((e) => {
2115
+ fail(e);
2116
+ });
2117
+ });
2118
+ });
2119
+
2120
+ describe("fetchAuthorizedDomains/getDomainCredentials", () => {
2121
+ it("should default to same-origin credentials when no domains are listed in authorizedCrossOriginDomains", (done) => {
2122
+ const session = new UserSession({
2123
+ clientId: "id",
2124
+ token: "token",
2125
+ refreshToken: "refresh",
2126
+ tokenExpires: TOMORROW,
2127
+ portal: "https://gis.city.gov/sharing/rest",
2128
+ });
2129
+
2130
+ fetchMock.postOnce("https://gisservices.city.gov/public/rest/info", {
2131
+ currentVersion: 10.51,
2132
+ fullVersion: "10.5.1.120",
2133
+ owningSystemUrl: "https://gis.city.gov",
2134
+ authInfo: {
2135
+ isTokenBasedSecurity: true,
2136
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
2137
+ },
2138
+ });
2139
+
2140
+ fetchMock.getOnce(
2141
+ "https://gis.city.gov/sharing/rest/portals/self?f=json&token=token",
2142
+ {
2143
+ authorizedCrossOriginDomains: [],
2144
+ }
2145
+ );
2146
+
2147
+ fetchMock.postOnce("https://gis.city.gov/sharing/rest/info", {
2148
+ owningSystemUrl: "http://gis.city.gov",
2149
+ authInfo: {
2150
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
2151
+ isTokenBasedSecurity: true,
2152
+ },
2153
+ });
2154
+
2155
+ fetchMock.postOnce("https://gis.city.gov/sharing/generateToken", {
2156
+ token: "serverToken",
2157
+ expires: TOMORROW,
2158
+ });
2159
+
2160
+ fetchMock.post(
2161
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query",
2162
+ {
2163
+ count: 123,
2164
+ }
2165
+ );
2166
+
2167
+ request(
2168
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query",
2169
+ {
2170
+ authentication: session,
2171
+ }
2172
+ )
2173
+ .then((response) => {
2174
+ const { credentials } = fetchMock.lastOptions(
2175
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
2176
+ );
2177
+ expect(credentials).toEqual("same-origin");
2178
+
2179
+ done();
2180
+ })
2181
+ .catch((e) => {
2182
+ fail(e);
2183
+ });
2184
+ });
2185
+
2186
+ it("should set the credentials option to include when a server is listed in authorizedCrossOriginDomains", (done) => {
2187
+ const session = new UserSession({
2188
+ clientId: "id",
2189
+ token: "token",
2190
+ refreshToken: "refresh",
2191
+ tokenExpires: TOMORROW,
2192
+ portal: "https://gis.city.gov/sharing/rest",
2193
+ });
2194
+
2195
+ fetchMock.postOnce("https://gisservices.city.gov/public/rest/info", {
2196
+ currentVersion: 10.51,
2197
+ fullVersion: "10.5.1.120",
2198
+ owningSystemUrl: "https://gis.city.gov",
2199
+ authInfo: {
2200
+ isTokenBasedSecurity: true,
2201
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
2202
+ },
2203
+ });
2204
+
2205
+ fetchMock.getOnce(
2206
+ "https://gis.city.gov/sharing/rest/portals/self?f=json&token=token",
2207
+ {
2208
+ authorizedCrossOriginDomains: ["https://gisservices.city.gov"],
2209
+ }
2210
+ );
2211
+
2212
+ fetchMock.postOnce("https://gis.city.gov/sharing/rest/info", {
2213
+ owningSystemUrl: "http://gis.city.gov",
2214
+ authInfo: {
2215
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
2216
+ isTokenBasedSecurity: true,
2217
+ },
2218
+ });
2219
+
2220
+ fetchMock.postOnce("https://gis.city.gov/sharing/generateToken", {
2221
+ token: "serverToken",
2222
+ expires: TOMORROW,
2223
+ });
2224
+
2225
+ fetchMock.post(
2226
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query",
2227
+ {
2228
+ count: 123,
2229
+ }
2230
+ );
2231
+
2232
+ request(
2233
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query",
2234
+ {
2235
+ authentication: session,
2236
+ }
2237
+ )
2238
+ .then((response) => {
2239
+ const { credentials } = fetchMock.lastOptions(
2240
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
2241
+ );
2242
+ expect(credentials).toEqual("include");
2243
+
2244
+ done();
2245
+ })
2246
+ .catch((e) => {
2247
+ fail(e);
2248
+ });
2249
+ });
2250
+ });
2251
+
2252
+ it("should still send same-origin credentials even if another domain is listed in authorizedCrossOriginDomains", (done) => {
2253
+ const session = new UserSession({
2254
+ clientId: "id",
2255
+ token: "token",
2256
+ refreshToken: "refresh",
2257
+ tokenExpires: TOMORROW,
2258
+ portal: "https://gis.city.gov/sharing/rest",
2259
+ });
2260
+
2261
+ fetchMock.postOnce("https://gisservices.city.gov/public/rest/info", {
2262
+ currentVersion: 10.51,
2263
+ fullVersion: "10.5.1.120",
2264
+ owningSystemUrl: "https://gis.city.gov",
2265
+ authInfo: {
2266
+ isTokenBasedSecurity: true,
2267
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
2268
+ },
2269
+ });
2270
+
2271
+ fetchMock.getOnce(
2272
+ "https://gis.city.gov/sharing/rest/portals/self?f=json&token=token",
2273
+ {
2274
+ authorizedCrossOriginDomains: ["https://other.city.gov"],
2275
+ }
2276
+ );
2277
+
2278
+ fetchMock.postOnce("https://gis.city.gov/sharing/rest/info", {
2279
+ owningSystemUrl: "http://gis.city.gov",
2280
+ authInfo: {
2281
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
2282
+ isTokenBasedSecurity: true,
2283
+ },
2284
+ });
2285
+
2286
+ fetchMock.postOnce("https://gis.city.gov/sharing/generateToken", {
2287
+ token: "serverToken",
2288
+ expires: TOMORROW,
2289
+ });
2290
+
2291
+ fetchMock.post(
2292
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query",
2293
+ {
2294
+ count: 123,
2295
+ }
2296
+ );
2297
+
2298
+ request(
2299
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query",
2300
+ {
2301
+ authentication: session,
2302
+ }
2303
+ )
2304
+ .then((response) => {
2305
+ const { credentials } = fetchMock.lastOptions(
2306
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
2307
+ );
2308
+ expect(credentials).toEqual("same-origin");
2309
+
2310
+ done();
2311
+ })
2312
+ .catch((e) => {
2313
+ fail(e);
2314
+ });
2315
+ });
2316
+
2317
+ it("should normalize optional protocols in authorizedCrossOriginDomains", (done) => {
2318
+ const session = new UserSession({
2319
+ clientId: "id",
2320
+ token: "token",
2321
+ refreshToken: "refresh",
2322
+ tokenExpires: TOMORROW,
2323
+ portal: "https://gis.city.gov/sharing/rest",
2324
+ });
2325
+
2326
+ fetchMock.getOnce(
2327
+ "https://gis.city.gov/sharing/rest/portals/self?f=json&token=token",
2328
+ {
2329
+ authorizedCrossOriginDomains: ["one.city.gov", "https://two.city.gov"],
2330
+ }
2331
+ );
2332
+
2333
+ (session as any)
2334
+ .fetchAuthorizedDomains()
2335
+ .then(() => {
2336
+ expect((session as any).trustedDomains).toEqual([
2337
+ "https://one.city.gov",
2338
+ "https://two.city.gov",
2339
+ ]);
2340
+ done();
2341
+ })
2342
+ .catch((e: Error) => {
2343
+ fail(e);
2344
+ });
2345
+ });
2346
+
2347
+ it("should not use domain credentials if portal is null", (done) => {
2348
+ const session = new UserSession({
2349
+ clientId: "id",
2350
+ token: "token",
2351
+ refreshToken: "refresh",
2352
+ tokenExpires: TOMORROW,
2353
+ portal: null,
2354
+ server: "https://fakeserver.com/arcgis",
2355
+ });
2356
+
2357
+ (session as any)
2358
+ .fetchAuthorizedDomains()
2359
+ .then(() => {
2360
+ done();
2361
+ })
2362
+ .catch((e: Error) => {
2363
+ fail(e);
2364
+ });
2365
+ });
2366
+
2367
+ it("should deprecate trustedServers", () => {
2368
+ const session = new UserSession({
2369
+ clientId: "id",
2370
+ token: "token",
2371
+ });
2372
+
2373
+ expect((session as any).trustedServers).toBe(
2374
+ (session as any).federatedServers
2375
+ );
2376
+ });
2377
+ });