@tellescope/sdk 0.0.23 → 0.0.27

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.
@@ -49,6 +49,7 @@ import {
49
49
  const host = process.env.TEST_URL || 'http://localhost:8080'
50
50
  const [email, password] = [process.env.TEST_EMAIL, process.env.TEST_PASSWORD]
51
51
  const [email2, password2] = [process.env.TEST_EMAIL_2, process.env.TEST_PASSWORD_2]
52
+ const [nonAdminEmail, nonAdminPassword] = [process.env.NON_ADMIN_EMAIL, process.env.NON_ADMIN_PASSWORD]
52
53
 
53
54
  const userId = '60398b0231a295e64f084fd9'
54
55
 
@@ -73,10 +74,11 @@ const userId = '60398b0231a295e64f084fd9'
73
74
 
74
75
  const sdk = new Session({ host })
75
76
  const sdkOther = new Session({ host, apiKey: "ba745e25162bb95a795c5fa1af70df188d93c4d3aac9c48b34a5c8c9dd7b80f7" })
77
+ const sdkNonAdmin = new Session({ host })
76
78
  const enduserSDK = new EnduserSession({ host })
77
79
  // const sdkOtherEmail = "sebass@tellescope.com"
78
80
 
79
- if (!(email && password && email2 && password2)) {
81
+ if (!(email && password && email2 && password2 && nonAdminEmail && nonAdminPassword)) {
80
82
  console.error("Set TEST_EMAIL and TEST_PASSWORD")
81
83
  process.exit()
82
84
  }
@@ -293,8 +295,13 @@ const generateEnduserAuthTests = async () => {
293
295
 
294
296
  const { authToken, enduser } = await sdk.api.endusers.generate_auth_token({ id: e.id })
295
297
  assert(!!authToken && !!enduser, 'invalid returned values', 'Generate authToken and get enduser')
296
- const { isAuthenticated } = await sdk.api.endusers.is_authenticated({ id: enduser.id, authToken })
298
+ let { isAuthenticated } = await sdk.api.endusers.is_authenticated({ id: enduser.id, authToken })
297
299
  assert(isAuthenticated, 'invalid authToken generated for enduser', 'Generate authToken for enduser is valid')
300
+ assert(
301
+ (await sdk.api.endusers.is_authenticated({ authToken })).isAuthenticated,
302
+ 'id omitted results in failed authentication',
303
+ 'id optional for is_authenticated'
304
+ )
298
305
 
299
306
  const { authToken: authTokenUID, enduser: enduser2 } = await sdk.api.endusers.generate_auth_token({ externalId })
300
307
  assert(!!authTokenUID && !!enduser2, 'invalid returned values eid', 'Generate authToken and get enduser eid')
@@ -967,6 +974,7 @@ const enduserAccessTests = async () => {
967
974
  const password = 'testpassword'
968
975
 
969
976
  const enduser = await sdk.api.endusers.createOne({ email })
977
+ const enduser2 = await sdk.api.endusers.createOne({ email: 'hi' + email })
970
978
  await sdk.api.endusers.set_password({ id: enduser.id, password }).catch(console.error)
971
979
  await enduserSDK.authenticate(email, password).catch(console.error)
972
980
 
@@ -1019,6 +1027,17 @@ const enduserAccessTests = async () => {
1019
1027
  }
1020
1028
  }
1021
1029
 
1030
+ await async_test(
1031
+ `enduser can update self`,
1032
+ () => enduserSDK.api.endusers.updateOne(enduser.id, { fname: "Sebastian", lname: "Coates" }),
1033
+ { onResult: e => e.id === enduser.id && e.fname === 'Sebastian' && e.lname === "Coates" }
1034
+ )
1035
+ await async_test(
1036
+ `enduser can't update other enduser`,
1037
+ () => enduserSDK.api.endusers.updateOne(enduser2.id, { fname: "Shouldn't Work"}),
1038
+ { shouldError: true, onError: e => e.message === "Could not find a record for the given id" }
1039
+ )
1040
+
1022
1041
  const ticketAccessible = await sdk.api.tickets.createOne({ enduserId: enduser.id, title: "Accessible ticket" })
1023
1042
  const ticketInaccessible = await sdk.api.tickets.createOne({ enduserId: PLACEHOLDER_ID, title: "Inaccessible ticket" })
1024
1043
  await async_test(
@@ -1045,6 +1064,7 @@ const enduserAccessTests = async () => {
1045
1064
  await sdk.api.tickets.deleteOne(ticketAccessible.id)
1046
1065
  await sdk.api.tickets.deleteOne(ticketInaccessible.id)
1047
1066
  await sdk.api.endusers.deleteOne(enduser.id)
1067
+ await sdk.api.endusers.deleteOne(enduser2.id)
1048
1068
  }
1049
1069
 
1050
1070
  const files_tests = async () => {
@@ -1053,7 +1073,7 @@ const files_tests = async () => {
1053
1073
  const { presignedUpload, file } = await sdk.api.files.prepare_file_upload({
1054
1074
  name: 'Test File', size: buff.byteLength, type: 'text/plain'
1055
1075
  })
1056
- await sdk.UPLOAD(presignedUpload, undefined, buff as any)
1076
+ await sdk.UPLOAD(presignedUpload, buff)
1057
1077
 
1058
1078
  const { downloadURL } = await sdk.api.files.file_download_URL({ secureName: file.secureName })
1059
1079
  const downloaded: string = await sdk.DOWNLOAD(downloadURL)
@@ -1075,6 +1095,43 @@ const enduser_session_tests = async () => {
1075
1095
  await sdk.api.endusers.deleteOne(enduser.id)
1076
1096
  }
1077
1097
 
1098
+ const users_tests = async () => {
1099
+ log_header("Users Tests")
1100
+ /* Update user tests */
1101
+ await async_test(
1102
+ `update user (non-admin, other user)`,
1103
+ () => sdkNonAdmin.api.users.updateOne(sdk.userInfo.id, { fname: 'Failure' }),
1104
+ { shouldError: true, onError: e => e.message === "Only admin users can update others' profiles" }
1105
+ )
1106
+ await async_test(
1107
+ `update user (non-admin, self)`,
1108
+ () => sdkNonAdmin.api.users.updateOne(sdkNonAdmin.userInfo.id, { fname: 'Updated' }),
1109
+ { onResult: u => u.id === sdkNonAdmin.userInfo.id && u.fname === "Updated" }
1110
+ )
1111
+ await async_test(
1112
+ `verify user update with admin get`,
1113
+ () => sdk.api.users.getOne(sdkNonAdmin.userInfo.id),
1114
+ { onResult: u => u.id === sdkNonAdmin.userInfo.id && u.fname === "Updated" }
1115
+ )
1116
+
1117
+ // reset fname to "Non" if this test throws, otherwise will falsely pass on next run
1118
+ assert(sdkNonAdmin.userInfo.fname === 'Updated', 'refresh_session not called on self after update', 'sdk updated on user update')
1119
+
1120
+ await async_test(
1121
+ `update user (admin, other user)`,
1122
+ () => sdk.api.users.updateOne(sdkNonAdmin.userInfo.id, { fname: 'Non' }), // change back
1123
+ { onResult: u => u.id === sdkNonAdmin.userInfo.id && u.fname === "Non" }
1124
+ )
1125
+ sdkNonAdmin.userInfo.fname = 'Non' // update back in sdk instance as well
1126
+
1127
+ await async_test(
1128
+ `verify user update with admin get`,
1129
+ () => sdk.api.users.getOne(sdkNonAdmin.userInfo.id),
1130
+ { onResult: u => u.id === sdkNonAdmin.userInfo.id && u.fname === "Non" }
1131
+ )
1132
+
1133
+ }
1134
+
1078
1135
  const tests: { [K in keyof ClientModelForName]: () => void } = {
1079
1136
  chats: chat_tests,
1080
1137
  endusers: enduser_tests,
@@ -1085,7 +1142,7 @@ const tests: { [K in keyof ClientModelForName]: () => void } = {
1085
1142
  emails: email_tests,
1086
1143
  sms_messages: sms_tests,
1087
1144
  chat_rooms: chat_room_tests,
1088
- users: () => {},
1145
+ users: users_tests,
1089
1146
  templates: () => {},
1090
1147
  files: files_tests,
1091
1148
  tickets: () => {},
@@ -1098,7 +1155,10 @@ const tests: { [K in keyof ClientModelForName]: () => void } = {
1098
1155
  log_header("API")
1099
1156
 
1100
1157
  try {
1101
- await sdk.authenticate(email, password)
1158
+ await Promise.all([
1159
+ sdk.authenticate(email, password),
1160
+ sdkNonAdmin.authenticate(nonAdminEmail, nonAdminPassword),
1161
+ ])
1102
1162
  await setup_tests()
1103
1163
  await multi_tenant_tests() // should come right after setup tests
1104
1164
  await badInputTests()
@@ -21,6 +21,7 @@ import {
21
21
  } from "@tellescope/types-models"
22
22
 
23
23
  import { Session } from "../sdk"
24
+ import { ChatMessage } from "@tellescope/types-client"
24
25
 
25
26
  const [email, password] = [process.env.TEST_EMAIL, process.env.TEST_PASSWORD]
26
27
  const [email2, password2] = [process.env.TEST_EMAIL_2, process.env.TEST_PASSWORD_2]
@@ -92,7 +93,20 @@ const chats_tests = async (isSubscribed: boolean) => {
92
93
  const room = await sdk.api.chat_rooms.createOne({ userIds: [sdk.userInfo.id] })
93
94
 
94
95
  const chat = await sdk.api.chats.createOne({ roomId: room.id, message: "Hello hello hi hello" })
95
- await check_next_webhook(a => objects_equivalent(a.records, [chat]), 'Create chat error', 'Create chat webhook', isSubscribed)
96
+ await check_next_webhook(
97
+ ({ records, relatedRecords }) => {
98
+ const record = records[0] as ChatMessage
99
+
100
+ return (
101
+ objects_equivalent(record, chat) &&
102
+ relatedRecords[record.roomId] !== undefined &&
103
+ relatedRecords[record.senderId as string] !== undefined &&
104
+ relatedRecords[record.roomId]?.id === room.id &&
105
+ relatedRecords[record.senderId as string]?.id === room.userIds?.[0]
106
+ )
107
+ },
108
+ 'Create chat error', 'Create chat webhook', isSubscribed
109
+ )
96
110
 
97
111
  // cleanup
98
112
  await sdk.api.chat_rooms.deleteOne(room.id) // also cleans up messages