Socialify

Folder ..

Viewing pull.test.js
194 lines (168 loc) • 5.4 KB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import GDrive from 'main/application/sync/gdrive'
import { google } from 'googleapis'

describe('#pull', () => {
  let sync, credentials, vault

  beforeEach(() => {
    const cryptor = { decrypt: () => credentials }
    vault = {
      write: jest.fn(),
      isDecryptable: data => data !== null
    }
    sync = new GDrive()
    sync.initialize(vault, cryptor)
  })

  afterEach(() => {
    jest.clearAllMocks()
  })

  describe('credentials are not configured', () => {
    beforeEach(() => {
      credentials = ''
    })

    test('does not pull data', () => {
      return sync.pull().then(result => {
        expect(result).toEqual(false)
      })
    })

    test('does not initialize drive client', () => {
      return sync.pull().then(() => {
        expect(google.drive).not.toHaveBeenCalled()
      })
    })
  })

  describe('credentials are configured', () => {
    let folderExistsMock, fileExistsMock, fileContentMock
    const drive = {
      files: {
        get: jest.fn(() => fileContentMock()),
        list: jest.fn(options => {
          if (options.q === "name = 'Swifty' and trashed = false") {
            return folderExistsMock()
          } else {
            return fileExistsMock()
          }
        }),
        create: jest.fn()
      }
    }
    google.drive = jest.fn(() => drive)

    beforeEach(() => {
      fileContentMock = () => Promise.resolve({ data: 'VAULT DATA' })
      folderExistsMock = () =>
        Promise.resolve({ data: { files: [{ id: 'asdfg' }] } })
      fileExistsMock = () =>
        Promise.resolve({ data: { files: [{ id: 'qwerty' }] } })

      credentials = '{"access_token": "qwert","refresh_token": "asdf"}'
    })

    test('initializes drive client', () => {
      return sync.pull().then(() => {
        expect(google.drive).toHaveBeenCalledWith({
          version: 'v3',
          auth: sync.client.auth
        })
      })
    })

    describe('folder does not exist', () => {
      beforeEach(() => {
        folderExistsMock = () => Promise.resolve({})
      })

      test('checks for presence of swifty folder', () => {
        return sync.pull().then(() => {
          expect(drive.files.list).toHaveBeenCalledWith({
            q: "name = 'Swifty' and trashed = false",
            fields: 'files(id, name)'
          })
        })
      })

      test('does not create swifty folder', () => {
        return sync.pull().then(() => {
          expect(drive.files.create).not.toHaveBeenCalled()
        })
      })

      test('does not write data to vault', () => {
        return sync.pull().then(() => {
          expect(vault.write).not.toHaveBeenCalled()
        })
      })

      test('resolves with falsey result', () => {
        return sync.pull().then(result => expect(result).toEqual(false))
      })
    })

    describe('folder exists', () => {
      describe('file does not exist', () => {
        beforeEach(() => {
          fileExistsMock = () => Promise.resolve({})
        })

        test('checks for presence of swifty folder', () => {
          return sync.pull().then(() => {
            expect(drive.files.list).toHaveBeenCalledWith({
              q: "name = 'Swifty' and trashed = false",
              fields: 'files(id, name)'
            })
          })
        })

        test('checks for presence of vault file', () => {
          return sync.pull().then(() => {
            expect(drive.files.list).toHaveBeenCalledWith({
              q: "name = 'vault.swftx' and 'asdfg' in parents",
              fields: 'files(id, name)'
            })
          })
        })

        test('does not read vault file', () => {
          return sync.pull().then(() => {
            expect(drive.files.get).not.toHaveBeenCalled()
          })
        })
      })
      describe('file exists', () => {
        test('checks for presence of swifty folder', () => {
          return sync.pull().then(() => {
            expect(drive.files.list).toHaveBeenCalledWith({
              q: "name = 'Swifty' and trashed = false",
              fields: 'files(id, name)'
            })
          })
        })

        test('checks for presence of vault file', () => {
          return sync.pull().then(() => {
            expect(drive.files.list).toHaveBeenCalledWith({
              q: "name = 'vault.swftx' and 'asdfg' in parents",
              fields: 'files(id, name)'
            })
          })
        })

        test('reads vault file', () => {
          return sync.pull().then(() => {
            expect(drive.files.get).toHaveBeenCalledWith({
              fileId: 'qwerty',
              alt: 'media'
            })
          })
        })

        describe('invalid data in vault', () => {
          beforeEach(() => {
            fileContentMock = () => Promise.resolve({ data: null })
          })

          test('does not write data to vault', () => {
            return sync.pull().then(() => {
              expect(vault.write).not.toHaveBeenCalled()
            })
          })

          test('resolves with falsy result', () => {
            return sync.pull().then(result => expect(result).toEqual(false))
          })
        })

        describe('valid data in vault', () => {
          test('writes data to vault', () => {
            return sync.pull().then(() => {
              expect(vault.write).toHaveBeenCalledWith('VAULT DATA')
            })
          })

          test('resolves with truthy result', () => {
            return sync.pull().then(result => expect(result).toEqual(true))
          })
        })
      })
    })
  })
})