def test_paste_to_for_cut_different_sheet(self): self.user = User() self.user.save() c2 = Cell() c2.formula = 'foo' c2.formatted_value = 'fv' d2 = Cell() d2.formula = 'fv to become formula' d2.formatted_value = d2.formula c3 = Cell() d3 = Cell() c5 = Cell() c5.formula = 'a safe source cell' dest_cells = { (3, 4): c2, (3, 5): c3, (4, 4): d2, (4, 5): d3, } source_sheet = Sheet() source_sheet.owner = self.user source_sheet.save() clipboard = Clipboard() clipboard.to_cells = Mock() clipboard.to_cells.return_value = dest_cells.iteritems() clipboard.is_cut = True clipboard.source_sheet = source_sheet clipboard.source_left = 3 clipboard.source_top = 2 clipboard.source_right = 4 clipboard.source_bottom = 3 destination_sheet = Sheet() destination_sheet.owner = self.user destination_worksheet = Worksheet() destination_worksheet.C4.formula = 'i am in danger!' destination_worksheet.A1.formula = 'i am safe :-)' destination_sheet.jsonify_worksheet(destination_worksheet) destination_sheet.save() clipboard.paste_to(destination_sheet, (3, 4), (3, 4)) updated_worksheet = destination_sheet.unjsonify_worksheet() self.assertEquals(updated_worksheet.A1.formula, 'i am safe :-)') self.assertEquals(updated_worksheet.C4.formula, c2.formula) self.assertEquals(updated_worksheet.D4.formula, d2.formula) self.assertEquals(updated_worksheet.C5.formula, c3.formula) self.assertEquals(updated_worksheet.d5.formula, d3.formula) # paste should reset the clipboard so that future pastes act as # though they came from a copy self.assertEquals(clipboard.source_left, 3) self.assertEquals(clipboard.source_top, 4) self.assertEquals(clipboard.source_right, 4) self.assertEquals(clipboard.source_bottom, 5) self.assertEquals(clipboard.is_cut, False) self.assertEquals(clipboard.source_sheet, None)
def test_cut_calls_copy_then_cuts_and_remembers_some_stuff(self): clipboard = Clipboard() sheet = Sheet() user = User() user.username = '******' user.save() sheet.owner = user clipboard.copy = Mock(wraps=clipboard.copy) worksheet = Worksheet() worksheet.A1.formula = 'outside' worksheet.A2.formula = 'inside' sheet.jsonify_worksheet(worksheet) sheet.save() clipboard.cut(sheet, (1, 2), (3, 4)) self.assertCalledOnce(clipboard.copy, sheet, (1, 2), (3, 4)) worksheet = sheet.unjsonify_worksheet() self.assertEquals(worksheet.A1.formula, 'outside') self.assertEquals(worksheet.A2, Cell()) self.assertEquals(clipboard.source_left, 1) self.assertEquals(clipboard.source_top, 2) self.assertEquals(clipboard.source_right, 3) self.assertEquals(clipboard.source_bottom, 4) self.assertEquals(clipboard.is_cut, True) self.assertEquals(clipboard.source_sheet, sheet)
def test_uuid_stays_constant_between_reads(self): sheet = Sheet() user = User(username='******') user.save() sheet.owner = user sheet.save() sheet2 = Sheet.objects.get(pk=sheet.id) self.assertEquals(sheet.api_key, sheet2.api_key)
def test_delete_private_key_does(self): self.assertEquals(len(OneTimePad.objects.all()), 0) sheet = Sheet() user = User(username='******') user.save() sheet.owner = user sheet.create_private_key() self.assertEquals(len(OneTimePad.objects.all()), 1) sheet._delete_private_key() self.assertEquals(len(OneTimePad.objects.all()), 0)
def test_create_private_key_uses_onetimepad(self): sheet = Sheet() sheet.version = 155 user = User(username='******') user.set_password('glurk') user.save() sheet.owner = user self.assertEquals(len(OneTimePad.objects.all()), 0) self.assertEquals(sheet.create_private_key(), OneTimePad.objects.get(user=sheet.owner).guid) sheet._delete_private_key()
def test_create_private_key_uses_onetimepad(self): sheet = Sheet() sheet.version = 155 user = User(username='******') user.set_password('glurk') user.save() sheet.owner = user self.assertEquals(len(OneTimePad.objects.all()), 0) self.assertEquals( sheet.create_private_key(), OneTimePad.objects.get(user=sheet.owner).guid) sheet._delete_private_key()
def test_calculate_always_deletes_private_key_in_finally_block( self, mock_calculate ): def raiser(*a, **kw): raise Exception() mock_calculate.side_effect = raiser sheet = Sheet() user = User(username='******') user.save() sheet.owner = user sheet._delete_private_key = Mock() self.assertRaises(Exception, sheet.calculate) self.assertCalledOnce(sheet._delete_private_key)
def test_calculate_always_deletes_private_key_in_finally_block( self, mock_calculate): def raiser(*a, **kw): raise Exception() mock_calculate.side_effect = raiser sheet = Sheet() user = User(username='******') user.save() sheet.owner = user sheet._delete_private_key = Mock() self.assertRaises(Exception, sheet.calculate) self.assertCalledOnce(sheet._delete_private_key)
def test_run_worksheet_should_return_worksheet_with_calculated_values_only( self, mock_urllib2): self.maxDiff = None original_sheet = Worksheet() original_sheet.A2.formula = '1' original_sheet.A2.value = 1 original_sheet.C3.formula = '5' original_sheet.C3.value = 5 original_sheet.E4.formula = '=A2 + C3' original_sheet.E4.value = 6 expected_sheet = Worksheet() expected_sheet.name = 'Untitled' for (col, row), cell in original_sheet.items(): expected_sheet[col, row].value = cell.value foreign_sheet = Sheet() foreign_sheet.owner = User(username='******', password='******') foreign_sheet.owner.save() foreign_sheet.contents_json = worksheet_to_json(original_sheet) foreign_sheet.calculate() mock_opener = mock_urllib2.build_opener.return_value mock_urlopen_file = mock_opener.open.return_value mock_urlopen_file.read.return_value = _sheet_to_value_only_json( foreign_sheet.name, worksheet_from_json(foreign_sheet.contents_json)) worksheet_url = 'ws_url/' result = run_worksheet(worksheet_url, None, sentinel.private_key) target_url = '%sv%s/json/' % (worksheet_url, CURRENT_API_VERSION) self.assertCalledOnce(mock_opener.open, target_url, data=urlencode({ 'dirigible_l337_private_key': sentinel.private_key })) self.assertEquals(type(result), Worksheet) self.assertEquals(result, expected_sheet)
def test_run_worksheet_should_return_worksheet_with_calculated_values_only(self, mock_urllib2): self.maxDiff = None original_sheet = Worksheet() original_sheet.A2.formula = '1' original_sheet.A2.value = 1 original_sheet.C3.formula = '5' original_sheet.C3.value = 5 original_sheet.E4.formula = '=A2 + C3' original_sheet.E4.value = 6 expected_sheet = Worksheet() expected_sheet.name = 'Untitled' for (col, row), cell in original_sheet.items(): expected_sheet[col, row].value = cell.value foreign_sheet = Sheet() foreign_sheet.owner = User(username='******', password='******') foreign_sheet.owner.save() foreign_sheet.contents_json = worksheet_to_json(original_sheet) foreign_sheet.calculate() mock_opener = mock_urllib2.build_opener.return_value mock_urlopen_file = mock_opener.open.return_value mock_urlopen_file.read.return_value = _sheet_to_value_only_json( foreign_sheet.name, worksheet_from_json(foreign_sheet.contents_json) ) worksheet_url = 'ws_url/' result = run_worksheet(worksheet_url, None, sentinel.private_key) target_url = '%sv%s/json/' % (worksheet_url, CURRENT_API_VERSION) self.assertCalledOnce(mock_opener.open, target_url, data=urlencode({'dirigible_l337_private_key': sentinel.private_key})) self.assertEquals(type(result), Worksheet) self.assertEquals(result, expected_sheet)
def test_copy_sheet_allows_other_users_to_copy_public_sheets(self): user = User(username='******') user.save() worksheet = Worksheet() worksheet.a1.value = 'some-cell-content' sheet = Sheet() sheet.owner = user sheet.is_public = True sheet.jsonify_worksheet(worksheet) sheet.save() original_sheet_id = sheet.id other_user = User(username='******') other_user.save() retval = copy_sheet_to_user(sheet, other_user) other_user_sheets = Sheet.objects.filter(owner=other_user) self.assertEquals(len(other_user_sheets), 1) copied_sheet = other_user_sheets[0] self.assertFalse(copied_sheet.is_public) copied_worksheet = copied_sheet.unjsonify_worksheet() self.assertEquals(copied_worksheet.a1.value, 'some-cell-content') self.assertEquals(copied_sheet.id, retval.id) self.assertNotEquals(retval.id, original_sheet_id)