Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix that create stream receive latitude and longitude 0 not crea… #558

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading