@creator.co/wapi 1.8.8 → 1.9.1

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,12 +1,12 @@
1
1
  import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager'
2
2
  import { SSMClient, GetParameterCommand } from '@aws-sdk/client-ssm'
3
- import { mockClient } from 'aws-sdk-client-mock'
4
3
  import { expect } from 'chai'
5
4
 
6
5
  import EnvironmentVar, { EnvironmentType } from '../../src/Config/EnvironmentVar.js'
6
+ import { mockAwsClient, mockAwsCommand, mockAwsResult } from '../AwsSdkTest.utils.js'
7
7
 
8
- const SSMMock = mockClient(SSMClient)
9
- const SecretsManagerMock = mockClient(SecretsManagerClient)
8
+ const SSMMock = mockAwsClient(SSMClient)
9
+ const SecretsManagerMock = mockAwsClient(SecretsManagerClient)
10
10
 
11
11
  function testRemoteEnv(mock, type, command) {
12
12
  describe(`Optional ${type} environment`, () => {
@@ -16,17 +16,19 @@ function testRemoteEnv(mock, type, command) {
16
16
  })
17
17
 
18
18
  test('Does not accept sync resolve', async () => {
19
- mock.on(command).resolves({
20
- ...(type == EnvironmentType.PlainRemote
21
- ? {
22
- Parameter: {
23
- Value: undefined,
24
- },
25
- }
26
- : {
27
- SecretString: undefined,
28
- }),
29
- })
19
+ mock.on(mockAwsCommand(command)).resolves(
20
+ mockAwsResult({
21
+ ...(type == EnvironmentType.PlainRemote
22
+ ? {
23
+ Parameter: {
24
+ Value: undefined,
25
+ },
26
+ }
27
+ : {
28
+ SecretString: undefined,
29
+ }),
30
+ })
31
+ )
30
32
  let err = null
31
33
  try {
32
34
  const env = new EnvironmentVar('abc', type, true)
@@ -39,24 +41,26 @@ function testRemoteEnv(mock, type, command) {
39
41
  })
40
42
 
41
43
  test('Optional value with null value', async () => {
42
- mock.on(command).resolves({
43
- ...(type == EnvironmentType.PlainRemote
44
- ? {
45
- Parameter: {
46
- Value: undefined,
47
- },
48
- }
49
- : {
50
- SecretString: undefined,
51
- }),
52
- })
44
+ mock.on(mockAwsCommand(command)).resolves(
45
+ mockAwsResult({
46
+ ...(type == EnvironmentType.PlainRemote
47
+ ? {
48
+ Parameter: {
49
+ Value: undefined,
50
+ },
51
+ }
52
+ : {
53
+ SecretString: undefined,
54
+ }),
55
+ })
56
+ )
53
57
  const env = new EnvironmentVar('abc', type, true)
54
58
  const token = await env.resolve()
55
59
  expect(token).is.undefined
56
60
  })
57
61
 
58
62
  test('Optional value with empty response', async () => {
59
- mock.on(command).resolves({})
63
+ mock.on(mockAwsCommand(command)).resolves(mockAwsResult({}))
60
64
  const env = new EnvironmentVar('abc', type, true)
61
65
  const token = await env.resolve()
62
66
  expect(token).is.undefined
@@ -64,17 +68,19 @@ function testRemoteEnv(mock, type, command) {
64
68
 
65
69
  test('Optional value with valid response', async () => {
66
70
  const value = 'abc'
67
- mock.on(command).resolves({
68
- ...(type == EnvironmentType.PlainRemote
69
- ? {
70
- Parameter: {
71
- Value: value,
72
- },
73
- }
74
- : {
75
- SecretString: value,
76
- }),
77
- })
71
+ mock.on(mockAwsCommand(command)).resolves(
72
+ mockAwsResult({
73
+ ...(type == EnvironmentType.PlainRemote
74
+ ? {
75
+ Parameter: {
76
+ Value: value,
77
+ },
78
+ }
79
+ : {
80
+ SecretString: value,
81
+ }),
82
+ })
83
+ )
78
84
  const env = new EnvironmentVar(value, type, true)
79
85
  const token = await env.resolve()
80
86
  expect(token).is.not.undefined
@@ -83,7 +89,7 @@ function testRemoteEnv(mock, type, command) {
83
89
 
84
90
  test('Optional value rejection', async () => {
85
91
  const value = 'abc'
86
- mock.on(command).rejects(new Error('failed!'))
92
+ mock.on(mockAwsCommand(command)).rejects(new Error('failed!'))
87
93
  const env = new EnvironmentVar(value, type, true)
88
94
  const token = await env.resolve()
89
95
  expect(token).is.undefined
@@ -97,17 +103,19 @@ function testRemoteEnv(mock, type, command) {
97
103
  })
98
104
 
99
105
  test('Does not accept sync resolve', async () => {
100
- mock.on(command).resolves({
101
- ...(type == EnvironmentType.PlainRemote
102
- ? {
103
- Parameter: {
104
- Value: undefined,
105
- },
106
- }
107
- : {
108
- SecretString: undefined,
109
- }),
110
- })
106
+ mock.on(mockAwsCommand(command)).resolves(
107
+ mockAwsResult({
108
+ ...(type == EnvironmentType.PlainRemote
109
+ ? {
110
+ Parameter: {
111
+ Value: undefined,
112
+ },
113
+ }
114
+ : {
115
+ SecretString: undefined,
116
+ }),
117
+ })
118
+ )
111
119
  let err = null
112
120
  try {
113
121
  const env = new EnvironmentVar('abc', type)
@@ -120,17 +128,19 @@ function testRemoteEnv(mock, type, command) {
120
128
  })
121
129
 
122
130
  test('Required fails when null', async () => {
123
- mock.on(command).resolves({
124
- ...(type == EnvironmentType.PlainRemote
125
- ? {
126
- Parameter: {
127
- Value: undefined,
128
- },
129
- }
130
- : {
131
- SecretString: undefined,
132
- }),
133
- })
131
+ mock.on(mockAwsCommand(command)).resolves(
132
+ mockAwsResult({
133
+ ...(type == EnvironmentType.PlainRemote
134
+ ? {
135
+ Parameter: {
136
+ Value: undefined,
137
+ },
138
+ }
139
+ : {
140
+ SecretString: undefined,
141
+ }),
142
+ })
143
+ )
134
144
  let err = null
135
145
  try {
136
146
  const env = new EnvironmentVar('abc', type)
@@ -144,7 +154,7 @@ function testRemoteEnv(mock, type, command) {
144
154
  })
145
155
 
146
156
  test('Required fails when empty response', async () => {
147
- mock.on(command).resolves({})
157
+ mock.on(mockAwsCommand(command)).resolves(mockAwsResult({}))
148
158
  let err = null
149
159
  try {
150
160
  const env = new EnvironmentVar('abc', type)
@@ -158,17 +168,19 @@ function testRemoteEnv(mock, type, command) {
158
168
 
159
169
  test('Required value with valid response', async () => {
160
170
  const value = 'abc'
161
- mock.on(command).resolves({
162
- ...(type == EnvironmentType.PlainRemote
163
- ? {
164
- Parameter: {
165
- Value: value,
166
- },
167
- }
168
- : {
169
- SecretString: value,
170
- }),
171
- })
171
+ mock.on(mockAwsCommand(command)).resolves(
172
+ mockAwsResult({
173
+ ...(type == EnvironmentType.PlainRemote
174
+ ? {
175
+ Parameter: {
176
+ Value: value,
177
+ },
178
+ }
179
+ : {
180
+ SecretString: value,
181
+ }),
182
+ })
183
+ )
172
184
  const env = new EnvironmentVar(value, type)
173
185
  const token = await env.resolve()
174
186
  expect(token).is.not.undefined
@@ -176,7 +188,7 @@ function testRemoteEnv(mock, type, command) {
176
188
  })
177
189
 
178
190
  test('Required value rejection', async () => {
179
- mock.on(command).rejects(new Error('failed!'))
191
+ mock.on(mockAwsCommand(command)).rejects(new Error('failed!'))
180
192
  let err = null
181
193
  try {
182
194
  const env = new EnvironmentVar('abc', type)
@@ -1,10 +1,10 @@
1
1
  import { DecryptCommand, EncryptCommand, KMSClient } from '@aws-sdk/client-kms'
2
- import { mockClient } from 'aws-sdk-client-mock'
3
2
  import { expect } from 'chai'
4
3
 
5
4
  import Crypto from '../../src/Crypto/Crypto.js'
5
+ import { mockAwsClient, mockAwsCommand, mockAwsResult } from '../AwsSdkTest.utils.js'
6
6
 
7
- const KMSMock = mockClient(KMSClient)
7
+ const KMSMock = mockAwsClient(KMSClient)
8
8
 
9
9
  function fakeEncryption(v) {
10
10
  const f = new TextEncoder().encode(v)
@@ -23,9 +23,11 @@ describe('Encryption', () => {
23
23
  const provider = new Crypto('ca-central-1', 'abc123')
24
24
  test('Encrypts json object', async () => {
25
25
  const encryptionObj = { userID: 123 }
26
- KMSMock.on(EncryptCommand).resolves({
27
- CiphertextBlob: new TextEncoder().encode(JSON.stringify(encryptionObj)),
28
- })
26
+ KMSMock.on(mockAwsCommand(EncryptCommand)).resolves(
27
+ mockAwsResult({
28
+ CiphertextBlob: new TextEncoder().encode(JSON.stringify(encryptionObj)),
29
+ })
30
+ )
29
31
  const token = await provider.encryptData(encryptionObj)
30
32
  expect(token).is.not.null
31
33
  expect(token).to.be.equals(fakeEncryption(JSON.stringify(encryptionObj)))
@@ -33,9 +35,11 @@ describe('Encryption', () => {
33
35
 
34
36
  test('Encrypts string', async () => {
35
37
  const encryptionText = 'Hello encryption!'
36
- KMSMock.on(EncryptCommand).resolves({
37
- CiphertextBlob: new TextEncoder().encode(encryptionText),
38
- })
38
+ KMSMock.on(mockAwsCommand(EncryptCommand)).resolves(
39
+ mockAwsResult({
40
+ CiphertextBlob: new TextEncoder().encode(encryptionText),
41
+ })
42
+ )
39
43
  const token = await provider.encryptData(encryptionText)
40
44
  expect(token).is.not.null
41
45
  expect(token).to.be.equals(fakeEncryption(encryptionText))
@@ -43,7 +47,7 @@ describe('Encryption', () => {
43
47
 
44
48
  test('Fails to encrypt a number', async () => {
45
49
  const encryptionText = 123
46
- KMSMock.on(EncryptCommand).rejects(new Error('failed'))
50
+ KMSMock.on(mockAwsCommand(EncryptCommand)).rejects(new Error('failed'))
47
51
  const token = await provider.encryptData(encryptionText)
48
52
  expect(token).is.null
49
53
  })
@@ -58,9 +62,11 @@ describe('Decryption', () => {
58
62
  const provider = new Crypto('ca-central-1', 'abc123')
59
63
  test('Decrypts json object', async () => {
60
64
  const encryptionObj = { userID: 123 }
61
- KMSMock.on(DecryptCommand).resolves({
62
- Plaintext: fakeDecryption(JSON.stringify(encryptionObj)),
63
- })
65
+ KMSMock.on(mockAwsCommand(DecryptCommand)).resolves(
66
+ mockAwsResult({
67
+ Plaintext: fakeDecryption(JSON.stringify(encryptionObj)),
68
+ })
69
+ )
64
70
  const token = await provider.decryptData(fakeEncryption(JSON.stringify(encryptionObj)))
65
71
  expect(token).is.not.null
66
72
  expect(token).to.be.equals(JSON.stringify(encryptionObj))
@@ -68,9 +74,11 @@ describe('Decryption', () => {
68
74
 
69
75
  test('Decrypts string', async () => {
70
76
  const encryptionText = 'abc123'
71
- KMSMock.on(DecryptCommand).resolves({
72
- Plaintext: fakeDecryption(encryptionText),
73
- })
77
+ KMSMock.on(mockAwsCommand(DecryptCommand)).resolves(
78
+ mockAwsResult({
79
+ Plaintext: fakeDecryption(encryptionText),
80
+ })
81
+ )
74
82
  const token = await provider.decryptData(fakeEncryption(encryptionText))
75
83
  expect(token).is.not.null
76
84
  expect(token).to.be.equals(encryptionText)
@@ -78,7 +86,7 @@ describe('Decryption', () => {
78
86
 
79
87
  test('Fails to decrypt a number', async () => {
80
88
  const encryptionText = 123
81
- KMSMock.on(DecryptCommand).rejects(new Error('failed'))
89
+ KMSMock.on(mockAwsCommand(DecryptCommand)).rejects(new Error('failed'))
82
90
  // @ts-ignore
83
91
  const token = await provider.decryptData(encryptionText)
84
92
  expect(token).is.null
@@ -1,10 +1,10 @@
1
1
  import { SESv2Client, SendEmailCommand } from '@aws-sdk/client-sesv2'
2
- import { mockClient } from 'aws-sdk-client-mock'
3
2
  import { expect } from 'chai'
4
3
 
5
4
  import Mailer from '../../src/Mailer/Mailer.js'
5
+ import { mockAwsClient, mockAwsCommand, mockAwsResult } from '../AwsSdkTest.utils.js'
6
6
 
7
- const SESMock = mockClient(SESv2Client)
7
+ const SESMock = mockAwsClient(SESv2Client)
8
8
 
9
9
  describe('SendRaw', () => {
10
10
  // reset mock
@@ -16,9 +16,11 @@ describe('SendRaw', () => {
16
16
 
17
17
  test('Send raw with success', async () => {
18
18
  const messageId = 'id-123'
19
- SESMock.on(SendEmailCommand).resolves({
20
- MessageId: messageId,
21
- })
19
+ SESMock.on(mockAwsCommand(SendEmailCommand)).resolves(
20
+ mockAwsResult({
21
+ MessageId: messageId,
22
+ })
23
+ )
22
24
  const res = await provider.sendRawEmail('test@test.com', 'My message', 'My subject')
23
25
  expect(res).is.not.null
24
26
  console.log(res)
@@ -32,16 +34,18 @@ describe('SendRaw', () => {
32
34
  const subject = 'My subject'
33
35
  const cc = 'test@test3.com'
34
36
  const replyTo = 'test@test4.com'
35
- SESMock.on(SendEmailCommand).resolves({
36
- MessageId: messageId,
37
- })
37
+ SESMock.on(mockAwsCommand(SendEmailCommand)).resolves(
38
+ mockAwsResult({
39
+ MessageId: messageId,
40
+ })
41
+ )
38
42
  const res = await provider.sendRawEmail(to, msg, subject, cc, from, replyTo)
39
43
  expect(res).is.not.null
40
44
  console.log(res)
41
45
  })
42
46
 
43
47
  test('Send raw with failure', async () => {
44
- SESMock.on(SendEmailCommand).rejects(new Error('failed!'))
48
+ SESMock.on(mockAwsCommand(SendEmailCommand)).rejects(new Error('failed!'))
45
49
  let res: any = null,
46
50
  err: any = null
47
51
  try {
@@ -1,10 +1,10 @@
1
1
  import { SNSClient, PublishCommand } from '@aws-sdk/client-sns'
2
- import { mockClient } from 'aws-sdk-client-mock'
3
2
  import { expect } from 'chai'
4
3
 
5
4
  import Publisher from '../../src/Publisher/Publisher.js'
5
+ import { mockAwsClient, mockAwsCommand, mockAwsResult } from '../AwsSdkTest.utils.js'
6
6
 
7
- const SNSMock = mockClient(SNSClient)
7
+ const SNSMock = mockAwsClient(SNSClient)
8
8
 
9
9
  describe('SNS Pub-Sub', () => {
10
10
  // reset mock
@@ -20,12 +20,14 @@ describe('SNS Pub-Sub', () => {
20
20
  const msg = { text: '123' }
21
21
  const topic = '123'
22
22
  const messageId = 'id-123'
23
- SNSMock.on(PublishCommand, {
23
+ SNSMock.on(mockAwsCommand(PublishCommand), {
24
24
  Message: JSON.stringify(msg),
25
25
  TopicArn: topic,
26
- }).resolves({
27
- MessageId: messageId,
28
- })
26
+ }).resolves(
27
+ mockAwsResult({
28
+ MessageId: messageId,
29
+ })
30
+ )
29
31
  const res = await provider.publishOnTopic(msg, topic)
30
32
  expect(res).is.not.null
31
33
  expect(res?.MessageId).to.be.equals(messageId)
@@ -35,12 +37,14 @@ describe('SNS Pub-Sub', () => {
35
37
  const msg = { text: '123' }
36
38
  const topic = '123'
37
39
  const messageId = 'id-123'
38
- SNSMock.on(PublishCommand, {
40
+ SNSMock.on(mockAwsCommand(PublishCommand), {
39
41
  Message: JSON.stringify(msg),
40
42
  TopicArn: topic,
41
- }).resolves({
42
- MessageId: messageId,
43
- })
43
+ }).resolves(
44
+ mockAwsResult({
45
+ MessageId: messageId,
46
+ })
47
+ )
44
48
  const res = await provider.publishOnTopic(msg, topic, {
45
49
  FifoGroup: 123,
46
50
  })
@@ -51,7 +55,7 @@ describe('SNS Pub-Sub', () => {
51
55
  test('Publish failure', async () => {
52
56
  const msg = { text: '123' }
53
57
  const topic = '123'
54
- SNSMock.on(PublishCommand).rejects(new Error('failed!'))
58
+ SNSMock.on(mockAwsCommand(PublishCommand)).rejects(new Error('failed!'))
55
59
  const res = await provider.publishOnTopic(msg, topic)
56
60
  expect(res).is.undefined
57
61
  })
@@ -71,12 +75,14 @@ describe('SNS SMS', () => {
71
75
  const msg = 'hello world'
72
76
  const phoneNumber = '+16042001234'
73
77
  const messageId = 'id-123'
74
- SNSMock.on(PublishCommand, {
78
+ SNSMock.on(mockAwsCommand(PublishCommand), {
75
79
  Message: msg,
76
80
  PhoneNumber: phoneNumber,
77
- }).resolves({
78
- MessageId: messageId,
79
- })
81
+ }).resolves(
82
+ mockAwsResult({
83
+ MessageId: messageId,
84
+ })
85
+ )
80
86
  const res = await provider.publishSMS(msg, phoneNumber)
81
87
  expect(res).is.not.null
82
88
  expect(res?.MessageId).to.be.equals(messageId)
@@ -85,7 +91,7 @@ describe('SNS SMS', () => {
85
91
  test('Publish failure', async () => {
86
92
  const msg = 'hello world'
87
93
  const phoneNumber = '+16042001234'
88
- SNSMock.on(PublishCommand).rejects(new Error('failed!'))
94
+ SNSMock.on(mockAwsCommand(PublishCommand)).rejects(new Error('failed!'))
89
95
  const res = await provider.publishSMS(msg, phoneNumber)
90
96
  expect(res).is.undefined
91
97
  })