# Copyright (c) 2015 "Hugo Herter http://hugoherter.com"
#
# This file is part of Billabong.
#
# Intercom is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Tests encryption functions."""

from billabong.settings import stores
from billabong.encryption import random_key, copy_and_encrypt, decrypt_blob

from .fixtures import record
assert record

store = stores[0]


def test_random_key():
    """Test generating a random key."""
    key = random_key()
    assert len(key) == 32
    assert type(key) is bytes


def test_copy_and_encrypt(record):
    """Test file encryption."""
    blob_id_ = copy_and_encrypt(store, 'hello.txt', key=b'0'*32)
    assert blob_id_ == record['blob']


def test_decrypt_blob(record):
    """Test file decryption."""
    blob_id = record['blob']
    g = decrypt_blob(store, blob_id, b'0'*32)
    data = b''.join(g)
    assert data == b'Hello world !\n\n'


def test_decrypt_offset():
    """Test partial file decryption."""
    offset = 340
    length = 69
    expected = open('lorem.txt', 'rb').read()[offset:offset+length]
    assert expected == (b"Qui animated corpse, cricket bat max brucks "
                        b"terribilem incessu zomby.")

    id_ = copy_and_encrypt(store, 'lorem.txt', key=b'0'*32)

    g = decrypt_blob(store, id_, b'0'*32, offset=offset, length=length)

    data = b''.join(g)
    assert data == expected

@@ -916,16 +916,27 @@
 t record
+ as record_
 %0Aassert 
@@ -945,15 +945,16 @@
 cord
+_
 %0A%0A
-store
+STORE
  = s
@@ -1091,26 +1091,30 @@
 ert 
-typ
+isinstanc
 e(key
-) is
+,
  bytes
+)
 %0A%0A%0Ad
@@ -1211,21 +1211,21 @@
 encrypt(
-store
+STORE
 , 'hello
@@ -1377,24 +1377,26 @@
 blob'%5D%0A    g
+en
  = decrypt_b
@@ -1399,21 +1399,21 @@
 pt_blob(
-store
+STORE
 , blob_i
@@ -1437,32 +1437,34 @@
 ata = b''.join(g
+en
 )%0A    assert dat
@@ -1813,21 +1813,21 @@
 encrypt(
-store
+STORE
 , 'lorem
@@ -1852,16 +1852,18 @@
 )%0A%0A    g
+en
  = decry
@@ -1870,21 +1870,21 @@
 pt_blob(
-store
+STORE
 , id_, b
@@ -1943,16 +1943,18 @@
 '.join(g
+en
 )%0A    as

Fix pylint in test_encryption