示例#1
0
 def test_farmer_run(self):
     with mock.patch(
         'downstream_farmer.shell.restore', autospec=True) as r,\
             mock.patch(
                 'six.moves.urllib.request.urlopen') as patch:
         r.side_effect = MockRestore(
             {'historyfile': dict(), 'identityfile': dict()})
         farmer = Farmer(self.test_args)
         farmer.api = API()
     with mock.patch(
         'downstream_farmer.shell.DownstreamClient') as patch,\
             mock.patch(
                 'downstream_farmer.shell.save', autospec=True) as s:
         patch.return_value.token = 'foo'
         patch.return_value.address = 'bar'
         farmer.run()
         patch.assert_called_with(
             farmer.url, farmer.token, farmer.address, farmer.size, '', '',
             farmer.api)
         self.assertTrue(patch.return_value.connect.called)
         self.assertEqual(farmer
                          .state['nodes'][patch.return_value
                                          .server]['token'],
                          patch.return_value.token)
         self.assertEqual(farmer
                          .state['nodes'][patch.return_value
                                          .server]['address'],
                          patch.return_value.address)
         self.assertTrue(s.called)
         patch.return_value.run.assert_called_with(farmer.number)
示例#2
0
 def test_farmer_run_reconnect(self):
     with mock.patch(
         'downstream_farmer.shell.restore', autospec=True) as r,\
             mock.patch(
                 'six.moves.urllib.request.urlopen') as patch:
         r.side_effect = MockRestore(
             {'historyfile': dict(), 'identityfile': dict()})
         farmer = Farmer(self.test_args)
     with mock.patch('downstream_farmer.shell.DownstreamClient') as patch,\
             mock.patch('downstream_farmer.shell.save', autospec=True),\
             mock.patch('time.sleep') as t:
         patch.return_value.run.side_effect = MockRaiseOnFirstCall(
             DownstreamError('first error'))
         farmer.run(reconnect=True)
         t.assert_called_with(10)
示例#3
0
 def test_farmer_run_no_reconnect(self):
     with mock.patch(
         'downstream_farmer.shell.restore', autospec=True) as r,\
             mock.patch(
                 'six.moves.urllib.request.urlopen') as patch:
         r.side_effect = MockRestore(
             {'historyfile': dict(), 'identityfile': dict()})
         farmer = Farmer(self.test_args)
     with mock.patch(
         'downstream_farmer.shell.DownstreamClient') as patch,\
             mock.patch(
                 'downstream_farmer.shell.save', autospec=True):
         patch.return_value.run.side_effect = MockRaiseOnFirstCall(
             DownstreamError('first error'))
         with self.assertRaises(DownstreamError) as ex:
             farmer.run()
         self.assertEqual(str(ex.exception), 'first error')
示例#4
0
 def test_farmer_init_address(self):
     with mock.patch(
         'downstream_farmer.shell.restore', autospec=True) as r,\
             mock.patch.object(Farmer, 'check_connectivity'):
         r.side_effect = MockRestore(
             {'historyfile': dict(), 'identityfile': dict()})
         farmer = Farmer(self.test_args)
         self.assertEqual(farmer.address, self.test_args.address)
示例#5
0
 def test_farmer_init_url_default(self):
     self.test_args.node_url = None
     with mock.patch(
         'downstream_farmer.shell.restore', autospec=True) as r,\
             mock.patch.object(Farmer, 'check_connectivity'):
         r.side_effect = MockRestore(
             {'historyfile': dict(), 'identityfile': dict()})
         farmer = Farmer(self.test_args)
         self.assertEqual(farmer.url, 'https://live.driveshare.org:8443')
示例#6
0
 def test_farmer_init_forcenew(self):
     self.test_args.forcenew = True
     with mock.patch(
         'downstream_farmer.shell.restore', autospec=True) as r,\
             mock.patch.object(Farmer, 'check_connectivity'):
         r.side_effect = MockRestore(
             {'historyfile': dict(), 'identityfile': dict()})
         farmer = Farmer(self.test_args)
     self.assertIsNone(farmer.token)
示例#7
0
    def test_farmer_save_restore_state(self):
        d = {'key': 'value'}
        path = 'test_file'
        with mock.patch.object(Farmer, '__init__', autospec=True) as i:
            i.return_value = None
            farmer = Farmer(None)
        farmer.path = path
        farmer.state = d
        save(farmer.path, farmer.state)
        farmer.state = restore(farmer.path)
        self.assertEqual(d, farmer.state)
        os.remove(path)

        # test path doesn't exist
        farmer.path = 'nonexistentpath'
        farmer.state = restore(farmer.path)
        self.assertEqual(farmer.state, dict())

        # test directory creation
        dir = 'testdir'
        path = os.path.join(dir, 'file')
        farmer.path = path
        farmer.state = d
        save(farmer.path, farmer.state)
        self.assertTrue(os.path.isdir(dir))
        self.assertTrue(os.path.exists(path))
        farmer.state = restore(farmer.path)
        self.assertEqual(d, farmer.state)
        os.remove(path)
        os.rmdir(dir)

        # test parse fail
        path = 'test_file'
        with open(path, 'w') as f:
            f.write('test contents')
        with mock.patch('json.loads') as l:
            l.side_effect = Exception('test error')
            with self.assertRaises(DownstreamError) as ex:
                restore(path)
            self.assertEqual(
                str(ex.exception), 'Couldn\'t parse \'{0}\': test error'
                .format(path))
        os.remove(path)
示例#8
0
 def test_farmer_init_url_from_state(self):
     self.test_args.node_url = None
     with mock.patch(
         'downstream_farmer.shell.restore', autospec=True) as r,\
             mock.patch.object(Farmer, 'check_connectivity'):
         r.side_effect = MockRestore(
             {'historyfile': {'last_node': 'stateurl'},
              'identityfile': dict()})
         farmer = Farmer(self.test_args)
         self.assertEqual(farmer.url, 'stateurl')
示例#9
0
 def test_farmer_init_no_token_no_address(self):
     self.test_args.token = None
     self.test_args.address = None
     with mock.patch(
         'downstream_farmer.shell.restore', autospec=True) as r,\
             mock.patch.object(Farmer, 'check_connectivity'),\
             self.assertRaises(DownstreamError) as ex:
         r.side_effect = MockRestore(
             {'historyfile': dict(), 'identityfile': dict()})
         Farmer(self.test_args)
     self.assertEqual(
         str(ex.exception),
         'Must specify farming address if one is not available.')
示例#10
0
 def test_farmer_load_signature_none(self):
     with mock.patch(
         'downstream_farmer.shell.restore', autospec=True) as r,\
             mock.patch.object(
                 Farmer, 'check_connectivity'):
         r.side_effect = MockRestore({'historyfile': dict(),
                                      'identityfile':
                                      {'identityaddress':
                                       {'signature': 'testsig', 'message':
                                        'testmessage'}}})
         farmer = Farmer(self.test_args)
         self.assertEqual(farmer.message, '')
         self.assertEqual(farmer.signature, '')
示例#11
0
 def test_farmer_init_address_from_state(self):
     self.test_args.address = None
     with mock.patch(
         'downstream_farmer.shell.restore', autospec=True) as r,\
             mock.patch.object(Farmer, 'check_connectivity'):
         r.side_effect = MockRestore({'historyfile': {
             'nodes': {
                 self.test_args.node_url.strip('/'): {
                     'token': 'statetoken',
                     'address': 'stateaddress'
                 }
             }
         }, 'identityfile': dict()})
         farmer = Farmer(self.test_args)
         self.assertEqual(farmer.address, 'stateaddress')
示例#12
0
 def test_farmer_init_address_from_identities(self):
     self.test_args.address = None
     with mock.patch(
         'downstream_farmer.shell.restore', autospec=True) as r,\
             mock.patch.object(Farmer, 'check_connectivity'):
         r.side_effect = MockRestore({'historyfile': dict(),
                                      'identityfile':
                                      {'19qVgG8C6eXwKMMyvVegsi3xCsKyk3Z3jV':
                                       {'signature': 'HyzVUenXXo4pa+kgm1v'
                                        'S8PNJM83eIXFC5r0q86FGbqFcdla6rcw'
                                        '72/ciXiEPfjli3ENfwWuESHhv6K9esI0'
                                        'dl5I=', 'message':
                                        'test message'}}})
         farmer = Farmer(self.test_args)
         self.assertEqual(
             farmer.address, '19qVgG8C6eXwKMMyvVegsi3xCsKyk3Z3jV')
示例#13
0
 def test_farmer_load_signature_invalid_sig(self):
     self.test_args.token = None
     self.test_args.address = None
     with mock.patch(
         'downstream_farmer.shell.restore', autospec=True) as r,\
             mock.patch.object(Farmer, 'check_connectivity'),\
             mock.patch(
                 'siggy.verify_signature') as s:
         s.return_value = False
         r.side_effect = MockRestore({'historyfile': dict(),
                                      'identityfile':
                                      {'identityaddress':
                                       {'signature': 'testsig', 'message':
                                        'testmessage'}}})
         with self.assertRaises(DownstreamError) as ex:
             Farmer(self.test_args)
         self.assertEqual(str(ex.exception), 'Signature provided does not'
                          ' match address being used. '
                          'Check your formatting, your SJCX address, and'
                          ' try again.')
示例#14
0
    def test_farmer_check_connectivity(self):
        with mock.patch(
            'downstream_farmer.shell.restore', autospec=True) as r,\
                mock.patch(
                    'six.moves.urllib.request.urlopen') as patch:
            r.side_effect = MockRestore(
                {'historyfile': dict(), 'identityfile': dict()})
            farmer = Farmer(self.test_args)
            patch.side_effect = URLError('Problem')
            with self.assertRaises(DownstreamError):
                farmer.check_connectivity()

        with mock.patch('six.moves.urllib.request.urlopen') as patch:
            farmer.check_connectivity()
            self.assertTrue(patch.called)
示例#15
0
 def test_farmer_load_signature_invalid_dict(self):
     self.test_args.token = None
     self.test_args.address = None
     with mock.patch(
         'downstream_farmer.shell.restore', autospec=True) as r,\
             mock.patch.object(Farmer, 'check_connectivity'):
         r.side_effect = MockRestore({'historyfile': dict(),
                                      'identityfile':
                                      {'identityaddress': {'invalid':
                                                           'dict'}}})
         with self.assertRaises(DownstreamError) as ex:
             Farmer(self.test_args)
         self.assertEqual(str(ex.exception),
                          'The file format for the identity file '
                          '{0} should be a JSON formatted dictionary like '
                          'the following:\n'
                          '   {{\n'
                          '      "your sjcx address": {{\n'
                          '         "message": "your message here",\n'
                          '         "signature":  "base64 signature from '
                          'bitcoin wallet or counterparty",\n'
                          '      }}\n'
                          '   }}'.format(self.test_args.identity))
示例#16
0
 def test_farmer_init_number_invalid(self):
     self.test_args.number = -1
     with self.assertRaises(DownstreamError) as ex:
         Farmer(self.test_args)
     self.assertEqual(
         str(ex.exception), 'Must specify a positive number of challenges.')
示例#17
0
 def test_farmer_init_size_invalid(self):
     self.test_args.size = 0
     with self.assertRaises(DownstreamError) as ex:
         Farmer(self.test_args)
     self.assertEqual(
         str(ex.exception), 'Must specify a positive size to farm.')