-
Notifications
You must be signed in to change notification settings - Fork 11
/
iotMock.js
108 lines (99 loc) · 3.32 KB
/
iotMock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const AWSMock = require('aws-sdk-mock')
const path = require('path')
module.exports = (client, redisClient) => {
AWSMock.mock('IotData', 'publish', (params, callback) => {
const {topic, payload} = params
client.publish(topic, payload, callback)
})
AWSMock.mock('IotData', 'getThingShadow', (params, callback) => {
redisClient.get(params.thingName, (err, result) => {
callback(err || null, {payload: result})
})
})
AWSMock.mock('IotData', 'updateThingShadow', (params, callback) => {
client.publish(`$aws/things/${params.thingName}/shadow/update`, params.payload, callback)
})
AWSMock.mock('Iot', 'createPolicy', (params, callback) => {
redisClient.get(params.policyName, (err, data) => {
if (err || data) {
callback(data ? {code: 'ResourceAlreadyExistsException'} : err)
} else {
const policy = {
versions: {
'1': {
policyDocument: params.policyDocument,
targets: [],
timestamp: (new Date()).getTime()
}
},
defaultVersion: '1'
}
redisClient.set(params.policyName, JSON.stringify(policy), (err) => {
if (err) {
callback(err)
} else {
callback(null, {
policyDocument: params.policyDocument,
policyName: params.policyName,
policyVersionId: 1,
policyArn: ''
})
}
})
}
})
})
AWSMock.mock('Iot', 'listTargetsForPolicy', (params, callback) => {
redisClient.get(params.policyName, (err, res) => {
if (err || !res) {
callback(err || {message: `Policy ${params.policyName} does not exists`})
} else {
const policy = JSON.parse(res)
callback(null, {targets: policy.versions[policy.defaultVersion].targets})
}
})
})
AWSMock.mock('Iot', 'detachPolicy', (params, callback) => {
redisClient.get(params.policyName, (err, res) => {
if (err || !res) {
callback(err || {message: `Policy ${params.policyName} does not exists`})
} else {
const policy = JSON.parse(res)
const principal = `${process.env.AWS_ACCOUNT_ID}:${params.target}`
const targets = policy.versions[policy.defaultVersion].targets
const index = targets.indexOf(principal)
if (index !== -1) {
targets.splice(index, 1)
}
redisClient.set(params.policyName, JSON.stringify(policy), (err) => {
if (err) {
callback(err)
} else {
callback(null)
}
})
}
})
})
AWSMock.mock('Iot', 'attachPolicy', (params, callback) => {
redisClient.get(params.policyName, (err, res) => {
if (err || !res) {
callback(err || {message: `Policy ${params.policyName} does not exists`})
} else {
const policy = JSON.parse(res)
const principal = `${process.env.AWS_ACCOUNT_ID}:${params.target}`
const targets = policy.versions[policy.defaultVersion].targets
if (!targets.find((target) => target === principal)) {
targets.push(principal)
}
redisClient.set(params.policyName, JSON.stringify(policy), (err) => {
if (err) {
callback(err)
} else {
callback(null)
}
})
}
})
})
}