aspernaturalias 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (370) hide show
  1. package/.prettierignore +2 -0
  2. package/.travis.yml +31 -0
  3. package/.vscode/launch.json +24 -0
  4. package/.vscode/settings.json +3 -0
  5. package/codecov.yml +3 -0
  6. package/demos/ago-node-cli/README.md +29 -0
  7. package/demos/ago-node-cli/ago.js +33 -0
  8. package/demos/ago-node-cli/index.js +11 -0
  9. package/demos/ago-node-cli/lib/item-export-command.js +48 -0
  10. package/demos/ago-node-cli/lib/item-search-command.js +35 -0
  11. package/demos/ago-node-cli/package-lock.json +152 -0
  12. package/demos/ago-node-cli/package.json +30 -0
  13. package/demos/attachments/README.md +5 -0
  14. package/demos/attachments/index.html +164 -0
  15. package/demos/attachments/package-lock.json +182 -0
  16. package/demos/attachments/package.json +18 -0
  17. package/demos/batch-geocoder-node/NYC_Restaurant_Inspection_Results.csv +100 -0
  18. package/demos/batch-geocoder-node/README.md +15 -0
  19. package/demos/batch-geocoder-node/batch-geocode.js +115 -0
  20. package/demos/batch-geocoder-node/config-template.js +18 -0
  21. package/demos/batch-geocoder-node/package-lock.json +94 -0
  22. package/demos/batch-geocoder-node/package.json +38 -0
  23. package/demos/express/README.md +15 -0
  24. package/demos/express/config.json.template +3 -0
  25. package/demos/express/package-lock.json +413 -0
  26. package/demos/express/package.json +18 -0
  27. package/demos/express/server.js +33 -0
  28. package/demos/feature-service-browser/README.md +6 -0
  29. package/demos/feature-service-browser/index.html +122 -0
  30. package/demos/feature-service-browser/package-lock.json +182 -0
  31. package/demos/feature-service-browser/package.json +18 -0
  32. package/demos/geocoder-browser/README.md +10 -0
  33. package/demos/geocoder-browser/config.js.template +1 -0
  34. package/demos/geocoder-browser/index.html +131 -0
  35. package/demos/geocoder-browser/package-lock.json +182 -0
  36. package/demos/geocoder-browser/package.json +19 -0
  37. package/demos/geocoder-browser/post-sign-in.html +25 -0
  38. package/demos/jsapi-integration/README.md +25 -0
  39. package/demos/jsapi-integration/config.js +6 -0
  40. package/demos/jsapi-integration/index.html +85 -0
  41. package/demos/jsapi-integration/package-lock.json +184 -0
  42. package/demos/jsapi-integration/package.json +19 -0
  43. package/demos/node-cli-item-management/README.md +10 -0
  44. package/demos/node-cli-item-management/index.js +238 -0
  45. package/demos/node-cli-item-management/package-lock.json +152 -0
  46. package/demos/node-cli-item-management/package.json +27 -0
  47. package/demos/node-cli-item-management/screenshot.png +0 -0
  48. package/demos/oauth2-browser/README.md +14 -0
  49. package/demos/oauth2-browser/authenticate.html +30 -0
  50. package/demos/oauth2-browser/config.js.template +6 -0
  51. package/demos/oauth2-browser/index.html +202 -0
  52. package/demos/oauth2-browser/logo.svg +4 -0
  53. package/demos/oauth2-browser/package-lock.json +163 -0
  54. package/demos/oauth2-browser/package.json +18 -0
  55. package/demos/oauth2-browser/style.css +36 -0
  56. package/demos/oauth2-browser-retry/README.md +25 -0
  57. package/demos/oauth2-browser-retry/authenticate.html +22 -0
  58. package/demos/oauth2-browser-retry/index.html +116 -0
  59. package/demos/oauth2-browser-retry/logo.svg +4 -0
  60. package/demos/stream-response-to-file/README.md +7 -0
  61. package/demos/stream-response-to-file/index.js +36 -0
  62. package/demos/stream-response-to-file/output/.gitkeep +0 -0
  63. package/demos/stream-response-to-file/package-lock.json +60 -0
  64. package/demos/stream-response-to-file/package.json +33 -0
  65. package/demos/tree-shaking-rollup/.babelrc +3 -0
  66. package/demos/tree-shaking-rollup/README.md +9 -0
  67. package/demos/tree-shaking-rollup/index.html +11 -0
  68. package/demos/tree-shaking-rollup/package-lock.json +2225 -0
  69. package/demos/tree-shaking-rollup/package.json +25 -0
  70. package/demos/tree-shaking-rollup/rollup.config.js +17 -0
  71. package/demos/tree-shaking-rollup/src/index.js +8 -0
  72. package/demos/tree-shaking-webpack/README.md +9 -0
  73. package/demos/tree-shaking-webpack/index.html +11 -0
  74. package/demos/tree-shaking-webpack/package-lock.json +4595 -0
  75. package/demos/tree-shaking-webpack/package.json +24 -0
  76. package/demos/tree-shaking-webpack/src/index.js +10 -0
  77. package/demos/tree-shaking-webpack/webpack.config.js +27 -0
  78. package/demos/vue/.env.example +11 -0
  79. package/demos/vue/.eslintrc.js +17 -0
  80. package/demos/vue/.postcssrc.js +5 -0
  81. package/demos/vue/README.md +17 -0
  82. package/demos/vue/babel.config.js +3 -0
  83. package/demos/vue/package-lock.json +11323 -0
  84. package/demos/vue/package.json +33 -0
  85. package/demos/vue/public/favicon.ico +0 -0
  86. package/demos/vue/public/index.html +24 -0
  87. package/demos/vue/src/assets/logo.svg +29 -0
  88. package/demos/vue/src/components/App.vue +305 -0
  89. package/demos/vue/src/components/Authenticate.vue +65 -0
  90. package/demos/vue/src/components/Loader.vue +230 -0
  91. package/demos/vue/src/main.js +92 -0
  92. package/demos/webmap-checker-sapper/.env.example +5 -0
  93. package/demos/webmap-checker-sapper/README.md +123 -0
  94. package/demos/webmap-checker-sapper/appveyor.yml +18 -0
  95. package/demos/webmap-checker-sapper/cypress/fixtures/example.json +5 -0
  96. package/demos/webmap-checker-sapper/cypress/integration/spec.js +19 -0
  97. package/demos/webmap-checker-sapper/cypress/plugins/index.js +17 -0
  98. package/demos/webmap-checker-sapper/cypress/support/commands.js +25 -0
  99. package/demos/webmap-checker-sapper/cypress/support/index.js +20 -0
  100. package/demos/webmap-checker-sapper/cypress.json +4 -0
  101. package/demos/webmap-checker-sapper/package-lock.json +3851 -0
  102. package/demos/webmap-checker-sapper/package.json +50 -0
  103. package/demos/webmap-checker-sapper/rollup.config.js +87 -0
  104. package/demos/webmap-checker-sapper/src/client.js +20 -0
  105. package/demos/webmap-checker-sapper/src/components/LayerStatus.html +108 -0
  106. package/demos/webmap-checker-sapper/src/components/Nav.html +21 -0
  107. package/demos/webmap-checker-sapper/src/components/WebMap.html +62 -0
  108. package/demos/webmap-checker-sapper/src/routes/_error.html +41 -0
  109. package/demos/webmap-checker-sapper/src/routes/_layout.html +21 -0
  110. package/demos/webmap-checker-sapper/src/routes/auth/authorize.js +18 -0
  111. package/demos/webmap-checker-sapper/src/routes/auth/exchange-token.js +20 -0
  112. package/demos/webmap-checker-sapper/src/routes/auth/post-sign-in.js +24 -0
  113. package/demos/webmap-checker-sapper/src/routes/auth/sign-out.js +10 -0
  114. package/demos/webmap-checker-sapper/src/routes/index.html +20 -0
  115. package/demos/webmap-checker-sapper/src/routes/webmaps/[webmapId].html +83 -0
  116. package/demos/webmap-checker-sapper/src/routes/webmaps/index.html +59 -0
  117. package/demos/webmap-checker-sapper/src/server.js +101 -0
  118. package/demos/webmap-checker-sapper/src/service-worker.js +82 -0
  119. package/demos/webmap-checker-sapper/src/template.html +33 -0
  120. package/demos/webmap-checker-sapper/src/userInfoMiddleware.js +21 -0
  121. package/demos/webmap-checker-sapper/src/utils.js +33 -0
  122. package/demos/webmap-checker-sapper/static/favicon.png +0 -0
  123. package/demos/webmap-checker-sapper/static/global.css +36 -0
  124. package/demos/webmap-checker-sapper/static/manifest.json +20 -0
  125. package/demos/webmap-checker-sapper/static/svelte-logo-192.png +0 -0
  126. package/demos/webmap-checker-sapper/static/svelte-logo-512.png +0 -0
  127. package/docs/FAQ.md +48 -0
  128. package/docs/HISTORY.md +62 -0
  129. package/docs/acetate.config.js +262 -0
  130. package/docs/build-typedoc.js +434 -0
  131. package/docs/generate-srihashes.js +53 -0
  132. package/docs/src/_layout.html +86 -0
  133. package/docs/src/api/_declaration.html +600 -0
  134. package/docs/src/api/_layout.html +204 -0
  135. package/docs/src/api/_package.html +38 -0
  136. package/docs/src/api/index.html +16 -0
  137. package/docs/src/guides/_layout.html +24 -0
  138. package/docs/src/guides/amd-requirejs-dojo.md +40 -0
  139. package/docs/src/guides/browser-authentication.md +41 -0
  140. package/docs/src/guides/bundlers.md +52 -0
  141. package/docs/src/guides/cli-authentication.md +9 -0
  142. package/docs/src/guides/client-server-authentication.md +9 -0
  143. package/docs/src/guides/from-a-cdn.md +38 -0
  144. package/docs/src/guides/index.md +59 -0
  145. package/docs/src/guides/node.md +87 -0
  146. package/docs/src/guides/package-overview.md +111 -0
  147. package/docs/src/guides/server-authentication.md +9 -0
  148. package/docs/src/guides/whats-new-v2-0.md +305 -0
  149. package/docs/src/img/icons.png +0 -0
  150. package/docs/src/img/icons@2x.png +0 -0
  151. package/docs/src/index.html +12 -0
  152. package/docs/src/js/api-search.js +112 -0
  153. package/docs/src/js/nav-toggle.js +41 -0
  154. package/docs/src/sass/_highlight.scss +96 -0
  155. package/docs/src/sass/_icons.scss +157 -0
  156. package/docs/src/sass/style.scss +242 -0
  157. package/docs/src/srihashes.json +12 -0
  158. package/jasmine.json +7 -0
  159. package/karma.conf.js +106 -0
  160. package/lerna.json +8 -0
  161. package/notes/README.md +88 -0
  162. package/package.json +129 -0
  163. package/packages/arcgis-rest-auth/README.md +71 -0
  164. package/packages/arcgis-rest-auth/package-lock.json +11 -0
  165. package/packages/arcgis-rest-auth/package.json +69 -0
  166. package/packages/arcgis-rest-auth/src/ApplicationSession.ts +122 -0
  167. package/packages/arcgis-rest-auth/src/UserSession.ts +997 -0
  168. package/packages/arcgis-rest-auth/src/authenticated-request-options.ts +24 -0
  169. package/packages/arcgis-rest-auth/src/fetch-token.ts +50 -0
  170. package/packages/arcgis-rest-auth/src/generate-token.ts +35 -0
  171. package/packages/arcgis-rest-auth/src/index.ts +9 -0
  172. package/packages/arcgis-rest-auth/test/ApplicationSession.test.ts +124 -0
  173. package/packages/arcgis-rest-auth/test/UserSession.test.ts +1315 -0
  174. package/packages/arcgis-rest-auth/test/fetchToken.test.ts +112 -0
  175. package/packages/arcgis-rest-auth/test/generateToken.test.ts +102 -0
  176. package/packages/arcgis-rest-auth/test/utils.ts +14 -0
  177. package/packages/arcgis-rest-auth/tsconfig.json +6 -0
  178. package/packages/arcgis-rest-feature-layer/README.md +77 -0
  179. package/packages/arcgis-rest-feature-layer/package-lock.json +11 -0
  180. package/packages/arcgis-rest-feature-layer/package.json +64 -0
  181. package/packages/arcgis-rest-feature-layer/src/add.ts +56 -0
  182. package/packages/arcgis-rest-feature-layer/src/addAttachment.ts +53 -0
  183. package/packages/arcgis-rest-feature-layer/src/decodeValues.ts +124 -0
  184. package/packages/arcgis-rest-feature-layer/src/delete.ts +61 -0
  185. package/packages/arcgis-rest-feature-layer/src/deleteAttachments.ts +52 -0
  186. package/packages/arcgis-rest-feature-layer/src/getAttachments.ts +55 -0
  187. package/packages/arcgis-rest-feature-layer/src/getLayer.ts +24 -0
  188. package/packages/arcgis-rest-feature-layer/src/helpers.ts +55 -0
  189. package/packages/arcgis-rest-feature-layer/src/index.ts +28 -0
  190. package/packages/arcgis-rest-feature-layer/src/query.ts +203 -0
  191. package/packages/arcgis-rest-feature-layer/src/queryRelated.ts +89 -0
  192. package/packages/arcgis-rest-feature-layer/src/update.ts +56 -0
  193. package/packages/arcgis-rest-feature-layer/src/updateAttachment.ts +59 -0
  194. package/packages/arcgis-rest-feature-layer/test/attachments.test.ts +202 -0
  195. package/packages/arcgis-rest-feature-layer/test/crud.test.ts +130 -0
  196. package/packages/arcgis-rest-feature-layer/test/decodeValues.test.ts +67 -0
  197. package/packages/arcgis-rest-feature-layer/test/getLayer.test.ts +31 -0
  198. package/packages/arcgis-rest-feature-layer/test/mocks/cvdQueryResponse.ts +225 -0
  199. package/packages/arcgis-rest-feature-layer/test/mocks/feature.ts +281 -0
  200. package/packages/arcgis-rest-feature-layer/test/mocks/fields.ts +779 -0
  201. package/packages/arcgis-rest-feature-layer/test/mocks/foo.txt +1 -0
  202. package/packages/arcgis-rest-feature-layer/test/mocks/service.ts +259 -0
  203. package/packages/arcgis-rest-feature-layer/test/query.test.ts +168 -0
  204. package/packages/arcgis-rest-feature-layer/tsconfig.json +6 -0
  205. package/packages/arcgis-rest-geocoding/README.md +86 -0
  206. package/packages/arcgis-rest-geocoding/package-lock.json +11 -0
  207. package/packages/arcgis-rest-geocoding/package.json +64 -0
  208. package/packages/arcgis-rest-geocoding/src/bulk.ts +105 -0
  209. package/packages/arcgis-rest-geocoding/src/geocode.ts +130 -0
  210. package/packages/arcgis-rest-geocoding/src/helpers.ts +54 -0
  211. package/packages/arcgis-rest-geocoding/src/index.ts +15 -0
  212. package/packages/arcgis-rest-geocoding/src/reverse.ts +84 -0
  213. package/packages/arcgis-rest-geocoding/src/suggest.ts +45 -0
  214. package/packages/arcgis-rest-geocoding/test/bulk.test.ts +151 -0
  215. package/packages/arcgis-rest-geocoding/test/geocode.test.ts +212 -0
  216. package/packages/arcgis-rest-geocoding/test/helpers.test.ts +85 -0
  217. package/packages/arcgis-rest-geocoding/test/mocks/responses.ts +637 -0
  218. package/packages/arcgis-rest-geocoding/test/reverse.test.ts +126 -0
  219. package/packages/arcgis-rest-geocoding/test/suggest.test.ts +53 -0
  220. package/packages/arcgis-rest-geocoding/tsconfig.json +6 -0
  221. package/packages/arcgis-rest-portal/README.md +73 -0
  222. package/packages/arcgis-rest-portal/package-lock.json +11 -0
  223. package/packages/arcgis-rest-portal/package.json +64 -0
  224. package/packages/arcgis-rest-portal/src/groups/create.ts +43 -0
  225. package/packages/arcgis-rest-portal/src/groups/get.ts +99 -0
  226. package/packages/arcgis-rest-portal/src/groups/helpers.ts +14 -0
  227. package/packages/arcgis-rest-portal/src/groups/join.ts +57 -0
  228. package/packages/arcgis-rest-portal/src/groups/notification.ts +77 -0
  229. package/packages/arcgis-rest-portal/src/groups/protect.ts +56 -0
  230. package/packages/arcgis-rest-portal/src/groups/remove.ts +32 -0
  231. package/packages/arcgis-rest-portal/src/groups/search.ts +25 -0
  232. package/packages/arcgis-rest-portal/src/groups/update.ts +39 -0
  233. package/packages/arcgis-rest-portal/src/index.ts +48 -0
  234. package/packages/arcgis-rest-portal/src/items/add.ts +137 -0
  235. package/packages/arcgis-rest-portal/src/items/create.ts +128 -0
  236. package/packages/arcgis-rest-portal/src/items/get.ts +177 -0
  237. package/packages/arcgis-rest-portal/src/items/helpers.ts +193 -0
  238. package/packages/arcgis-rest-portal/src/items/protect.ts +39 -0
  239. package/packages/arcgis-rest-portal/src/items/remove.ts +131 -0
  240. package/packages/arcgis-rest-portal/src/items/search.ts +25 -0
  241. package/packages/arcgis-rest-portal/src/items/update.ts +145 -0
  242. package/packages/arcgis-rest-portal/src/sharing/access.ts +84 -0
  243. package/packages/arcgis-rest-portal/src/sharing/group-sharing.ts +210 -0
  244. package/packages/arcgis-rest-portal/src/sharing/helpers.ts +84 -0
  245. package/packages/arcgis-rest-portal/src/users/get-user-url.ts +18 -0
  246. package/packages/arcgis-rest-portal/src/users/get-user.ts +58 -0
  247. package/packages/arcgis-rest-portal/src/users/invitation.ts +162 -0
  248. package/packages/arcgis-rest-portal/src/users/notification.ts +70 -0
  249. package/packages/arcgis-rest-portal/src/users/update.ts +66 -0
  250. package/packages/arcgis-rest-portal/src/util/SearchQueryBuilder.ts +374 -0
  251. package/packages/arcgis-rest-portal/src/util/generic-search.ts +67 -0
  252. package/packages/arcgis-rest-portal/src/util/get-portal-url.ts +27 -0
  253. package/packages/arcgis-rest-portal/src/util/get-portal.ts +52 -0
  254. package/packages/arcgis-rest-portal/src/util/search.ts +38 -0
  255. package/packages/arcgis-rest-portal/test/groups/crud.test.ts +180 -0
  256. package/packages/arcgis-rest-portal/test/groups/get.test.ts +101 -0
  257. package/packages/arcgis-rest-portal/test/groups/join.test.ts +72 -0
  258. package/packages/arcgis-rest-portal/test/groups/notification.test.ts +112 -0
  259. package/packages/arcgis-rest-portal/test/groups/protect.test.ts +72 -0
  260. package/packages/arcgis-rest-portal/test/groups/search.test.ts +110 -0
  261. package/packages/arcgis-rest-portal/test/items/add.test.ts +321 -0
  262. package/packages/arcgis-rest-portal/test/items/create.test.ts +375 -0
  263. package/packages/arcgis-rest-portal/test/items/get.test.ts +309 -0
  264. package/packages/arcgis-rest-portal/test/items/protect.test.ts +122 -0
  265. package/packages/arcgis-rest-portal/test/items/remove.test.ts +238 -0
  266. package/packages/arcgis-rest-portal/test/items/search.test.ts +272 -0
  267. package/packages/arcgis-rest-portal/test/items/update.test.ts +515 -0
  268. package/packages/arcgis-rest-portal/test/mocks/groups/responses.ts +144 -0
  269. package/packages/arcgis-rest-portal/test/mocks/items/foo.zip +0 -0
  270. package/packages/arcgis-rest-portal/test/mocks/items/item.ts +122 -0
  271. package/packages/arcgis-rest-portal/test/mocks/items/resources.ts +31 -0
  272. package/packages/arcgis-rest-portal/test/mocks/items/search.ts +121 -0
  273. package/packages/arcgis-rest-portal/test/mocks/portal/response.ts +112 -0
  274. package/packages/arcgis-rest-portal/test/mocks/sharing/sharing.ts +18 -0
  275. package/packages/arcgis-rest-portal/test/mocks/users/invitation.ts +70 -0
  276. package/packages/arcgis-rest-portal/test/mocks/users/notification.ts +34 -0
  277. package/packages/arcgis-rest-portal/test/mocks/users/user.ts +173 -0
  278. package/packages/arcgis-rest-portal/test/sharing/access.test.ts +162 -0
  279. package/packages/arcgis-rest-portal/test/sharing/group-sharing.test.ts +650 -0
  280. package/packages/arcgis-rest-portal/test/sharing/helpers.test.ts +55 -0
  281. package/packages/arcgis-rest-portal/test/users/get-user-url.test.ts +40 -0
  282. package/packages/arcgis-rest-portal/test/users/get-user.test.ts +90 -0
  283. package/packages/arcgis-rest-portal/test/users/invitation.test.ts +127 -0
  284. package/packages/arcgis-rest-portal/test/users/notification.test.ts +77 -0
  285. package/packages/arcgis-rest-portal/test/users/update.test.ts +151 -0
  286. package/packages/arcgis-rest-portal/test/util/SearchQueryBuilder.test.ts +340 -0
  287. package/packages/arcgis-rest-portal/test/util/get-portal-url.test.ts +37 -0
  288. package/packages/arcgis-rest-portal/test/util/portal.test.ts +97 -0
  289. package/packages/arcgis-rest-portal/tsconfig.json +6 -0
  290. package/packages/arcgis-rest-request/README.md +72 -0
  291. package/packages/arcgis-rest-request/package-lock.json +11 -0
  292. package/packages/arcgis-rest-request/package.json +60 -0
  293. package/packages/arcgis-rest-request/src/index.ts +24 -0
  294. package/packages/arcgis-rest-request/src/request.ts +389 -0
  295. package/packages/arcgis-rest-request/src/utils/ArcGISRequestError.ts +76 -0
  296. package/packages/arcgis-rest-request/src/utils/ErrorTypes.ts +29 -0
  297. package/packages/arcgis-rest-request/src/utils/GrantTypes.ts +5 -0
  298. package/packages/arcgis-rest-request/src/utils/HTTPMethods.ts +6 -0
  299. package/packages/arcgis-rest-request/src/utils/IAuthenticationManager.ts +21 -0
  300. package/packages/arcgis-rest-request/src/utils/IFetchTokenParams.ts +11 -0
  301. package/packages/arcgis-rest-request/src/utils/IGenerateTokenParams.ts +9 -0
  302. package/packages/arcgis-rest-request/src/utils/IParamBuilder.ts +3 -0
  303. package/packages/arcgis-rest-request/src/utils/IParams.ts +6 -0
  304. package/packages/arcgis-rest-request/src/utils/IParamsBuilder.ts +5 -0
  305. package/packages/arcgis-rest-request/src/utils/IRequestOptions.ts +43 -0
  306. package/packages/arcgis-rest-request/src/utils/ITokenRequestOptions.ts +9 -0
  307. package/packages/arcgis-rest-request/src/utils/ResponseFormats.ts +10 -0
  308. package/packages/arcgis-rest-request/src/utils/append-custom-params.ts +49 -0
  309. package/packages/arcgis-rest-request/src/utils/clean-url.ts +16 -0
  310. package/packages/arcgis-rest-request/src/utils/encode-form-data.ts +38 -0
  311. package/packages/arcgis-rest-request/src/utils/encode-query-string.ts +23 -0
  312. package/packages/arcgis-rest-request/src/utils/process-params.ts +109 -0
  313. package/packages/arcgis-rest-request/src/utils/retryAuthError.ts +10 -0
  314. package/packages/arcgis-rest-request/src/utils/warn.ts +11 -0
  315. package/packages/arcgis-rest-request/src/utils/with-options.ts +48 -0
  316. package/packages/arcgis-rest-request/test/mocks/errors.ts +76 -0
  317. package/packages/arcgis-rest-request/test/mocks/geojson-feature-collection.ts +13 -0
  318. package/packages/arcgis-rest-request/test/mocks/param-builder.ts +7 -0
  319. package/packages/arcgis-rest-request/test/mocks/sharing-rest-info.ts +41 -0
  320. package/packages/arcgis-rest-request/test/mocks/webmap.ts +41 -0
  321. package/packages/arcgis-rest-request/test/request.test.ts +496 -0
  322. package/packages/arcgis-rest-request/test/utils/ArcGISAuthError.test.ts +191 -0
  323. package/packages/arcgis-rest-request/test/utils/ArcGISRequestError.test.ts +51 -0
  324. package/packages/arcgis-rest-request/test/utils/check-for-errors.test.ts +111 -0
  325. package/packages/arcgis-rest-request/test/utils/clean-url.test.ts +47 -0
  326. package/packages/arcgis-rest-request/test/utils/encode-form-data.test.ts +133 -0
  327. package/packages/arcgis-rest-request/test/utils/process-params.test.ts +193 -0
  328. package/packages/arcgis-rest-request/test/utils/with-options.test.ts +133 -0
  329. package/packages/arcgis-rest-request/tsconfig.json +4 -0
  330. package/packages/arcgis-rest-routing/README.md +75 -0
  331. package/packages/arcgis-rest-routing/package-lock.json +11 -0
  332. package/packages/arcgis-rest-routing/package.json +63 -0
  333. package/packages/arcgis-rest-routing/src/helpers.ts +16 -0
  334. package/packages/arcgis-rest-routing/src/index.ts +11 -0
  335. package/packages/arcgis-rest-routing/src/solveRoute.ts +124 -0
  336. package/packages/arcgis-rest-routing/test/mocks/responses.ts +825 -0
  337. package/packages/arcgis-rest-routing/test/solveRoute.test.ts +509 -0
  338. package/packages/arcgis-rest-routing/tsconfig.json +6 -0
  339. package/packages/arcgis-rest-service-admin/README.md +73 -0
  340. package/packages/arcgis-rest-service-admin/package-lock.json +11 -0
  341. package/packages/arcgis-rest-service-admin/package.json +65 -0
  342. package/packages/arcgis-rest-service-admin/src/addTo.ts +70 -0
  343. package/packages/arcgis-rest-service-admin/src/create.ts +213 -0
  344. package/packages/arcgis-rest-service-admin/src/index.ts +9 -0
  345. package/packages/arcgis-rest-service-admin/test/addTo.test.ts +350 -0
  346. package/packages/arcgis-rest-service-admin/test/create.test.ts +377 -0
  347. package/packages/arcgis-rest-service-admin/test/mocks/layerDefinition.ts +79 -0
  348. package/packages/arcgis-rest-service-admin/test/mocks/move.ts +11 -0
  349. package/packages/arcgis-rest-service-admin/test/mocks/service.ts +69 -0
  350. package/packages/arcgis-rest-service-admin/tsconfig.json +6 -0
  351. package/packages/arcgis-rest-types/README.md +66 -0
  352. package/packages/arcgis-rest-types/package.json +54 -0
  353. package/packages/arcgis-rest-types/src/feature.ts +39 -0
  354. package/packages/arcgis-rest-types/src/geometry.ts +272 -0
  355. package/packages/arcgis-rest-types/src/group.ts +72 -0
  356. package/packages/arcgis-rest-types/src/index.ts +7 -0
  357. package/packages/arcgis-rest-types/src/item.ts +65 -0
  358. package/packages/arcgis-rest-types/src/symbol.ts +170 -0
  359. package/packages/arcgis-rest-types/src/user.ts +48 -0
  360. package/packages/arcgis-rest-types/src/webmap.ts +1345 -0
  361. package/packages/arcgis-rest-types/tsconfig.json +9 -0
  362. package/support/changelog.js +393 -0
  363. package/support/deploy-doc-site.js +16 -0
  364. package/support/dev.sh +6 -0
  365. package/support/publish.sh +47 -0
  366. package/support/test-helpers.js +9 -0
  367. package/tsconfig.json +63 -0
  368. package/tslint.json +16 -0
  369. package/umd-base-profile.js +81 -0
  370. package/umd-production-profile.js +13 -0
@@ -0,0 +1,1315 @@
1
+ /* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
2
+ * Apache-2.0 */
3
+
4
+ import { UserSession } from "../src/index";
5
+ import { ICredential } from "../src/UserSession";
6
+
7
+ import {
8
+ request,
9
+ ArcGISRequestError,
10
+ ArcGISAuthError,
11
+ ErrorTypes
12
+ } from "@esri/arcgis-rest-request";
13
+ import * as fetchMock from "fetch-mock";
14
+ import { YESTERDAY, TOMORROW } from "./utils";
15
+
16
+ describe("UserSession", () => {
17
+ afterEach(fetchMock.restore);
18
+
19
+ it("should serialize to and from JSON", () => {
20
+ const session = new UserSession({
21
+ clientId: "clientId",
22
+ redirectUri: "https://example-app.com/redirect-uri",
23
+ token: "token",
24
+ tokenExpires: TOMORROW,
25
+ refreshToken: "refreshToken",
26
+ refreshTokenExpires: TOMORROW,
27
+ refreshTokenTTL: 1440,
28
+ username: "c@sey",
29
+ password: "123456"
30
+ });
31
+
32
+ const session2 = UserSession.deserialize(session.serialize());
33
+
34
+ expect(session2.clientId).toEqual("clientId");
35
+ expect(session2.redirectUri).toEqual(
36
+ "https://example-app.com/redirect-uri"
37
+ );
38
+ expect(session2.ssl).toBe(undefined);
39
+ expect(session2.token).toEqual("token");
40
+ expect(session2.tokenExpires).toEqual(TOMORROW);
41
+ expect(session2.refreshToken).toEqual("refreshToken");
42
+ expect(session2.refreshTokenExpires).toEqual(TOMORROW);
43
+ expect(session2.username).toEqual("c@sey");
44
+ expect(session2.password).toEqual("123456");
45
+ expect(session2.tokenDuration).toEqual(20160);
46
+ expect(session2.refreshTokenTTL).toEqual(1440);
47
+ });
48
+
49
+ describe(".getToken()", () => {
50
+ it("should return unexpired tokens for trusted arcgis.com domains", done => {
51
+ const session = new UserSession({
52
+ clientId: "id",
53
+ token: "token",
54
+ tokenExpires: TOMORROW
55
+ });
56
+
57
+ Promise.all([
58
+ session.getToken("https://www.arcgis.com/sharing/rest/portals/self"),
59
+ session.getToken(
60
+ "https://services1.arcgis.com/MOCK_ORG/arcgis/rest/services/Private_Service/FeatureServer"
61
+ )
62
+ ])
63
+ .then(([token1, token2]) => {
64
+ expect(token1).toBe("token");
65
+ expect(token2).toBe("token");
66
+ done();
67
+ })
68
+ .catch(e => {
69
+ fail(e);
70
+ });
71
+ });
72
+
73
+ it("should return unexpired tokens when an org url is passed", done => {
74
+ const session = new UserSession({
75
+ clientId: "id",
76
+ token: "token",
77
+ tokenExpires: TOMORROW,
78
+ portal: "https://custom.maps.arcgis.com/sharing/rest"
79
+ });
80
+
81
+ session
82
+ .getToken(
83
+ "https://services1.arcgis.com/MOCK_ORG/arcgis/rest/services/Private_Service/FeatureServer"
84
+ )
85
+ .then(token => {
86
+ expect(token).toBe("token");
87
+ done();
88
+ })
89
+ .catch(e => {
90
+ fail(e);
91
+ });
92
+ });
93
+
94
+ it("should return unexpired tokens for the configured portal domain", done => {
95
+ const session = new UserSession({
96
+ clientId: "id",
97
+ token: "token",
98
+ tokenExpires: TOMORROW,
99
+ portal: "https://gis.city.gov/sharing/rest"
100
+ });
101
+
102
+ session
103
+ .getToken("https://gis.city.gov/sharing/rest/portals/self")
104
+ .then(token => {
105
+ expect(token).toBe("token");
106
+ done();
107
+ })
108
+ .catch(e => {
109
+ fail(e);
110
+ });
111
+ });
112
+
113
+ it("should return unexpired tokens for the configured portal domain, regardless of CASING", done => {
114
+ // This was a real configuration discovered on a portal instance
115
+ const session = new UserSession({
116
+ clientId: "id",
117
+ token: "token",
118
+ tokenExpires: TOMORROW,
119
+ portal: "https://pnp00035.esri.com/sharing/rest"
120
+ });
121
+
122
+ session
123
+ .getToken("https://PNP00035.esri.com/sharing/rest/portals/self")
124
+ .then(token => {
125
+ expect(token).toBe("token");
126
+ done();
127
+ })
128
+ .catch(e => {
129
+ fail(e);
130
+ });
131
+ });
132
+
133
+ it("should fetch token when contacting a server that is federated, even if on same domain, regardless of domain casing", done => {
134
+ // This was a real configuration discovered on a portal instance
135
+ // apparently when federating servers, the UI does not force the
136
+ // server url to lowercase, and this any feature service items generated
137
+ // will have the server name using the casing the admin entered.
138
+ // this is just a test to ensure that the mis-matched casing does not
139
+ // break the federation flow.
140
+ const session = new UserSession({
141
+ clientId: "id",
142
+ token: "existing-session-token",
143
+ refreshToken: "refresh",
144
+ tokenExpires: TOMORROW,
145
+ portal: "https://pnp00035.esri.com/portal/sharing/rest"
146
+ });
147
+
148
+ fetchMock.postOnce("https://pnp00035.esri.com/server/rest/info", {
149
+ currentVersion: 10.61,
150
+ fullVersion: "10.6.1",
151
+ owningSystemUrl: "https://pnp00035.esri.com/portal",
152
+ authInfo: {
153
+ isTokenBasedSecurity: true,
154
+ tokenServicesUrl:
155
+ "https://pnp00035.esri.com/portal/sharing/rest/generateToken"
156
+ }
157
+ });
158
+
159
+ fetchMock.postOnce("https://pnp00035.esri.com/portal/sharing/rest/info", {
160
+ owningSystemUrl: "https://pnp00035.esri.com/portal",
161
+ authInfo: {
162
+ tokenServicesUrl:
163
+ "https://pnp00035.esri.com/portal/sharing/rest/generateToken",
164
+ isTokenBasedSecurity: true
165
+ }
166
+ });
167
+
168
+ fetchMock.postOnce(
169
+ "https://pnp00035.esri.com/portal/sharing/rest/generateToken",
170
+ {
171
+ token: "new-server-token",
172
+ expires: TOMORROW
173
+ }
174
+ );
175
+
176
+ // request the token twice, for the same domain, but with different casing
177
+ // and we expect a single POST to generate a token once
178
+ session
179
+ .getToken(
180
+ "https://PNP00035.esri.com/server/rest/services/Hosted/perimeters_dd83/FeatureServer"
181
+ )
182
+ .then(token => {
183
+ expect(token).toBe("new-server-token");
184
+ return session.getToken(
185
+ "https://pnp00035.esri.com/server/rest/services/Hosted/otherService/FeatureServer"
186
+ );
187
+ })
188
+ .then(token => {
189
+ expect(token).toBe("new-server-token");
190
+ done();
191
+ })
192
+ .catch(e => {
193
+ fail(e);
194
+ });
195
+ });
196
+
197
+ it("should fetch new tokens when tokens for trusted arcgis.com domains are expired", done => {
198
+ const session = new UserSession({
199
+ clientId: "id",
200
+ token: "token",
201
+ refreshToken: "refresh",
202
+ tokenExpires: YESTERDAY
203
+ });
204
+
205
+ fetchMock.mock(
206
+ "https://www.arcgis.com/sharing/rest/oauth2/token",
207
+ {
208
+ access_token: "new",
209
+ expires_in: 1800,
210
+ username: "c@sey"
211
+ },
212
+ { repeat: 2, method: "POST" }
213
+ );
214
+
215
+ Promise.all([
216
+ session.getToken("https://www.arcgis.com/sharing/rest/portals/self"),
217
+ session.getToken(
218
+ "https://services1.arcgis.com/MOCK_ORG/arcgis/rest/services/Private_Service/FeatureServer"
219
+ )
220
+ ])
221
+ .then(([token1, token2]) => {
222
+ expect(token1).toBe("new");
223
+ expect(token2).toBe("new");
224
+ done();
225
+ })
226
+ .catch(e => {
227
+ fail(e);
228
+ });
229
+ });
230
+
231
+ it("should generate a token for an untrusted, federated server", done => {
232
+ const session = new UserSession({
233
+ clientId: "id",
234
+ token: "token",
235
+ refreshToken: "refresh",
236
+ tokenExpires: TOMORROW,
237
+ portal: "https://gis.city.gov/sharing/rest"
238
+ });
239
+
240
+ fetchMock.postOnce("https://gisservices.city.gov/public/rest/info", {
241
+ currentVersion: 10.51,
242
+ fullVersion: "10.5.1.120",
243
+ owningSystemUrl: "https://gis.city.gov",
244
+ authInfo: {
245
+ isTokenBasedSecurity: true,
246
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken"
247
+ }
248
+ });
249
+
250
+ fetchMock.postOnce("https://gis.city.gov/sharing/rest/info", {
251
+ owningSystemUrl: "http://gis.city.gov",
252
+ authInfo: {
253
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
254
+ isTokenBasedSecurity: true
255
+ }
256
+ });
257
+
258
+ fetchMock.postOnce("https://gis.city.gov/sharing/generateToken", {
259
+ token: "serverToken",
260
+ expires: TOMORROW
261
+ });
262
+
263
+ session
264
+ .getToken(
265
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
266
+ )
267
+ .then(token => {
268
+ expect(token).toBe("serverToken");
269
+ return session.getToken(
270
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
271
+ );
272
+ })
273
+ .then(token => {
274
+ expect(token).toBe("serverToken");
275
+ done();
276
+ })
277
+ .catch(e => {
278
+ fail(e);
279
+ });
280
+ });
281
+
282
+ it("should generate a token for an untrusted, federated server admin call", done => {
283
+ const session = new UserSession({
284
+ clientId: "id",
285
+ token: "token",
286
+ refreshToken: "refresh",
287
+ tokenExpires: TOMORROW,
288
+ portal: "https://gis.city.gov/sharing/rest"
289
+ });
290
+
291
+ fetchMock.postOnce("https://gisservices.city.gov/public/rest/info", {
292
+ currentVersion: 10.51,
293
+ fullVersion: "10.5.1.120",
294
+ owningSystemUrl: "https://gis.city.gov",
295
+ authInfo: {
296
+ isTokenBasedSecurity: true,
297
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken"
298
+ }
299
+ });
300
+
301
+ fetchMock.postOnce("https://gis.city.gov/sharing/rest/info", {
302
+ owningSystemUrl: "http://gis.city.gov",
303
+ authInfo: {
304
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
305
+ isTokenBasedSecurity: true
306
+ }
307
+ });
308
+
309
+ fetchMock.postOnce("https://gis.city.gov/sharing/generateToken", {
310
+ token: "serverToken",
311
+ expires: TOMORROW
312
+ });
313
+
314
+ session
315
+ .getToken(
316
+ "https://gisservices.city.gov/public/rest/admin/services/trees/FeatureServer/addToDefinition"
317
+ )
318
+ .then(token => {
319
+ expect(token).toBe("serverToken");
320
+ return session.getToken(
321
+ "https://gisservices.city.gov/public/rest/admin/services/trees/FeatureServer/addToDefinition"
322
+ );
323
+ })
324
+ .then(token => {
325
+ expect(token).toBe("serverToken");
326
+ done();
327
+ })
328
+ .catch(e => {
329
+ fail(e);
330
+ });
331
+ });
332
+
333
+ it("should generate a token for an untrusted, federated server with user credentials", done => {
334
+ const session = new UserSession({
335
+ username: "c@sey",
336
+ password: "jones",
337
+ portal: "https://gis.city.gov/sharing/rest"
338
+ });
339
+
340
+ fetchMock.postOnce("https://gisservices.city.gov/public/rest/info", {
341
+ currentVersion: 10.51,
342
+ fullVersion: "10.5.1.120",
343
+ owningSystemUrl: "https://gis.city.gov",
344
+ authInfo: {
345
+ isTokenBasedSecurity: true,
346
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken"
347
+ }
348
+ });
349
+
350
+ fetchMock.postOnce("https://gis.city.gov/sharing/rest/info", {
351
+ owningSystemUrl: "http://gis.city.gov",
352
+ authInfo: {
353
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
354
+ isTokenBasedSecurity: true
355
+ }
356
+ });
357
+
358
+ fetchMock.postOnce("https://gis.city.gov/sharing/generateToken", {
359
+ token: "serverToken",
360
+ expires: TOMORROW
361
+ });
362
+
363
+ session
364
+ .getToken(
365
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
366
+ )
367
+ .then(token => {
368
+ expect(token).toBe("serverToken");
369
+ done();
370
+ })
371
+ .catch(e => {
372
+ fail(e);
373
+ });
374
+ });
375
+
376
+ it("should only make 1 token request to an untrusted portal for similar URLs", done => {
377
+ const session = new UserSession({
378
+ clientId: "id",
379
+ token: "token",
380
+ refreshToken: "refresh",
381
+ tokenExpires: TOMORROW,
382
+ portal: "https://gis.city.gov/sharing/rest"
383
+ });
384
+
385
+ fetchMock.mock(
386
+ "https://gisservices.city.gov/public/rest/info",
387
+ {
388
+ currentVersion: 10.51,
389
+ fullVersion: "10.5.1.120",
390
+ owningSystemUrl: "https://gis.city.gov",
391
+ authInfo: {
392
+ isTokenBasedSecurity: true,
393
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken"
394
+ }
395
+ },
396
+ { repeat: 1, method: "POST" }
397
+ );
398
+
399
+ fetchMock.mock(
400
+ "https://gis.city.gov/sharing/rest/info",
401
+ {
402
+ owningSystemUrl: "http://gis.city.gov",
403
+ authInfo: {
404
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken",
405
+ isTokenBasedSecurity: true
406
+ }
407
+ },
408
+ { repeat: 1, method: "POST" }
409
+ );
410
+
411
+ fetchMock.mock(
412
+ "https://gis.city.gov/sharing/generateToken",
413
+ {
414
+ token: "serverToken",
415
+ expires: TOMORROW
416
+ },
417
+ { repeat: 1, method: "POST" }
418
+ );
419
+
420
+ Promise.all([
421
+ session.getToken(
422
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
423
+ ),
424
+ session.getToken(
425
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
426
+ )
427
+ ])
428
+ .then(([token1, token2]) => {
429
+ expect(token1).toBe("serverToken");
430
+ expect(token2).toBe("serverToken");
431
+ expect(
432
+ fetchMock.calls("https://gis.city.gov/sharing/generateToken").length
433
+ ).toBe(1);
434
+ done();
435
+ })
436
+ .catch(e => {
437
+ fail(e);
438
+ });
439
+ });
440
+
441
+ it("should throw an ArcGISAuthError when the owning system doesn't match", done => {
442
+ const session = new UserSession({
443
+ clientId: "id",
444
+ token: "token",
445
+ refreshToken: "refresh",
446
+ tokenExpires: YESTERDAY
447
+ });
448
+
449
+ fetchMock.post("https://gisservices.city.gov/public/rest/info", {
450
+ currentVersion: 10.51,
451
+ fullVersion: "10.5.1.120",
452
+ owningSystemUrl: "https://gis.city.gov",
453
+ authInfo: {
454
+ isTokenBasedSecurity: true,
455
+ tokenServicesUrl: "https://gis.city.gov/sharing/generateToken"
456
+ }
457
+ });
458
+
459
+ session
460
+ .getToken(
461
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
462
+ )
463
+ .catch(e => {
464
+ expect(e.name).toEqual(ErrorTypes.ArcGISAuthError);
465
+ expect(e.code).toEqual("NOT_FEDERATED");
466
+ expect(e.message).toEqual(
467
+ "NOT_FEDERATED: https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query is not federated with https://www.arcgis.com/sharing/rest."
468
+ );
469
+ done();
470
+ });
471
+ });
472
+
473
+ it("should throw a fully hydrated ArcGISAuthError when no owning system is advertised", done => {
474
+ const session = new UserSession({
475
+ clientId: "id",
476
+ token: "token",
477
+ refreshToken: "refresh",
478
+ tokenExpires: YESTERDAY
479
+ });
480
+
481
+ fetchMock.post("https://gisservices.city.gov/public/rest/info", {
482
+ currentVersion: 10.51,
483
+ fullVersion: "10.5.1.120"
484
+ });
485
+
486
+ fetchMock.post(
487
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query",
488
+ {
489
+ error: {
490
+ code: 499,
491
+ message: "Token Required",
492
+ details: []
493
+ }
494
+ }
495
+ );
496
+
497
+ request(
498
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query",
499
+ {
500
+ authentication: session,
501
+ params: {
502
+ foo: "bar"
503
+ }
504
+ }
505
+ ).catch(e => {
506
+ expect(e.name).toEqual(ErrorTypes.ArcGISAuthError);
507
+ expect(e.code).toEqual("NOT_FEDERATED");
508
+ expect(e.message).toEqual(
509
+ "NOT_FEDERATED: https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query is not federated with any portal and is not explicitly trusted."
510
+ );
511
+ expect(e.url).toEqual(
512
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query"
513
+ );
514
+ expect(e.options.params.foo).toEqual("bar");
515
+ done();
516
+ });
517
+ });
518
+
519
+ it("should not throw an ArcGISAuthError when the unfederated service is public", done => {
520
+ const session = new UserSession({
521
+ clientId: "id",
522
+ token: "token",
523
+ refreshToken: "refresh",
524
+ tokenExpires: YESTERDAY
525
+ });
526
+
527
+ fetchMock.post("https://gisservices.city.gov/public/rest/info", {
528
+ currentVersion: 10.51,
529
+ fullVersion: "10.5.1.120"
530
+ });
531
+
532
+ fetchMock.post(
533
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query",
534
+ {
535
+ count: 123
536
+ }
537
+ );
538
+
539
+ request(
540
+ "https://gisservices.city.gov/public/rest/services/trees/FeatureServer/0/query",
541
+ {
542
+ authentication: session,
543
+ params: {
544
+ returnCount: true
545
+ }
546
+ }
547
+ )
548
+ .then(res => {
549
+ expect(res.count).toEqual(123);
550
+ done();
551
+ })
552
+ .catch(e => {
553
+ fail(e);
554
+ });
555
+ });
556
+ });
557
+
558
+ describe(".refreshSession()", () => {
559
+ it("should refresh with a username and password if expired", done => {
560
+ const session = new UserSession({
561
+ username: "c@sey",
562
+ password: "123456"
563
+ });
564
+
565
+ fetchMock.postOnce("https://www.arcgis.com/sharing/rest/generateToken", {
566
+ token: "token",
567
+ expires: TOMORROW.getTime(),
568
+ username: " c@sey"
569
+ });
570
+
571
+ session
572
+ .refreshSession()
573
+ .then(s => {
574
+ expect(s.token).toBe("token");
575
+ expect(s.tokenExpires).toEqual(TOMORROW);
576
+ done();
577
+ })
578
+ .catch(e => {
579
+ fail(e);
580
+ });
581
+ });
582
+
583
+ it("should refresh with an unexpired refresh token", done => {
584
+ const session = new UserSession({
585
+ clientId: "clientId",
586
+ token: "token",
587
+ username: "c@sey",
588
+ refreshToken: "refreshToken",
589
+ refreshTokenExpires: TOMORROW
590
+ });
591
+
592
+ fetchMock.postOnce("https://www.arcgis.com/sharing/rest/oauth2/token", {
593
+ access_token: "newToken",
594
+ expires_in: 60,
595
+ username: " c@sey"
596
+ });
597
+
598
+ session
599
+ .refreshSession()
600
+ .then(s => {
601
+ expect(s.token).toBe("newToken");
602
+ expect(s.tokenExpires.getTime()).toBeGreaterThan(Date.now());
603
+ done();
604
+ })
605
+ .catch(e => {
606
+ fail(e);
607
+ });
608
+ });
609
+
610
+ it("should refresh with an expired refresh token", done => {
611
+ const session = new UserSession({
612
+ clientId: "clientId",
613
+ token: "token",
614
+ username: "c@sey",
615
+ refreshToken: "refreshToken",
616
+ refreshTokenExpires: YESTERDAY,
617
+ redirectUri: "https://example-app.com/redirect-uri"
618
+ });
619
+
620
+ fetchMock.postOnce("https://www.arcgis.com/sharing/rest/oauth2/token", {
621
+ access_token: "newToken",
622
+ expires_in: 60,
623
+ username: " c@sey",
624
+ refresh_token: "newRefreshToken"
625
+ });
626
+
627
+ session
628
+ .refreshSession()
629
+ .then(s => {
630
+ expect(s.token).toBe("newToken");
631
+ expect(s.tokenExpires.getTime()).toBeGreaterThan(Date.now());
632
+ expect(s.refreshToken).toBe("newRefreshToken");
633
+ expect(s.refreshTokenExpires.getTime()).toBeGreaterThan(Date.now());
634
+ done();
635
+ })
636
+ .catch(e => {
637
+ fail(e);
638
+ });
639
+ });
640
+
641
+ it("should reject if we cannot refresh the token", done => {
642
+ const session = new UserSession({
643
+ clientId: "clientId",
644
+ token: "token",
645
+ username: "c@sey"
646
+ });
647
+
648
+ session.refreshSession().catch(e => {
649
+ expect(e instanceof ArcGISAuthError).toBeTruthy();
650
+ expect(e.name).toBe("ArcGISAuthError");
651
+ expect(e.message).toBe("Unable to refresh token.");
652
+ done();
653
+ });
654
+ });
655
+
656
+ it("should only make 1 token request to the portal for similar URLs", done => {
657
+ const session = new UserSession({
658
+ clientId: "id",
659
+ token: "token",
660
+ refreshToken: "refresh",
661
+ tokenExpires: YESTERDAY
662
+ });
663
+
664
+ fetchMock.mock(
665
+ "https://www.arcgis.com/sharing/rest/oauth2/token",
666
+ {
667
+ access_token: "new",
668
+ expires_in: 1800,
669
+ username: "c@sey"
670
+ },
671
+ { repeat: 1, method: "POST" }
672
+ );
673
+
674
+ Promise.all([
675
+ session.getToken("https://www.arcgis.com/sharing/rest/portals/self"),
676
+ session.getToken("https://www.arcgis.com/sharing/rest/portals/self")
677
+ ])
678
+ .then(([token1, token2]) => {
679
+ expect(token1).toBe("new");
680
+ expect(token2).toBe("new");
681
+ expect(
682
+ fetchMock.calls("https://www.arcgis.com/sharing/rest/oauth2/token")
683
+ .length
684
+ ).toBe(1);
685
+ done();
686
+ })
687
+ .catch(e => {
688
+ fail(e);
689
+ });
690
+ });
691
+ });
692
+
693
+ describe(".beginOAuth2()", () => {
694
+ it("should authorize via a popup", done => {
695
+ const MockWindow: any = {
696
+ open: jasmine.createSpy("spy")
697
+ };
698
+
699
+ UserSession.beginOAuth2(
700
+ {
701
+ clientId: "clientId123",
702
+ redirectUri: "http://example-app.com/redirect",
703
+ state: "abc123"
704
+ },
705
+ MockWindow
706
+ )
707
+ .then(session => {
708
+ expect(session.token).toBe("token");
709
+ expect(session.username).toBe("c@sey");
710
+ expect(session.ssl).toBe(true);
711
+ expect(session.tokenExpires).toEqual(TOMORROW);
712
+ done();
713
+ })
714
+ .catch(e => {
715
+ fail(e);
716
+ });
717
+
718
+ expect(MockWindow.open).toHaveBeenCalledWith(
719
+ "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=",
720
+ "oauth-window",
721
+ "height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes"
722
+ );
723
+
724
+ MockWindow.__ESRI_REST_AUTH_HANDLER_clientId123(
725
+ JSON.stringify(undefined),
726
+ JSON.stringify({
727
+ token: "token",
728
+ expires: TOMORROW,
729
+ username: "c@sey",
730
+ ssl: true
731
+ })
732
+ );
733
+ });
734
+
735
+ it("should reject the promise if there is an error", done => {
736
+ const MockWindow: any = {
737
+ open: jasmine.createSpy("spy")
738
+ };
739
+
740
+ UserSession.beginOAuth2(
741
+ {
742
+ clientId: "clientId123",
743
+ redirectUri: "http://example-app.com/redirect",
744
+ locale: "fr"
745
+ },
746
+ MockWindow
747
+ ).catch(e => {
748
+ done();
749
+ });
750
+
751
+ expect(MockWindow.open).toHaveBeenCalledWith(
752
+ "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",
753
+ "oauth-window",
754
+ "height=400,width=600,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes"
755
+ );
756
+
757
+ MockWindow.__ESRI_REST_AUTH_HANDLER_clientId123(
758
+ JSON.stringify({
759
+ errorMessage: "unable to sign in",
760
+ error: "SIGN_IN_FAILED"
761
+ })
762
+ );
763
+ });
764
+
765
+ it("should authorize in the same window/tab", () => {
766
+ const MockWindow: any = {
767
+ location: {
768
+ href: ""
769
+ }
770
+ };
771
+
772
+ // https://github.com/palantir/tslint/issues/3056
773
+ void UserSession.beginOAuth2(
774
+ {
775
+ clientId: "clientId123",
776
+ redirectUri: "http://example-app.com/redirect",
777
+ popup: false
778
+ },
779
+ MockWindow
780
+ );
781
+
782
+ expect(MockWindow.location.href).toBe(
783
+ "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="
784
+ );
785
+ });
786
+
787
+ it("should authorize using a social media provider", () => {
788
+ const MockWindow: any = {
789
+ location: {
790
+ href: ""
791
+ }
792
+ };
793
+
794
+ // https://github.com/palantir/tslint/issues/3056
795
+ void UserSession.beginOAuth2(
796
+ {
797
+ clientId: "clientId123",
798
+ redirectUri: "http://example-app.com/redirect",
799
+ popup: false,
800
+ provider: "facebook"
801
+ },
802
+ MockWindow
803
+ );
804
+
805
+ expect(MockWindow.location.href).toBe(
806
+ "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="
807
+ );
808
+ });
809
+
810
+ it("should authorize using the other social media provider", () => {
811
+ const MockWindow: any = {
812
+ location: {
813
+ href: ""
814
+ }
815
+ };
816
+
817
+ // https://github.com/palantir/tslint/issues/3056
818
+ void UserSession.beginOAuth2(
819
+ {
820
+ clientId: "clientId123",
821
+ redirectUri: "http://example-app.com/redirect",
822
+ popup: false,
823
+ provider: "google"
824
+ },
825
+ MockWindow
826
+ );
827
+
828
+ expect(MockWindow.location.href).toBe(
829
+ "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="
830
+ );
831
+ });
832
+ });
833
+
834
+ describe(".completeOAuth2()", () => {
835
+ it("should return a new user session if it cannot find a valid parent", () => {
836
+ const MockWindow = {
837
+ location: {
838
+ href:
839
+ "https://example-app.com/redirect-uri#access_token=token&expires_in=1209600&username=c%40sey&ssl=true&persist=true"
840
+ },
841
+ get parent() {
842
+ return this;
843
+ }
844
+ };
845
+
846
+ const session = UserSession.completeOAuth2(
847
+ {
848
+ clientId: "clientId",
849
+ redirectUri: "https://example-app.com/redirect-uri"
850
+ },
851
+ MockWindow
852
+ );
853
+
854
+ expect(session.token).toBe("token");
855
+ expect(session.tokenExpires.getTime()).toBeGreaterThan(Date.now());
856
+ expect(session.username).toBe("c@sey");
857
+ expect(session.ssl).toBe(true);
858
+ });
859
+
860
+ it("should return a new user session with ssl as false when callback hash does not have ssl parameter", () => {
861
+ const MockWindow = {
862
+ location: {
863
+ href:
864
+ "https://example-app.com/redirect-uri#access_token=token&expires_in=1209600&username=c%40sey&persist=true"
865
+ },
866
+ get parent() {
867
+ return this;
868
+ }
869
+ };
870
+
871
+ const session = UserSession.completeOAuth2(
872
+ {
873
+ clientId: "clientId",
874
+ redirectUri: "https://example-app.com/redirect-uri"
875
+ },
876
+ MockWindow
877
+ );
878
+ expect(session.ssl).toBe(false);
879
+ });
880
+
881
+ it("should callback to create a new user session if finds a valid opener", done => {
882
+ const MockWindow = {
883
+ opener: {
884
+ parent: {
885
+ __ESRI_REST_AUTH_HANDLER_clientId(
886
+ errorString: string,
887
+ oauthInfoString: string
888
+ ) {
889
+ const oauthInfo = JSON.parse(oauthInfoString);
890
+ expect(oauthInfo.token).toBe("token");
891
+ expect(oauthInfo.username).toBe("c@sey");
892
+ expect(oauthInfo.ssl).toBe(true);
893
+ expect(new Date(oauthInfo.expires).getTime()).toBeGreaterThan(
894
+ Date.now()
895
+ );
896
+ }
897
+ }
898
+ },
899
+ close() {
900
+ done();
901
+ },
902
+ location: {
903
+ href:
904
+ "https://example-app.com/redirect-uri#access_token=token&expires_in=1209600&username=c%40sey&ssl=true"
905
+ }
906
+ };
907
+
908
+ UserSession.completeOAuth2(
909
+ {
910
+ clientId: "clientId",
911
+ redirectUri: "https://example-app.com/redirect-uri"
912
+ },
913
+ MockWindow
914
+ );
915
+ });
916
+
917
+ it("should callback to create a new user session if finds a valid parent", done => {
918
+ const MockWindow = {
919
+ parent: {
920
+ __ESRI_REST_AUTH_HANDLER_clientId(
921
+ errorString: string,
922
+ oauthInfoString: string
923
+ ) {
924
+ const oauthInfo = JSON.parse(oauthInfoString);
925
+ expect(oauthInfo.token).toBe("token");
926
+ expect(oauthInfo.username).toBe("c@sey");
927
+ expect(oauthInfo.ssl).toBe(true);
928
+ expect(new Date(oauthInfo.expires).getTime()).toBeGreaterThan(
929
+ Date.now()
930
+ );
931
+ }
932
+ },
933
+ close() {
934
+ done();
935
+ },
936
+ location: {
937
+ href:
938
+ "https://example-app.com/redirect-uri#access_token=token&expires_in=1209600&username=c%40sey&ssl=true"
939
+ }
940
+ };
941
+
942
+ UserSession.completeOAuth2(
943
+ {
944
+ clientId: "clientId",
945
+ redirectUri: "https://example-app.com/redirect-uri"
946
+ },
947
+ MockWindow
948
+ );
949
+ });
950
+
951
+ it("should throw an error from the authorization window", () => {
952
+ const MockWindow = {
953
+ location: {
954
+ href:
955
+ "https://example-app.com/redirect-uri#error=Invalid_Signin&error_description=Invalid_Signin"
956
+ },
957
+ get parent() {
958
+ return this;
959
+ }
960
+ };
961
+
962
+ expect(function() {
963
+ UserSession.completeOAuth2(
964
+ {
965
+ clientId: "clientId",
966
+ redirectUri: "https://example-app.com/redirect-uri"
967
+ },
968
+ MockWindow
969
+ );
970
+ }).toThrowError(ArcGISRequestError, "Invalid_Signin: Invalid_Signin");
971
+ });
972
+ });
973
+
974
+ describe(".authorize()", () => {
975
+ it("should redirect the request to the authorization page", done => {
976
+ const spy = jasmine.createSpy("spy");
977
+ const MockResponse: any = {
978
+ writeHead: spy,
979
+ end() {
980
+ expect(spy.calls.mostRecent().args[0]).toBe(301);
981
+ expect(spy.calls.mostRecent().args[1].Location).toBe(
982
+ "https://arcgis.com/sharing/rest/oauth2/authorize?client_id=clientId&duration=20160&response_type=code&redirect_uri=https%3A%2F%2Fexample-app.com%2Fredirect-uri"
983
+ );
984
+ done();
985
+ }
986
+ };
987
+
988
+ UserSession.authorize(
989
+ {
990
+ clientId: "clientId",
991
+ redirectUri: "https://example-app.com/redirect-uri"
992
+ },
993
+ MockResponse
994
+ );
995
+ });
996
+ });
997
+
998
+ describe(".exchangeAuthorizationCode()", () => {
999
+ let paramsSpy: jasmine.Spy;
1000
+
1001
+ beforeEach(() => {
1002
+ paramsSpy = spyOn(FormData.prototype, "append").and.callThrough();
1003
+ });
1004
+
1005
+ afterAll(() => {
1006
+ paramsSpy.calls.reset();
1007
+ });
1008
+
1009
+ it("should exchange an authorization code for a new UserSession", done => {
1010
+ fetchMock.postOnce("https://www.arcgis.com/sharing/rest/oauth2/token", {
1011
+ access_token: "token",
1012
+ expires_in: 1800,
1013
+ refresh_token: "refreshToken",
1014
+ username: "Casey",
1015
+ ssl: true
1016
+ });
1017
+
1018
+ UserSession.exchangeAuthorizationCode(
1019
+ {
1020
+ clientId: "clientId",
1021
+ redirectUri: "https://example-app.com/redirect-uri"
1022
+ },
1023
+ "code"
1024
+ )
1025
+ .then(session => {
1026
+ expect(session.token).toBe("token");
1027
+ expect(session.tokenExpires.getTime()).toBeGreaterThan(Date.now());
1028
+ expect(session.username).toBe("Casey");
1029
+ expect(session.refreshToken).toBe("refreshToken");
1030
+ expect(session.ssl).toBe(true);
1031
+ done();
1032
+ })
1033
+ .catch(e => {
1034
+ fail(e);
1035
+ });
1036
+ });
1037
+ });
1038
+
1039
+ describe(".getUser()", () => {
1040
+ afterEach(fetchMock.restore);
1041
+
1042
+ it("should cache metadata about the user", done => {
1043
+ // we intentionally only mock one response
1044
+ fetchMock.once(
1045
+ "https://www.arcgis.com/sharing/rest/community/users/jsmith?f=json&token=token",
1046
+ {
1047
+ username: "jsmith",
1048
+ fullName: "John Smith",
1049
+ role: "org_publisher"
1050
+ }
1051
+ );
1052
+
1053
+ const session = new UserSession({
1054
+ clientId: "clientId",
1055
+ redirectUri: "https://example-app.com/redirect-uri",
1056
+ token: "token",
1057
+ tokenExpires: TOMORROW,
1058
+ refreshToken: "refreshToken",
1059
+ refreshTokenExpires: TOMORROW,
1060
+ refreshTokenTTL: 1440,
1061
+ username: "jsmith",
1062
+ password: "123456"
1063
+ });
1064
+
1065
+ session
1066
+ .getUser()
1067
+ .then(response => {
1068
+ expect(response.role).toEqual("org_publisher");
1069
+ session
1070
+ .getUser()
1071
+ .then(cachedResponse => {
1072
+ expect(cachedResponse.fullName).toEqual("John Smith");
1073
+ done();
1074
+ })
1075
+ .catch(e => {
1076
+ fail(e);
1077
+ });
1078
+ })
1079
+ .catch(e => {
1080
+ fail(e);
1081
+ });
1082
+ });
1083
+ });
1084
+
1085
+ describe("to/fromCredential()", () => {
1086
+ const MOCK_CREDENTIAL: ICredential = {
1087
+ expires: TOMORROW.getTime(),
1088
+ server: "https://www.arcgis.com",
1089
+ ssl: false,
1090
+ token: "token",
1091
+ userId: "jsmith"
1092
+ };
1093
+
1094
+ const MOCK_USER_SESSION = new UserSession({
1095
+ clientId: "clientId",
1096
+ redirectUri: "https://example-app.com/redirect-uri",
1097
+ token: "token",
1098
+ ssl: false,
1099
+ tokenExpires: TOMORROW,
1100
+ refreshToken: "refreshToken",
1101
+ refreshTokenExpires: TOMORROW,
1102
+ refreshTokenTTL: 1440,
1103
+ username: "jsmith",
1104
+ password: "123456"
1105
+ });
1106
+
1107
+ it("should create a credential object from a session", () => {
1108
+ const creds = MOCK_USER_SESSION.toCredential();
1109
+ expect(creds.userId).toEqual("jsmith");
1110
+ expect(creds.server).toEqual("https://www.arcgis.com/sharing/rest");
1111
+ expect(creds.ssl).toEqual(false);
1112
+ expect(creds.token).toEqual("token");
1113
+ expect(creds.expires).toEqual(TOMORROW.getTime());
1114
+ });
1115
+
1116
+ it("should create a UserSession from a credential", () => {
1117
+ const session = UserSession.fromCredential(MOCK_CREDENTIAL);
1118
+ expect(session.username).toEqual("jsmith");
1119
+ expect(session.portal).toEqual("https://www.arcgis.com/sharing/rest");
1120
+ expect(session.ssl).toEqual(false);
1121
+ expect(session.token).toEqual("token");
1122
+ expect(session.tokenExpires).toEqual(new Date(TOMORROW));
1123
+ });
1124
+
1125
+ it("should create a UserSession from a credential that came from a UserSession", () => {
1126
+ const creds = MOCK_USER_SESSION.toCredential();
1127
+ const credSession = UserSession.fromCredential(creds);
1128
+ expect(credSession.username).toEqual("jsmith");
1129
+ expect(credSession.portal).toEqual("https://www.arcgis.com/sharing/rest");
1130
+ expect(credSession.ssl).toEqual(false);
1131
+ expect(credSession.token).toEqual("token");
1132
+ expect(credSession.tokenExpires).toEqual(new Date(TOMORROW));
1133
+ });
1134
+ });
1135
+
1136
+ describe("getServerRootUrl()", () => {
1137
+ it("should lowercase domain names", () => {
1138
+ const session = new UserSession({
1139
+ clientId: "id",
1140
+ token: "token",
1141
+ tokenExpires: TOMORROW
1142
+ });
1143
+
1144
+ const root = session.getServerRootUrl(
1145
+ "https://PNP00035.esri.com/server/rest/services/Hosted/perimeters_dd83/FeatureServer"
1146
+ );
1147
+ expect(root).toEqual("https://pnp00035.esri.com/server");
1148
+ });
1149
+
1150
+ it("should not lowercase path names", () => {
1151
+ const session = new UserSession({
1152
+ clientId: "id",
1153
+ token: "token",
1154
+ tokenExpires: TOMORROW
1155
+ });
1156
+
1157
+ const root = session.getServerRootUrl(
1158
+ "https://pnp00035.esri.com/tiles/LkFyxb9zDq7vAOAm/arcgis/rest/services/NB_Stereographic/VectorTileServer"
1159
+ );
1160
+ expect(root).toEqual(
1161
+ "https://pnp00035.esri.com/tiles/LkFyxb9zDq7vAOAm/arcgis"
1162
+ );
1163
+ });
1164
+
1165
+ it("should respect the original https/http protocol", () => {
1166
+ const session = new UserSession({
1167
+ clientId: "id",
1168
+ token: "token",
1169
+ tokenExpires: TOMORROW
1170
+ });
1171
+
1172
+ const root = session.getServerRootUrl(
1173
+ "http://pnp00035.esri.com/tiles/LkFyxb9zDq7vAOAm/arcgis/rest/services/NB_Stereographic/VectorTileServer"
1174
+ );
1175
+ expect(root).toEqual(
1176
+ "http://pnp00035.esri.com/tiles/LkFyxb9zDq7vAOAm/arcgis"
1177
+ );
1178
+ });
1179
+ });
1180
+
1181
+ describe("non-federated server", () => {
1182
+ it("shouldnt fetch a fresh token if the current one isnt expired.", done => {
1183
+ const MOCK_USER_SESSION = new UserSession({
1184
+ username: "c@sey",
1185
+ password: "123456",
1186
+ token: "token",
1187
+ tokenExpires: TOMORROW,
1188
+ server: "https://fakeserver.com/arcgis"
1189
+ });
1190
+
1191
+ MOCK_USER_SESSION.getToken(
1192
+ "https://fakeserver.com/arcgis/rest/services/Fake/MapServer/"
1193
+ )
1194
+ .then(token => {
1195
+ expect(token).toBe("token");
1196
+ done();
1197
+ })
1198
+ .catch(err => {
1199
+ fail(err);
1200
+ });
1201
+ });
1202
+
1203
+ it("should fetch a fresh token if the current one is expired.", done => {
1204
+ const MOCK_USER_SESSION = new UserSession({
1205
+ username: "jsmith",
1206
+ password: "123456",
1207
+ token: "token",
1208
+ tokenExpires: YESTERDAY,
1209
+ server: "https://fakeserver.com/arcgis"
1210
+ });
1211
+
1212
+ fetchMock.postOnce("https://fakeserver.com/arcgis/rest/info", {
1213
+ currentVersion: 10.61,
1214
+ fullVersion: "10.6.1",
1215
+ authInfo: {
1216
+ isTokenBasedSecurity: true,
1217
+ tokenServicesUrl: "https://fakeserver.com/arcgis/tokens/"
1218
+ }
1219
+ });
1220
+
1221
+ fetchMock.postOnce("https://fakeserver.com/arcgis/tokens/", {
1222
+ token: "fresh-token",
1223
+ expires: TOMORROW.getTime(),
1224
+ username: " jsmith"
1225
+ });
1226
+
1227
+ MOCK_USER_SESSION.getToken(
1228
+ "https://fakeserver.com/arcgis/rest/services/Fake/MapServer/"
1229
+ )
1230
+ .then(token => {
1231
+ expect(token).toBe("fresh-token");
1232
+ const [url, options]: [string, RequestInit] = fetchMock.lastCall(
1233
+ "https://fakeserver.com/arcgis/tokens/"
1234
+ );
1235
+ expect(options.method).toBe("POST");
1236
+ expect(options.body).toContain("f=json");
1237
+ expect(options.body).toContain("username=jsmith");
1238
+ expect(options.body).toContain("password=123456");
1239
+ expect(options.body).toContain("client=referer");
1240
+ done();
1241
+ })
1242
+ .catch(err => {
1243
+ fail(err);
1244
+ });
1245
+ });
1246
+
1247
+ it("should trim down the server url if necessary.", done => {
1248
+ const MOCK_USER_SESSION = new UserSession({
1249
+ username: "jsmith",
1250
+ password: "123456",
1251
+ token: "token",
1252
+ tokenExpires: YESTERDAY,
1253
+ server: "https://fakeserver.com/arcgis/rest/services/blah/"
1254
+ });
1255
+
1256
+ fetchMock.postOnce("https://fakeserver.com/arcgis/rest/info", {
1257
+ currentVersion: 10.61,
1258
+ fullVersion: "10.6.1",
1259
+ authInfo: {
1260
+ isTokenBasedSecurity: true,
1261
+ tokenServicesUrl: "https://fakeserver.com/arcgis/tokens/"
1262
+ }
1263
+ });
1264
+
1265
+ fetchMock.postOnce("https://fakeserver.com/arcgis/tokens/", {
1266
+ token: "fresh-token",
1267
+ expires: TOMORROW.getTime(),
1268
+ username: " jsmith"
1269
+ });
1270
+
1271
+ MOCK_USER_SESSION.getToken(
1272
+ "https://fakeserver.com/arcgis/rest/services/Fake/MapServer/"
1273
+ )
1274
+ .then(token => {
1275
+ expect(token).toBe("fresh-token");
1276
+ done();
1277
+ })
1278
+ .catch(err => {
1279
+ fail(err);
1280
+ });
1281
+ });
1282
+
1283
+ it("should throw an error if the server isnt trusted.", done => {
1284
+ fetchMock.postOnce("https://fakeserver2.com/arcgis/rest/info", {
1285
+ currentVersion: 10.61,
1286
+ fullVersion: "10.6.1",
1287
+ authInfo: {
1288
+ isTokenBasedSecurity: true,
1289
+ tokenServicesUrl: "https://fakeserver2.com/arcgis/tokens/"
1290
+ }
1291
+ });
1292
+ const MOCK_USER_SESSION = new UserSession({
1293
+ username: "c@sey",
1294
+ password: "123456",
1295
+ token: "token",
1296
+ tokenExpires: TOMORROW,
1297
+ server: "https://fakeserver.com/arcgis"
1298
+ });
1299
+
1300
+ MOCK_USER_SESSION.getToken(
1301
+ "https://fakeserver2.com/arcgis/rest/services/Fake/MapServer/"
1302
+ )
1303
+ .then(token => {
1304
+ fail(token);
1305
+ })
1306
+ .catch(err => {
1307
+ expect(err.code).toBe("NOT_FEDERATED");
1308
+ expect(err.originalMessage).toBe(
1309
+ "https://fakeserver2.com/arcgis/rest/services/Fake/MapServer/ is not federated with any portal and is not explicitly trusted."
1310
+ );
1311
+ done();
1312
+ });
1313
+ });
1314
+ });
1315
+ });