/* eslint-env node, mocha */
/* global $pg_database */
import request from 'superagent'
import knexCleaner from 'knex-cleaner'

import { getSingleton } from '../../app/app'
import { DummyPublisher } from '../../app/pubsub'
import { PubSub } from '../../app/models'
import * as funcTestHelper from './functional_test_helper'


describe('RequestRevocation', () => {
  let app

  before(async () => {
    app = await getSingleton()
    PubSub.setPublisher(new DummyPublisher())
  })

  beforeEach(async () => {
    await knexCleaner.clean($pg_database)
  })

  describe('user Luna, private users Mars, Zeus and private group pepyatka-dev', () => {
    const lunaContext = {}
    const marsContext = {}
    const zeusContext = {}

    beforeEach(funcTestHelper.createUserCtx(lunaContext, 'luna', 'pw'))
    beforeEach(funcTestHelper.createUserCtx(marsContext, 'mars', 'pw'))
    beforeEach(funcTestHelper.createUserCtx(zeusContext, 'zeus', 'pw'))
    beforeEach(() => funcTestHelper.goPrivate(marsContext))
    beforeEach(() => funcTestHelper.goPrivate(zeusContext))

    beforeEach((done) => {
      request
        .post(`${app.config.host}/v1/groups`)
        .send({
          group:     { username: 'pepyatka-dev', screenName: 'Pepyatka Developers', isPrivate: '1' },
          authToken: zeusContext.authToken
        })
        .end(() => {
          request
            .post(`${app.config.host}/v1/groups/pepyatka-dev/sendRequest`)
            .send({ authToken: lunaContext.authToken })
            .end(() => {
              request
                .post(`${app.config.host}/v1/users/${zeusContext.user.username}/sendRequest`)
                .send({ authToken: lunaContext.authToken })
                .end(() => {
                  done()
                })
            })
        })
    })

    it('should reject unauthenticated users', (done) => {
      request
        .post(`${app.config.host}/v2/requests/${marsContext.user.username}/revoke`)
        .end((err) => {
          err.should.not.be.empty
          err.status.should.eql(401)
          done()
        })
    })

    it('should reject nonexisting user', (done) => {
      request
        .post(`${app.config.host}/v2/requests/foobar/revoke`)
        .send({ authToken: lunaContext.authToken })
        .end((err) => {
          err.should.not.be.empty
          err.status.should.eql(404)
          done()
        })
    })

    it('should reject nonexisting subscription request to user', (done) => {
      request
        .post(`${app.config.host}/v2/requests/${marsContext.user.username}/revoke`)
        .send({ authToken: lunaContext.authToken })
        .end((err) => {
          err.should.not.be.empty
          err.status.should.eql(404)
          done()
        })
    })

    it('should reject nonexisting subscription request to group', (done) => {
      request
        .post(`${app.config.host}/v2/requests/pepyatka-dev/revoke`)
        .send({ authToken: marsContext.authToken })
        .end((err) => {
          err.should.not.be.empty
          err.status.should.eql(404)
          done()
        })
    })

    it('should remove existing subscription request to group', (done) => {
      request
        .post(`${app.config.host}/v2/requests/pepyatka-dev/revoke`)
        .send({ authToken: lunaContext.authToken })
        .end((err, res) => {
          res.should.not.be.empty
          res.status.should.eql(200)
          // TODO: check lunaContext.whoami (pendingRequests)
          // TODO: check zeusContext.whoami (pendingRequests)
          done()
        })
    })

    it('should remove existing subscription request to user', (done) => {
      request
        .post(`${app.config.host}/v2/requests/${zeusContext.user.username}/revoke`)
        .send({ authToken: lunaContext.authToken })
        .end((err, res) => {
          res.should.not.be.empty
          res.status.should.eql(200)
          // TODO: check lunaContext.whoami (pendingRequests)
          // TODO: check zeusContext.whoami (pendingRequests)
          done()
        })
    })
  })
})

@@ -642,20 +642,18 @@
 %3E %7B%0A    
-cons
+le
 t lunaCo
@@ -667,20 +667,18 @@
  %7B%7D%0A    
-cons
+le
 t marsCo
@@ -696,12 +696,10 @@
     
-cons
+le
 t ze
@@ -721,32 +721,122 @@
 %0A    beforeEach(
+async () =%3E %7B%0A      %5BlunaContext, marsContext, zeusContext%5D = await Promise.all(%5B%0A        
 funcTestHelper.c
@@ -848,25 +848,14 @@
 User
-Ctx(lunaContext, 
+Async(
 'lun
@@ -863,33 +863,26 @@
 ', 'pw')
-)
+,
 %0A    
-beforeEach(
+    
 funcTest
@@ -902,25 +902,14 @@
 User
-Ctx(marsContext, 
+Async(
 'mar
@@ -917,33 +917,26 @@
 ', 'pw')
-)
+,
 %0A    
-beforeEach(
+    
 funcTest
@@ -956,60 +956,90 @@
 User
-Ctx(zeusContext, 'zeus', 'pw'))%0A    beforeEach(() =%3E
+Async('zeus', 'pw')%0A      %5D)%0A%0A      const %5B,, group%5D = await Promise.all(%5B%0A       
  fun
@@ -1076,30 +1076,17 @@
 ext)
-)
+,
 %0A    
-beforeEach(() =%3E
+   
  fun
@@ -1123,146 +1123,62 @@
 ext)
-)%0A
+,
 %0A    
-beforeEach((done) =%3E %7B%0A      request%0A        .post(%60$%7Bapp.config.host%7D/v1/groups%60)%0A        .send(%7B%0A          group:     %7B username:
+    funcTestHelper.createGroupAsync(zeusContext,
  'pe
@@ -1193,20 +1193,8 @@
 ev',
- screenName:
  'Pe
@@ -1217,549 +1217,198 @@
 s', 
-isPrivate: '1' %7D,%0A          authToken: zeusContext.authToken%0A        %7D)%0A        .end(() =%3E %7B%0A          request%0A            .post(%60$%7Bapp.config.host%7D/v1/groups/pepyatka-dev/sendRequest%60)%0A            .send(%7B authToken: lunaContext.authToken %7D)%0A            .end(() =%3E %7B%0A              request%0A                .post(%60$%7Bapp.config.host%7D/v1/users/$%7BzeusContext.user.username%7D/sendRequest%60)%0A                .send(%7B authToken: lunaContext.authToken %7D)%0A                .end(() =%3E %7B%0A                  done()%0A                %7D)%0A            %7D)%0A        %7D)
+true)%0A      %5D)%0A%0A      await Promise.all(%5B%0A        funcTestHelper.sendRequestToJoinGroup(lunaContext, group),%0A        funcTestHelper.sendRequestToSubscribe(lunaContext, zeusContext)%0A      %5D);
 %0A   

Optimize "request_revocation" tests