blob: 1deb66f4e1d328cd99389ecd66c9a072a742bb92 [file] [log] [blame]
# Copyright 2014 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for contact logic."""
import datetime
import unittest
from google.appengine.ext import ndb
from melange.logic import signature as signature_logic
from melange.models import signature as signature_model
from seeder import profile as profile_seeder
from seeder import document as document_seeder
from seeder import program as program_seeder
class GetSignatureForProfileAndDocumentTest(unittest.TestCase):
"""Unit tests for getSignatureForProfileAndDocument function."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
self.program = program_seeder.seedProgram()
self.profile = profile_seeder.seedProfile(self.program.key())
self.document = document_seeder.seedDocument(self.program)
self.signature = signature_model.Signature(
profile=self.profile.key,
legal_name=self.profile.legal_name,
email=self.profile.contact.email,
address=self.profile.residential_address,
signed_on=datetime.datetime.utcnow(),
document=ndb.Key.from_old_key(self.document.key()))
self.signature.put()
def testSignatureExists(self):
"""Tests that signature is returned correctly if exists."""
signature = signature_logic.getSignatureForProfileAndDocument(
self.profile.key, ndb.Key.from_old_key(self.document.key()))
self.assertIsNotNone(signature)
self.assertEqual(signature.key, self.signature.key)
def testSignatureDoesNotExist(self):
"""Tests that signature is not returned if does not exist."""
# check for another profile for which signature does not exist
other_profile = profile_seeder.seedProfile(self.program.key())
signature = signature_logic.getSignatureForProfileAndDocument(
other_profile.key, ndb.Key.from_old_key(self.document.key()))
self.assertIsNone(signature)
other_document = document_seeder.seedDocument(self.program)
signature = signature_logic.getSignatureForProfileAndDocument(
self.profile.key, ndb.Key.from_old_key(other_document.key()))
class CreateSignatureTest(unittest.TestCase):
"""Unit tests for createSignature function."""
def testSignatureCreated(self):
"""Tests that signature is created correctly."""
program = program_seeder.seedProgram()
document_key = ndb.Key.from_old_key(
document_seeder.seedDocument(program).key())
profile = profile_seeder.seedProfile(program.key())
signature = signature_logic.createSignature(document_key, profile)
# check that the signature is created correctly
self.assertIsNotNone(signature)
self.assertEqual(document_key, signature.document)
self.assertEqual(profile.key, signature.profile)
self.assertEqual(profile.legal_name, signature.legal_name)
self.assertEqual(profile.contact.email, signature.email)
self.assertEqual(profile.residential_address, signature.address)
# check that the function to get signature returns is as well
key_from_db = signature_logic.getSignatureForProfileAndDocument(
profile.key, document_key).key
self.assertEqual(signature.key, key_from_db)