Skip to content

Commit

Permalink
Merge pull request #558 from rfcx/bug/create-stream-with-0-not-null
Browse files Browse the repository at this point in the history
Fix that create stream receive latitude and longitude 0 not crea…
  • Loading branch information
Tooseriuz authored Mar 6, 2024
2 parents 1c3f6e9 + b3d5567 commit b1a8ad6
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
48 changes: 48 additions & 0 deletions core/streams/create.int.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,54 @@ describe('POST /streams', () => {
expect(stream.id).toBe(definedId)
})

test('returns 201 when latitude 0 and longitude 0 in request body, latitude and longitude to null', async () => {
const requestBody = {
name: 'test-stream-with-id',
latitude: 0,
longitude: 0
}

const response = await request(app).post('/').send(requestBody)

expect(response.statusCode).toBe(201)
const id = response.header.location.replace('/streams/', '')
const stream = await models.Stream.findByPk(id)
expect(stream.latitude).toBeNull()
expect(stream.longitude).toBeNull()
})

test('returns 201 when longitude 0 in request body, longitude to null', async () => {
const requestBody = {
name: 'test-stream-with-id',
latitude: 20,
longitude: 0
}

const response = await request(app).post('/').send(requestBody)

expect(response.statusCode).toBe(201)
const id = response.header.location.replace('/streams/', '')
const stream = await models.Stream.findByPk(id)
expect(stream.latitude).toBe(requestBody.latitude)
expect(stream.longitude).toBeNull()
})

test('returns 201 when latitude 0 in request body, latitude to null', async () => {
const requestBody = {
name: 'test-stream-with-id',
latitude: 0,
longitude: 19
}

const response = await request(app).post('/').send(requestBody)

expect(response.statusCode).toBe(201)
const id = response.header.location.replace('/streams/', '')
const stream = await models.Stream.findByPk(id)
expect(stream.latitude).toBeNull()
expect(stream.longitude).toBe(requestBody.longitude)
})

test('returns 201 when hidden in request body', async () => {
const requestBody = {
name: 'test-stream-with-id',
Expand Down
4 changes: 2 additions & 2 deletions core/streams/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ module.exports = (req, res) => {
}

if (params.latitude === 0) {
params.latitude = null
stream.latitude = null
}

if (params.longitude === 0) {
params.longitude = null
stream.longitude = null
}

if (params.projectId) {
Expand Down
48 changes: 48 additions & 0 deletions core/streams/update.int.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,54 @@ describe('PATCH /streams/:id', () => {
expect(streamUpdated.hidden).toBe(requestBody.hidden)
})

test('Success update latitude and longitude to 0, latitude and longitude to null', async () => {
const project = { id: 'ft1', name: 'Forest village', createdById: seedValues.primaryUserId }
await models.Project.create(project)
await models.UserProjectRole.create({ user_id: seedValues.primaryUserId, project_id: project.id, role_id: seedValues.roleOwner })
const stream = { id: 'am1', name: 'Big tree', latitude: 12, longitude: 13, projectId: project.id, createdById: seedValues.otherUserId }
await models.Stream.create(stream)

const requestBody = { latitude: 0, longitude: 0 }
const response = await request(app).patch(`/${stream.id}`).send(requestBody)

expect(response.statusCode).toBe(204)
const streamUpdated = await models.Stream.findByPk(stream.id)
expect(streamUpdated.latitude).toBeNull()
expect(streamUpdated.longitude).toBeNull()
})

test('Success update longitude to 0, longitude to null', async () => {
const project = { id: 'ft1', name: 'Forest village', createdById: seedValues.primaryUserId }
await models.Project.create(project)
await models.UserProjectRole.create({ user_id: seedValues.primaryUserId, project_id: project.id, role_id: seedValues.roleOwner })
const stream = { id: 'am1', name: 'Big tree', latitude: 12, longitude: 13, projectId: project.id, createdById: seedValues.otherUserId }
await models.Stream.create(stream)

const requestBody = { latitude: 12, longitude: 0 }
const response = await request(app).patch(`/${stream.id}`).send(requestBody)

expect(response.statusCode).toBe(204)
const streamUpdated = await models.Stream.findByPk(stream.id)
expect(streamUpdated.latitude).toBe(stream.latitude)
expect(streamUpdated.longitude).toBeNull()
})

test('Success update latitude to 0, latitude to null', async () => {
const project = { id: 'ft1', name: 'Forest village', createdById: seedValues.primaryUserId }
await models.Project.create(project)
await models.UserProjectRole.create({ user_id: seedValues.primaryUserId, project_id: project.id, role_id: seedValues.roleOwner })
const stream = { id: 'am1', name: 'Big tree', latitude: 12, longitude: 13, projectId: project.id, createdById: seedValues.otherUserId }
await models.Stream.create(stream)

const requestBody = { latitude: 0, longitude: 13 }
const response = await request(app).patch(`/${stream.id}`).send(requestBody)

expect(response.statusCode).toBe(204)
const streamUpdated = await models.Stream.findByPk(stream.id)
expect(streamUpdated.latitude).toBeNull()
expect(streamUpdated.longitude).toBe(stream.longitude)
})

test('forbidden by stream guest', async () => {
const stream = { id: 'am1', name: 'Big tree', createdById: seedValues.otherUserId }
await models.Stream.create(stream)
Expand Down

0 comments on commit b1a8ad6

Please sign in to comment.