import path from 'path'
import request from 'supertest'
import { useDatabase } from 'server/test/utils'
import factory from 'server/test/factory'
import app from './app'

describe('api routes', () => {
  useDatabase()

  describe('POST /builds', () => {
    describe('with no valid token', () => {
      it('should respond bad request', async () => {
        await request(app)
          .post('/builds')
          .set('Host', 'api.argos-ci.dev')
          .attach('screenshots[]', path.join(__dirname, '__fixtures__/screenshot_test.jpg'))
          .field('commit', 'test-commit')
          .field('branch', 'test-branch')
          .expect(401)
          .expect((res) => {
            expect(res.body.error.message).toBe('Invalid token')
          })
      })
    })

    describe('with no corresponding repository', () => {
      it('should respond bad request', async () => {
        await request(app)
          .post('/builds')
          .set('Host', 'api.argos-ci.dev')
          .attach('screenshots[]', path.join(__dirname, '__fixtures__/screenshot_test.jpg'))
          .field('commit', 'test-commit')
          .field('branch', 'test-branch')
          .field('token', 'xx')
          .expect(400)
          .expect((res) => {
            expect(res.body.error.message).toBe('Repository not found (token: "xx")')
          })
      })
    })

    describe('with repository not enabled', () => {
      it('should respond bad request', async () => {
        await factory.create('Repository', {
          enabled: false,
          token: 'xx',
        })

        await request(app)
          .post('/builds')
          .set('Host', 'api.argos-ci.dev')
          .attach('screenshots[]', path.join(__dirname, '__fixtures__/screenshot_test.jpg'))
          .field('commit', 'test-commit')
          .field('branch', 'test-branch')
          .field('token', 'xx')
          .expect(400)
          .expect((res) => {
            expect(res.body.error.message).toBe('Repository not enabled')
          })
      })
    })

    describe('with valid repository', () => {
      it('should upload screenshots, update database and return result', async () => {
        await factory.create('Repository', {
          enabled: true,
          token: 'xx',
        })

        await request(app)
          .post('/builds')
          .set('Host', 'api.argos-ci.dev')
          .attach('screenshots[]', path.join(__dirname, '__fixtures__/screenshot_test.jpg'))
          .field('commit', 'test-commit')
          .field('branch', 'test-branch')
          .field('token', 'xx')
          .expect((res) => {
            expect(res.body.id).not.toBeUndefined()
          })
          .expect(200)
      })
    })
  })

  describe('GET /buckets', () => {
    let repository

    beforeEach(async () => {
      repository = await factory.create('Repository')

      await factory.create('ScreenshotBucket', {
        name: 'test-bucket',
        commit: 'test-commit',
        branch: 'test-branch',
        repositoryId: repository.id,
      })
    })

    it('should returns buckets', () => {
      return request(app)
        .get('/buckets')
        .set('Host', 'api.argos-ci.dev')
        .expect(200)
        .expect((res) => {
          expect(res.body[0].name).toBe('test-bucket')
          expect(res.body[0].commit).toBe('test-commit')
          expect(res.body[0].branch).toBe('test-branch')
          expect(res.body[0].repositoryId).toBe(repository.id)
        })
    })

    describe('with query.branch', () => {
      it('should filter by branch', async () => {
        await request(app)
          .get('/buckets?branch=whatever')
          .set('Host', 'api.argos-ci.dev')
          .expect(200)
          .expect((res) => {
            expect(res.body).toHaveLength(0)
          })

        await request(app)
          .get('/buckets?branch=test-branch')
          .set('Host', 'api.argos-ci.dev')
          .expect(200)
          .expect((res) => {
            expect(res.body).toHaveLength(1)
          })
      })
    })
  })
})

@@ -622,31 +622,8 @@
 h')%0A
-          .expect(401)%0A
     
@@ -639,32 +639,32 @@
 pect((res) =%3E %7B%0A
+
             expe
@@ -717,32 +717,55 @@
 ')%0A          %7D)%0A
+          .expect(401)%0A
       %7D)%0A    %7D)%0A
@@ -1181,39 +1181,16 @@
 , 'xx')%0A
-          .expect(400)%0A
         
@@ -1301,32 +1301,55 @@
 ')%0A          %7D)%0A
+          .expect(400)%0A
       %7D)%0A    %7D)%0A
@@ -1866,39 +1866,16 @@
 , 'xx')%0A
-          .expect(400)%0A
         
@@ -1974,32 +1974,55 @@
 ')%0A          %7D)%0A
+          .expect(400)%0A
       %7D)%0A    %7D)%0A
@@ -3171,37 +3171,16 @@
 i.dev')%0A
-        .expect(200)%0A
         
@@ -3433,32 +3433,53 @@
 .id)%0A        %7D)%0A
+        .expect(200)%0A
     %7D)%0A%0A    desc
@@ -3675,39 +3675,16 @@
 i.dev')%0A
-          .expect(200)%0A
         
@@ -3753,32 +3753,55 @@
 (0)%0A          %7D)
+%0A          .expect(200)
 %0A%0A        await 
@@ -3902,39 +3902,16 @@
 i.dev')%0A
-          .expect(200)%0A
         
@@ -3976,16 +3976,16 @@
 ngth(1)%0A
-
         
@@ -3985,24 +3985,47 @@
          %7D)%0A
+          .expect(200)%0A
       %7D)%0A   

small change