Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 212 lines (172 sloc) 7.434 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
import string
import random
import os
import sys
import nose.tools
import boto
from boto.ec2.connection import EC2Connection
import boto.ec2
import sys
from ConfigParser import SafeConfigParser
import time
import unittest
import tempfile
import filecmp
import pycb
import pynimbusauthz
from pynimbusauthz.db import *
from pynimbusauthz.user import *
import pycb.test_common
from boto.s3.connection import OrdinaryCallingFormat
from boto.s3.connection import S3Connection
import random
import nimbus_remove_user
import nimbus_new_user
import nimbus_list_users
import nimbus_edit_user

class TestUsers(unittest.TestCase):

    def setUp(self):
        self.users = []

    def tearDown(self):
        for f in self.users:
            nimbus_remove_user.main([f])

    def get_user_name(self, friendly_name=None):
        if friendly_name == None:
            friendly_name = str(uuid.uuid1())
        self.users.append(friendly_name)
        return friendly_name

    def test_make_remove_user(self):
        friendly_name = self.get_user_name()
        rc = nimbus_new_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))
        rc = nimbus_remove_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))

    def test_make_user_twice(self):
        friendly_name = self.get_user_name()
        rc = nimbus_new_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))
        rc = nimbus_new_user.main([friendly_name])
        self.assertNotEqual(rc, 0, "should be 0 %d" % (rc))

        rc = nimbus_remove_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))

    def test_remove_user_twice(self):
        friendly_name = self.get_user_name()
        rc = nimbus_new_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))

        rc = nimbus_remove_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))
        rc = nimbus_remove_user.main([friendly_name])
        self.assertNotEqual(rc, 0, "should not be 0 %d" % (rc))

    def test_add_remove_add_remove(self):
        friendly_name = self.get_user_name()
        rc = nimbus_new_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))
        rc = nimbus_remove_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))

        rc = nimbus_new_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))
        rc = nimbus_remove_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))

    def test_remove_unknown_user(self):
        friendly_name = self.get_user_name()
        rc = nimbus_remove_user.main([friendly_name])
        self.assertNotEqual(rc, 0, "should be 0 %d" % (rc))

    def find_in_file(self, fname, needle):
        found = False
        f = open(fname)
        l = f.readline()
        while l:
            print "#### " + l
            x = l.find(needle)
            if x >= 0:
                found = True
            l = f.readline()
        f.close()
        return found

    def test_new_user_s3ids(self):
        friendly_name = self.get_user_name()

        (tmpFD, outFileName) = tempfile.mkstemp("cumulustests")
        os.close(tmpFD)

        s3id = str(uuid.uuid1())
        s3pw = str(uuid.uuid1())
        rc = nimbus_new_user.main(["-a", s3id, "-p", s3pw, "-b", "-r", "access_id,access_secret", "-O", outFileName, friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))
        needle = "%s,%s" % (s3id, s3pw)
        print needle
        rc = self.find_in_file(outFileName, needle)
        os.unlink(outFileName)
        self.assertTrue(rc)

        rc = nimbus_remove_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))

    def test_no_cert(self):
        friendly_name = self.get_user_name()

        (tmpFD, outFileName) = tempfile.mkstemp("cumulustests")
        os.close(tmpFD)

        s3id = str(uuid.uuid1())
        s3pw = str(uuid.uuid1())
        rc = nimbus_new_user.main(["--noaccess", "-b", "-r", "access_id,access_secret", "-O", outFileName, friendly_name])
        needle = "None,None"
        rc = self.find_in_file(outFileName, needle)
        os.unlink(outFileName)
        self.assertTrue(rc)
        
        rc = nimbus_remove_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))

    def test_no_s3(self):
        friendly_name = self.get_user_name()

        (tmpFD, outFileName) = tempfile.mkstemp("cumulustests")
        os.close(tmpFD)

        s3id = str(uuid.uuid1())
        s3pw = str(uuid.uuid1())
        rc = nimbus_new_user.main(["--nocert", "-b", "-r", "cert,key,dn", "-O", outFileName, friendly_name])
        needle = "None,None,None"
        rc = self.find_in_file(outFileName, needle)
        os.unlink(outFileName)
        self.assertTrue(rc)
        
        rc = nimbus_remove_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))

    def test_edit_user(self):
        friendly_name = self.get_user_name()

        (tmpFD, outFileName) = tempfile.mkstemp("cumulustests")
        os.close(tmpFD)

        s3id = str(uuid.uuid1())
        s3pw = str(uuid.uuid1())
        rc = nimbus_new_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))
        rc = nimbus_edit_user.main(["-b", "-a", s3id, "-p", s3pw, "-r", "access_id,access_secret", "-O", outFileName, friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))
        needle = "%s,%s" % (s3id, s3pw)
        print "--> %s <--" % (needle)
        rc = self.find_in_file(outFileName, needle)
        os.unlink(outFileName)
        self.assertTrue(rc)
        
        rc = nimbus_remove_user.main([friendly_name])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))

    def test_list_user(self):
        name1 = self.get_user_name()
        rc = nimbus_new_user.main([name1])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))

        name2 = self.get_user_name()
        rc = nimbus_new_user.main([name2])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))

        name3 = self.get_user_name()
        rc = nimbus_new_user.main([name3])
        self.assertEqual(rc, 0, "should be 0 %d" % (rc))

        (tmpFD, outFileName) = tempfile.mkstemp("cumulustests")
        os.close(tmpFD)
        rc = nimbus_list_users.main(["-b", "-r", "display_name", "-O", outFileName, '%'])
        rc = self.find_in_file(outFileName, name1)
        self.assertTrue(rc)

        rc = self.find_in_file(outFileName, name2)
        self.assertTrue(rc)

        rc = self.find_in_file(outFileName, name3)
        self.assertTrue(rc)

        os.unlink(outFileName)

    def test_db_commit_user(self):
        # insert a new user with an error
        friendly_name = self.get_user_name(friendly_name="test1@nimbus.test")
        rc = nimbus_new_user.main(["--cert", "none", "--key", "none", friendly_name])
        self.assertNotEqual(rc, 0, "we expect this one to fail %d" % (rc))

        # insert the user without the error to make sure the previous was rolled back
        friendly_name = self.get_user_name(friendly_name="test1@nimbus.test")
        rc = nimbus_new_user.main([friendly_name])
        self.assertEqual(rc, 0, "but then this clarification should succeed %d" % (rc))

Something went wrong with that request. Please try again.