Skip to content

Commit f62628e

Browse files
authored
Merge pull request particle-iot#62 from AntonPuko/uuid
add uuid package
2 parents 6c0aec8 + 1570930 commit f62628e

File tree

8 files changed

+37
-28
lines changed

8 files changed

+37
-28
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"request": "*",
6060
"spark-protocol": "../spark-protocol",
6161
"ursa": "*",
62+
"uuid": "^3.0.1",
6263
"when": "*",
6364
"xtend": "*"
6465
},

src/controllers/WebhooksController.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,8 @@ class WebhooksController extends Controller {
6262
throw validateError;
6363
}
6464

65-
const newWebhook = await this._webhookRepository.create({
66-
...model,
67-
created_at: new Date(),
68-
id: '',
69-
});
65+
const newWebhook = await this._webhookRepository.create(model);
66+
7067
return this.ok({
7168
created_at: newWebhook.created_at,
7269
event: newWebhook.event,

src/repository/UserFileRepository.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import type { TokenObject, User, UserCredentials } from '../types';
44

5-
import { JSONFileManager, uuid } from 'spark-protocol';
5+
import uuid from 'uuid';
6+
import { JSONFileManager } from 'spark-protocol';
67
import PasswordHasher from '../lib/PasswordHasher';
78
import HttpError from '../lib/HttpError';
89

@@ -20,12 +21,16 @@ class UserFileRepository {
2021

2122
const salt = await PasswordHasher.generateSalt();
2223
const passwordHash = await PasswordHasher.hash(password, salt);
24+
let id = uuid();
25+
while (await this.getById(id)) {
26+
id = uuid();
27+
}
2328

2429
const modelToSave = {
2530
accessTokens: [],
2631
created_at: new Date(),
2732
created_by: null,
28-
id: uuid(),
33+
id,
2934
passwordHash,
3035
salt,
3136
username,
@@ -43,10 +48,10 @@ class UserFileRepository {
4348
throw new HttpError('Not implemented');
4449
};
4550

46-
getAll = (): Promise<Array<User>> =>
51+
getAll = async (): Promise<Array<User>> =>
4752
this._fileManager.getAllData();
4853

49-
getById = (id: string): Promise<?User> =>
54+
getById = async (id: string): Promise<?User> =>
5055
this._fileManager.getFile(`${id}.json`);
5156

5257
getByUsername = async (username: string): Promise<?User> =>
@@ -91,7 +96,7 @@ class UserFileRepository {
9196
this._fileManager.writeFile(`${user.id}.json`, userToSave);
9297
};
9398

94-
deleteById = (id: string): Promise<void> =>
99+
deleteById = async (id: string): Promise<void> =>
95100
this._fileManager.deleteFile(`${id}.json`);
96101

97102

src/repository/WebhookFileRepository.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// @flow
22

3-
import type { Webhook } from '../types';
3+
import type { Webhook, WebhookMutator } from '../types';
44

5-
import { JSONFileManager, uuid } from 'spark-protocol';
5+
import uuid from 'uuid';
6+
import { JSONFileManager } from 'spark-protocol';
67
import HttpError from '../lib/HttpError';
78

89
class WebhookFileRepository {
@@ -12,11 +13,16 @@ class WebhookFileRepository {
1213
this._fileManager = new JSONFileManager(path);
1314
}
1415

15-
create = async (model: Webhook): Promise<Webhook> => {
16+
create = async (model: WebhookMutator): Promise<Webhook> => {
17+
let id = uuid();
18+
while (await this.getById(id)) {
19+
id = uuid();
20+
}
21+
1622
const modelToSave = {
1723
...model,
1824
created_at: new Date(),
19-
id: uuid(),
25+
id,
2026
};
2127

2228
this._fileManager.createFile(`${modelToSave.id}.json`, modelToSave);
@@ -32,7 +38,7 @@ class WebhookFileRepository {
3238
getById = async (id: string): Promise<?Webhook> =>
3339
this._fileManager.getFile(`${id}.json`);
3440

35-
update = (model: Webhook): Promise<Webhook> => {
41+
update = async (model: WebhookMutator): Promise<Webhook> => {
3642
throw new HttpError('Not implemented');
3743
};
3844
}

test/DevicesController.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ test.serial('should claim device', async t => {
134134
// TODO write test for checking the error if device belongs to somebody else
135135
// TODO write tests for updateDevice & callFunction
136136

137-
test.after.always(() => {
138-
settings.usersRepository.deleteById(testUser.id);
139-
settings.deviceAttributeRepository.deleteById(DEVICE_ID);
140-
settings.deviceKeyFileRepository.delete(DEVICE_ID);
137+
test.after.always(async (): Promise<void> => {
138+
await settings.usersRepository.deleteById(testUser.id);
139+
await settings.deviceAttributeRepository.deleteById(DEVICE_ID);
140+
await settings.deviceKeyFileRepository.delete(DEVICE_ID);
141141
});

test/ProvisioningController.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ test('should throw an error if public key is not provided', async t => {
7575
t.is(response.body.error, 'No key provided');
7676
});
7777

78-
test.after.always(() => {
79-
settings.usersRepository.deleteById(testUser.id);
80-
settings.deviceAttributeRepository.deleteById(DEVICE_ID);
81-
settings.deviceKeyFileRepository.delete(DEVICE_ID);
78+
test.after.always(async (): Promise<void> => {
79+
await settings.usersRepository.deleteById(testUser.id);
80+
await settings.deviceAttributeRepository.deleteById(DEVICE_ID);
81+
await settings.deviceKeyFileRepository.delete(DEVICE_ID);
8282
});

test/UsersController.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,6 @@ test.serial('should deleteById access token for the user', async t => {
8181
));
8282
});
8383

84-
test.after.always(() => {
85-
settings.usersRepository.deleteById(user.id);
84+
test.after.always(async (): Promise<void> => {
85+
await settings.usersRepository.deleteById(user.id);
8686
});

test/WebhooksController.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ test.serial('should delete webhook', async t => {
157157
));
158158
});
159159

160-
test.after.always(() => {
161-
settings.webhookRepository.deleteById(testWebhook.id);
162-
settings.usersRepository.deleteById(testUser.id);
160+
test.after.always(async (): Promise<void> => {
161+
await settings.webhookRepository.deleteById(testWebhook.id);
162+
await settings.usersRepository.deleteById(testUser.id);
163163
});

0 commit comments

Comments
 (0)