@tellescope/sdk 0.0.28 → 0.0.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,12 @@
1
1
  require('source-map-support').install();
2
- import * as fs from "fs"
2
+ import crypto from "crypto"
3
3
  import * as buffer from "buffer" // only node >=15.7.0
4
4
 
5
5
  import {
6
6
  Enduser,
7
7
  ClientModelForName,
8
8
  ClientModelForName_required,
9
+ UserDisplayInfo,
9
10
  } from "@tellescope/types-client"
10
11
  import {
11
12
  Model as ClientModel,
@@ -52,6 +53,7 @@ const [email2, password2] = [process.env.TEST_EMAIL_2, process.env.TEST_PASSWORD
52
53
  const [nonAdminEmail, nonAdminPassword] = [process.env.NON_ADMIN_EMAIL, process.env.NON_ADMIN_PASSWORD]
53
54
 
54
55
  const userId = '60398b0231a295e64f084fd9'
56
+ const businessId = '60398b1131a295e64f084ff6'
55
57
 
56
58
  // const example_SDK_usage = async () => {
57
59
  // // initialize SDK and authenticate a user
@@ -75,7 +77,8 @@ const userId = '60398b0231a295e64f084fd9'
75
77
  const sdk = new Session({ host })
76
78
  const sdkOther = new Session({ host, apiKey: "ba745e25162bb95a795c5fa1af70df188d93c4d3aac9c48b34a5c8c9dd7b80f7" })
77
79
  const sdkNonAdmin = new Session({ host })
78
- const enduserSDK = new EnduserSession({ host })
80
+ const enduserSDK = new EnduserSession({ host, businessId })
81
+ const enduserSDKDifferentBusinessId = new EnduserSession({ host, businessId: '80398b1131a295e64f084ff6' })
79
82
  // const sdkOtherEmail = "sebass@tellescope.com"
80
83
 
81
84
  if (!(email && password && email2 && password2 && nonAdminEmail && nonAdminPassword)) {
@@ -103,6 +106,16 @@ const setup_tests = async () => {
103
106
  await sdk.authenticate(email, password)
104
107
  await async_test('test_authenticated (re-authenticated)', sdk.test_authenticated, { expectedResult: 'Authenticated!' })
105
108
 
109
+ const uInfo = sdk.userInfo
110
+ const originalAuthToken = sdk.authToken
111
+ await sdk.refresh_session()
112
+ assert(uInfo.id === sdk.userInfo.id, 'userInfo mismatch', 'userInfo id preserved after refresh')
113
+ assert(
114
+ !!originalAuthToken && !!sdk.authToken && sdk.authToken !== originalAuthToken,
115
+ 'same authToken after refresh',
116
+ 'authToken refresh'
117
+ )
118
+
106
119
  await async_test('reset_db', () => sdk.reset_db(), passOnVoid)
107
120
  }
108
121
 
@@ -837,13 +850,61 @@ const chat_room_tests = async () => {
837
850
  const sdk2 = new Session({ host })
838
851
  await sdk2.authenticate(email2, password2)
839
852
 
840
- const room = await sdk.api.chat_rooms.createOne({ type: 'internal', userIds: [userId] })
853
+ const email='enduser@tellescope.com', password='enduserPassword!';
854
+ const enduser = await sdk.api.endusers.createOne({ email })
855
+ await sdk.api.endusers.set_password({ id: enduser.id, password }).catch(console.error)
856
+ await enduserSDK.authenticate(email, password).catch(console.error)
857
+
858
+ const room = await sdk.api.chat_rooms.createOne({ type: 'internal', userIds: [userId], enduserIds: [enduserSDK.userInfo.id] })
841
859
  await async_test(
842
860
  `get-chat-room (not a user)`,
843
861
  () => sdk2.api.chat_rooms.getOne(room.id),
844
862
  { shouldError: true, onError: e => e.message === "Could not find a record for the given id" }
845
863
  )
846
- await sdk.api.chat_rooms.deleteOne(room.id)
864
+ await async_test(
865
+ `user_display_info for room (not a user)`,
866
+ () => sdk2.api.chat_rooms.display_info({ id: room.id }),
867
+ { shouldError: true, onError: e => e.message === "Could not find a record for the given id" }
868
+ )
869
+
870
+ const verifyRoomDisplayInfo = (info: Indexable<UserDisplayInfo>) => {
871
+ if (!info) return false
872
+ if (typeof info !== 'object') return false
873
+ if (Object.keys(info).length !== 2) return false
874
+ if (!info[sdk.userInfo.id]) return false
875
+ if (!info[enduserSDK.userInfo.id]) return false
876
+ const [user, enduser] = [info[sdk.userInfo.id], info[enduserSDK.userInfo.id]]
877
+ if (!(
878
+ user.id === sdk.userInfo.id &&
879
+ user.fname === sdk.userInfo.fname &&
880
+ user.lname === sdk.userInfo.lname &&
881
+ user.avatar === sdk.userInfo.avatar &&
882
+ !!user.createdAt &&
883
+ !!user.lastActive &&
884
+ !!user.lastLogout
885
+ )) return false
886
+ if (!(
887
+ enduser.id === enduserSDK.userInfo.id &&
888
+ enduser.fname === enduserSDK.userInfo.fname &&
889
+ enduser.lname === enduserSDK.userInfo.lname &&
890
+ enduser.avatar === enduserSDK.userInfo.avatar &&
891
+ !!enduser.createdAt &&
892
+ !!enduser.lastActive &&
893
+ !!enduser.lastLogout
894
+ )) return false
895
+ return true
896
+ }
897
+ await async_test(
898
+ `user_display_info for room (for user)`,
899
+ () => sdk.api.chat_rooms.display_info({ id: room.id }),
900
+ { onResult: r => r.id === room.id && verifyRoomDisplayInfo(r.display_info) }
901
+ )
902
+ await async_test(
903
+ `user_display_info for room (for enduser)`,
904
+ () => enduserSDK.api.chat_rooms.display_info({ id: room.id }),
905
+ { onResult: r => r.id === room.id && verifyRoomDisplayInfo(r.display_info) }
906
+ )
907
+ await sdk.api.chat_rooms.deleteOne(room.id)
847
908
 
848
909
 
849
910
  const emptyRoom = await sdk.api.chat_rooms.createOne({ })
@@ -868,6 +929,8 @@ const chat_room_tests = async () => {
868
929
  { onResult: r => r.id === emptyRoom.id }
869
930
  )
870
931
  await sdk.api.chat_rooms.deleteOne(emptyRoom.id)
932
+
933
+ await sdk.api.endusers.deleteOne(enduser.id)
871
934
  }
872
935
 
873
936
  const chat_tests = async() => {
@@ -978,11 +1041,30 @@ const enduserAccessTests = async () => {
978
1041
  await sdk.api.endusers.set_password({ id: enduser.id, password }).catch(console.error)
979
1042
  await enduserSDK.authenticate(email, password).catch(console.error)
980
1043
 
1044
+ await wait(undefined, 1000) // wait so that refresh_session generates a new authToken (different timestamp)
1045
+
1046
+ const uInfo = enduserSDK.userInfo
1047
+ const originalAuthToken = enduserSDK.authToken
1048
+ await enduserSDK.refresh_session()
1049
+ assert(uInfo.id === enduserSDK.userInfo.id, 'userInfo mismatch', 'userInfo id preserved after refresh')
1050
+ assert(
1051
+ !!originalAuthToken && !!enduserSDK.authToken && enduserSDK.authToken !== originalAuthToken,
1052
+ 'same authToken after refresh',
1053
+ 'authToken refresh'
1054
+ )
1055
+
1056
+ await async_test(
1057
+ `no-enduser-access for different businessId`,
1058
+ () => enduserSDKDifferentBusinessId.authenticate(email, password),
1059
+ { shouldError: true, onError: (e: any) => e?.message === "Could not find a corresponding account" }
1060
+ )
1061
+
981
1062
  for (const n in schema) {
982
1063
  const endpoint = url_safe_path(n)
983
1064
  const model = schema[n as keyof typeof schema]
984
1065
  if (n === 'webhooks') continue // no default endpoints implemented
985
1066
 
1067
+ //@ts-ignore
986
1068
  if (!model?.enduserActions?.read && (model.defaultActions.read || model.customActions.read)) {
987
1069
  await async_test(
988
1070
  `no-enduser-access getOne (${endpoint})`,
@@ -990,6 +1072,7 @@ const enduserAccessTests = async () => {
990
1072
  { shouldError: true, onError: (e: any) => e === 'Unauthenticated' || e?.message === 'This action is not allowed' }
991
1073
  )
992
1074
  }
1075
+ //@ts-ignore
993
1076
  if (!model.enduserActions?.readMany && (model.defaultActions.readMany || model.customActions.readMany)) {
994
1077
  await async_test(
995
1078
  `no-enduser-access getSome (${endpoint})`,
@@ -997,6 +1080,7 @@ const enduserAccessTests = async () => {
997
1080
  { shouldError: true, onError: (e: any) => e === 'Unauthenticated' || e?.message === 'This action is not allowed' }
998
1081
  )
999
1082
  }
1083
+ //@ts-ignore
1000
1084
  if (!model.enduserActions?.create && (model.defaultActions.create || model.customActions.create)) {
1001
1085
  await async_test(
1002
1086
  `no-enduser-access createOne (${endpoint})`,
@@ -1004,6 +1088,7 @@ const enduserAccessTests = async () => {
1004
1088
  { shouldError: true, onError: (e: any) => e === 'Unauthenticated' || e?.message === 'This action is not allowed' }
1005
1089
  )
1006
1090
  }
1091
+ //@ts-ignore
1007
1092
  if (!model.enduserActions?.createMany && (model.defaultActions.createMany || model.customActions.createMany)) {
1008
1093
  await async_test(
1009
1094
  `no-enduser-access createMany (${endpoint})`,
@@ -1011,6 +1096,7 @@ const enduserAccessTests = async () => {
1011
1096
  { shouldError: true, onError: (e: any) => e === 'Unauthenticated' || e?.message === 'This action is not allowed' }
1012
1097
  )
1013
1098
  }
1099
+ //@ts-ignore
1014
1100
  if (!model.enduserActions?.update && (model.defaultActions.update || model.customActions.update)) {
1015
1101
  await async_test(
1016
1102
  `no-enduser-access update (${endpoint})`,
@@ -1018,6 +1104,7 @@ const enduserAccessTests = async () => {
1018
1104
  { shouldError: true, onError: (e: any) => e === 'Unauthenticated' || e?.message === 'This action is not allowed' }
1019
1105
  )
1020
1106
  }
1107
+ //@ts-ignore
1021
1108
  if (!model.enduserActions?.delete && (model.defaultActions.delete || model.customActions.delete)) {
1022
1109
  await async_test(
1023
1110
  `no-enduser-access delete (${endpoint})`,
@@ -1079,6 +1166,9 @@ const files_tests = async () => {
1079
1166
  const downloaded: string = await sdk.DOWNLOAD(downloadURL)
1080
1167
 
1081
1168
  assert(downloaded === buff.toString(), 'downloaded file does not match uploaded file', 'upload, download comparison')
1169
+
1170
+ const { downloadURL: cachedURL } = await sdk.api.files.file_download_URL({ secureName: file.secureName })
1171
+ assert(downloadURL === cachedURL, 'cache download url failed', 'download url cache')
1082
1172
  }
1083
1173
 
1084
1174
  const enduser_session_tests = async () => {
@@ -1131,6 +1221,19 @@ const users_tests = async () => {
1131
1221
  { onResult: u => u.id === sdkNonAdmin.userInfo.id && u.fname === "Non" }
1132
1222
  )
1133
1223
 
1224
+ const randomFieldValue = crypto.randomBytes(32).toString('hex')
1225
+ await async_test(
1226
+ `update user (custom fields)`,
1227
+ () => sdk.api.users.updateOne(sdk.userInfo.id, { fields: { f1: randomFieldValue, f2: randomFieldValue } }), // change back
1228
+ { onResult: u => u.id === sdk.userInfo.id && u.fields?.f1 === randomFieldValue && u.fields?.f2 === randomFieldValue }
1229
+ )
1230
+ // sdkNonAdmin.userInfo.fname = 'Non' // update back in sdk instance as well
1231
+
1232
+ await async_test(
1233
+ `verify user update (custom fields)`,
1234
+ () => sdk.api.users.getOne(sdk.userInfo.id),
1235
+ { onResult: u => u.id === sdk.userInfo.id && u.fields?.f1 === randomFieldValue && u.fields?.f2 === randomFieldValue }
1236
+ )
1134
1237
  }
1135
1238
 
1136
1239
  const tests: { [K in keyof ClientModelForName]: () => void } = {
@@ -1178,7 +1281,7 @@ const tests: { [K in keyof ClientModelForName]: () => void } = {
1178
1281
 
1179
1282
  let n: keyof typeof schema;
1180
1283
  for (n in schema) {
1181
- const returnValidation = schema[n].customActions?.create?.returns
1284
+ const returnValidation = (schema[n].customActions as any)?.create?.returns
1182
1285
 
1183
1286
  await run_generated_tests({
1184
1287
  queries: sdk.api[n] as any,