Writing a Sleigh Authentication in Python


The challenge

Christmas is coming and many people dreamed of having a ride with Santa’s sleigh. But, of course, only Santa himself is allowed to use this wonderful transportation. And in order to make sure, that only he can board the sleigh, there’s an authentication mechanism.

Your task is to implement the authenticate() method of the sleigh, which takes the name of the person, who wants to board the sleigh and a secret password. If, and only if, the name equals “Santa Claus” and the password is “Ho Ho Ho!” (yes, even Santa has a secret password with uppercase and lowercase letters and special characters :D), the return value must be true. Otherwise, it should return false.

Examples:

sleigh = Sleigh()
sleigh.authenticate('Santa Claus', 'Ho Ho Ho!') # must return True

sleigh.authenticate('Santa', 'Ho Ho Ho!') # must return False
sleigh.authenticate('Santa Claus', 'Ho Ho!') # must return False
sleigh.authenticate('jhoffner', 'CodeWars') # Nope, even Jake is not allowed to use the sleigh ;)

The solution in Python code

Option 1:

class Sleigh(object):
    def authenticate(self, name, password):
        return name == 'Santa Claus' and password == 'Ho Ho Ho!'

Option 2:

class Sleigh(object):
    def authenticate(self, name, password):
        return (name, password) == ('Santa Claus', 'Ho Ho Ho!')

Option 3:

class Sleigh(object):

    def __init__(self):
        self.known_credentials = {'Santa Claus': 'Ho Ho Ho!'}
        
    def authenticate(self, name, password):
        if name in self.known_credentials.keys() and \
          password == self.known_credentials.get(name):
            return True
        else:
            return False

Test cases to validate our solution

test.describe("Santa's Sleigh")

sleigh = Sleigh()
def test_credentials(name, password, expected):
    test.assert_equals(sleigh.authenticate(name, password), expected, 'Tested name %s and password %s' % (name,password))

test.it('must authenticate with correct credentials')
test_credentials('Santa Claus', 'Ho Ho Ho!', True)
    
test.it('Must not authenticate with incorrect credentials')
test_credentials('Santa', 'Ho Ho Ho!', False)
test_credentials('Santa Claus', 'Ho Ho!', False)
test_credentials('jhoffner', 'CodeWars', False)