def test_login(self): clerk = MockClerk(SCHEMA) clerk.store(Author(username='******', password='******')) sess = Sess(InMemorySessPool(), RequestBuilder().build(), Response()) aa = AuthorAuth(sess, clerk) assert aa.fetch(1).username == 'fred' aa.login(1) assert aa.user.username == 'fred' assert aa.validate({"username": "******", "password": "******"}) assert not aa.validate({"username": "******", "password": "******"})
class SiteTest(unittest.TestCase): def setUp(self): self.clerk = MockClerk(schema) def test_aliases(self): d = Domain(domain="test.com") s = Site(domain=d) d.site = s a = Domain(domain="alias.com") s.aliases << a self.clerk.store(s) self.clerk.store(d) s = self.clerk.fetch(Site, ID=1) assert [a] == s.aliases
class FrontEndAppTest(unittest.TestCase): def setUp(self): self.clerk = MockClerk(schema) os.chdir("frontend") self.app = duckbill.FrontEndApp(self.clerk, {}) self.app.debug = 1 def test_createAccount(self): # test to expose a bug... all this SHOULD do is show a form.. #@TODO: will need to clean this up once weblib refactoring is done REQ = weblib.RequestBuilder().build( method="GET", querystring="action=create&what=account", form = {}, cookie = {}, content = None) app = duckbill.FrontEndApp(self.clerk, REQ) app.act() def test_closeAccount(self): acc = duckbill.Account(account="ftempy") acc.subscriptions << duckbill.Subscription() acc = self.clerk.store(acc) accID = acc.ID assert acc.status=='active' REQ = weblib.RequestBuilder().build( method="POST", querystring="", form = {'action':'close_account', 'reason':'xxx', 'ID':str(accID)}, cookie = {}, content = None) app = duckbill.FrontEndApp(self.clerk, REQ) self.assertRaises(weblib.Redirect, app.act) self.assertEquals('closed',acc.status, 'dig nibbit?') all = self.clerk.match(duckbill.Account) assert len(all) == 1, all acc = all[0] self.assertEquals('closed',acc.status, 'dag nabbit!') self.assertEquals('closed',acc.subscriptions[0].status) def tearDown(self): os.chdir("..")
class FrontEndAppTest(unittest.TestCase): def setUp(self): self.clerk = MockClerk(schema) os.chdir("frontend") self.app = duckbill.FrontEndApp(self.clerk, {}) self.app.debug = 1 def test_createAccount(self): # test to expose a bug... all this SHOULD do is show a form.. #@TODO: will need to clean this up once weblib refactoring is done REQ = weblib.RequestBuilder().build( method="GET", querystring="action=create&what=account", form={}, cookie={}, content=None) app = duckbill.FrontEndApp(self.clerk, REQ) app.act() def test_closeAccount(self): acc = duckbill.Account(account="ftempy") acc.subscriptions << duckbill.Subscription() acc = self.clerk.store(acc) accID = acc.ID assert acc.status == 'active' REQ = weblib.RequestBuilder().build(method="POST", querystring="", form={ 'action': 'close_account', 'reason': 'xxx', 'ID': str(accID) }, cookie={}, content=None) app = duckbill.FrontEndApp(self.clerk, REQ) self.assertRaises(weblib.Redirect, app.act) self.assertEquals('closed', acc.status, 'dig nibbit?') all = self.clerk.match(duckbill.Account) assert len(all) == 1, all acc = all[0] self.assertEquals('closed', acc.status, 'dag nabbit!') self.assertEquals('closed', acc.subscriptions[0].status) def tearDown(self): os.chdir("..")
def test_basics(self): c = MockClerk(SCHEMA) idx = StoryIndex(c, ":memory:") s1 = c.store(Story(title="onedog", description="a big dog")) s2 = c.store(Story(title="twodog", description="dog eat dog")) s3 = c.store(Story(title="onecat", description="a fat cat")) idx.addStory(s1) idx.addStory(s2) idx.addStory(s3) dogs = idx.match("dog") cats = idx.match("cat") assert len(dogs) == 2, dogs assert len(cats) == 1, cats # and check scoring: assert dogs[0].title == "twodog" assert dogs[1].title == "onedog" blank = c.store(Story(title="", description="")) idx.addStory(blank)
class NodeTest(unittest.TestCase): def setUp(self): self.clerk = MockClerk( Schema({ Node: "Node", Node.parent: "parentID", Node.children: "parentID", })) s = self.clerk.storage # we use the storage object so we can define the # database without dealing with read-only attribures (path) s.store("Node", name='top', path='top', parentID=0) s.store("Node", name='sub', path='top/sub', parentID=1) s.store("Node", name='subsub', path='top/sub/sub', parentID=2) #import pdb; pdb.set_trace() def test_crumbs(self): node = self.clerk.fetch(Node, 1) assert node.crumbs == [], \ "Didn't get right crumbs for node 1." node = self.clerk.fetch(Node, ID=3) goal = [{ "ID": 1, "name": "top", "path": "top" }, { "ID": 2, "name": "sub", "path": "top/sub" }] assert len(node.crumbs) == len(goal), \ "Didn't get right crumbs for node 3." def test_path(self): node = self.clerk.fetch(Node, 2) node.name = "subnode" node = self.clerk.store(node) assert node.path == "top/subnode", \ "Node has wrong path after name change: %s" % node.path def test_parent(self): node = self.clerk.fetch(Node, 2) assert isinstance(node.parent, Node), \ ".parent doesn't return a Node"
class NodeTest(unittest.TestCase): def setUp(self): self.clerk = MockClerk(Schema({ Node: "Node", Node.parent: "parentID", Node.children: "parentID", })) s = self.clerk.storage # we use the storage object so we can define the # database without dealing with read-only attribures (path) s.store("Node", name='top', path='top', parentID=0) s.store("Node", name='sub', path='top/sub', parentID=1) s.store("Node", name='subsub', path='top/sub/sub', parentID=2) #import pdb; pdb.set_trace() def test_crumbs(self): node = self.clerk.fetch(Node, 1) assert node.crumbs == [], \ "Didn't get right crumbs for node 1." node = self.clerk.fetch(Node, ID=3) goal = [{"ID": 1, "name": "top", "path": "top"}, {"ID": 2, "name": "sub", "path": "top/sub"}] assert len(node.crumbs) == len(goal), \ "Didn't get right crumbs for node 3." def test_path(self): node = self.clerk.fetch(Node, 2) node.name="subnode" node = self.clerk.store(node) assert node.path == "top/subnode", \ "Node has wrong path after name change: %s" % node.path def test_parent(self): node = self.clerk.fetch(Node, 2) assert isinstance(node.parent, Node), \ ".parent doesn't return a Node"
class NewSiteGruntTest(unittest.TestCase): def setUp(self): self.clerk = MockClerk(Schema(cornerhost.config.DBMAP)) self.user = self.clerk.store( User(username="******", server=Server(name="mockium"))) def makeGrunt(self): return NewSiteGrunt(self.clerk, self.user) def test_storage(self): g = self.makeGrunt() assert g.user.username == "ftempy" assert g.user.ID == 1 g.addDomain("ftempy.com") d = self.clerk.fetch(Domain, 1) assert d.domain == "ftempy.com", "Domain record didn't get saved!" assert d.user.ID == 1, "didn't store domain.user.ID: %s" % d.user.ID assert d.ID == 1 assert d.site is None self.makeGrunt().addSite("ftempy.com") sites = self.clerk.match(Site) assert len(sites) == 1, "expectedd 1 site, got %s" % len(sites) s = sites[0] assert s.domain.ID == 1, "wrong domainID: %s" % s.domain.ID assert s.user.ID == 1, "didn't store site.user.ID: %s" % s.user.ID assert s.ID == 1 d = self.clerk.fetch(Domain, 1) assert d.site.ID == 1, "didn't update domain.site.ID" def test_makeDirs(self): "it should just pass the call the the appropriate beaker" MockBeaker.log = [] mk = MockBeaker() self.makeGrunt().makeDirs("ftempy.com") assert mk.log == ["makeSiteDirs('ftempy','ftempy.com') "], \ "didn't relay makesite request to beaker!: %s" % mk.log def test_checkDomain(self): c = self.clerk def fails(d): try: self.makeGrunt().checkDomain(d) except ValueError: return True # invalid domains should fail: assert fails("sabren..com") # should fail if domain in the system: assert not fails("ftempy.com") u = c.fetch(User, 1) u.domains << Domain(domain="ftempy.com") c.store(u) assert fails("ftempy.com") # www indicates subdomains, which we don't allow yet! assert fails("www.wtempy.com") # another subdomain check: assert not fails("sub.rufustempy.com") assert fails("sub.ftempy.com") # finally, disallow *.sabren.com wildcard sudomains: assert fails("ftempy.sabren.com") assert fails("ftempy.ab.sabren.com") def test_addSub(self): grunt = self.makeGrunt() try: grunt.addSub("sub", "domain.com") gotError = 0 except ValueError: gotError = 1 assert gotError, "should have failed! - domain.com is unknown" # but if we add it: grunt.addDomain("domain.com") # have to reload ftempy though: grunt = self.makeGrunt() grunt.addSub("sub", "domain.com") # now make sure it got saved: d = self.clerk.match(Domain, domain="sub.domain.com")[0] assert d.rectype == "cname" assert d.location == "domain.com" assert d.zone.domain == "domain.com" assert d.user.ID == 1 assert not d.processmail # and make sure we can't add subs to subs # (instead just make a new subdomain with a dot in it) try: grunt = self.makeGrunt() grunt.addSub("sub", "sub.domain.com") except: pass else: self.fail("should fail when nesting subdomains")
class AdminAppTest(unittest.TestCase): def setUp(self): self.clerk=MockClerk(schema) self.sess = {"username":"******"} self.app = AdminApp(self.clerk, self.sess) def test_UpdateUserCommand(self): fred = self.clerk.store(User(username="******")) script = self.clerk.store(Plan(name="script")) cmd = admin.UpdateUserCommand() req = { 'status':'locked', 'plan':'script', 'diskextra':'10', 'bandextra':'20', 'boxextra':'0', 'dbextra':'0', '_clerk': self.clerk, '_sess': self.sess, } self.assertRaises(Redirect, cmd.invoke, **req) assert fred.status == req['status'] assert fred.plan.name == req['plan'] assert fred.diskextra == int(req['diskextra']) assert fred.bandextra == int(req['bandextra']) def test_IsPasswdCommand(self): u = self.clerk.store(User(username="******")) class MockBeaker: def ispasswd(self, username, password): return True class MockUser(User): def ispasswd(self, pwd): return pwd=="good" def mockLoadUser(u,c): return MockUser() cmd = admin.IsPasswdCommand() cmd.loadUser = mockLoadUser model = cmd.invoke(self.clerk, u, password="******") assert model["message"]=="yes" model = cmd.invoke(self.clerk, u, password="******") assert model["message"]=="no" def test_JumpCommand(self): wanda = User(username="******") wanda.domains << Domain(domain="wtempy.com") self.clerk.store(wanda) invoke = lambda jumpto: ( cmd.invoke(_sess=self.sess, _clerk=self.clerk, jumpto=jumpto)) # searching for wanda should succeed: cmd = admin.JumpCommand() self.assertRaises(Redirect, invoke, jumpto="wanda") assert self.sess["username"] == "wanda" # but since there's no one else, another search should fail: self.assertRaises(AssertionError, invoke, jumpto="invalid") # you should also be able to look her up by domain: del self.sess["username"] self.assertRaises(Redirect, invoke, jumpto="wtempy.com") assert self.sess["username"] == "wanda" def test_ReviewSignup(self): newbie = Signup(username="******") self.clerk.store(newbie) cmd = admin.ReviewSignup() model = cmd.invoke(self.clerk, ID=newbie.ID) assert model.signup.username=="newbie"
class AdminAppTest(unittest.TestCase): def setUp(self): self.clerk = MockClerk(schema) self.sess = {"username": "******"} self.app = AdminApp(self.clerk, self.sess) def test_UpdateUserCommand(self): fred = self.clerk.store(User(username="******")) script = self.clerk.store(Plan(name="script")) cmd = admin.UpdateUserCommand() req = { 'status': 'locked', 'plan': 'script', 'diskextra': '10', 'bandextra': '20', 'boxextra': '0', 'dbextra': '0', '_clerk': self.clerk, '_sess': self.sess, } self.assertRaises(Redirect, cmd.invoke, **req) assert fred.status == req['status'] assert fred.plan.name == req['plan'] assert fred.diskextra == int(req['diskextra']) assert fred.bandextra == int(req['bandextra']) def test_IsPasswdCommand(self): u = self.clerk.store(User(username="******")) class MockBeaker: def ispasswd(self, username, password): return True class MockUser(User): def ispasswd(self, pwd): return pwd == "good" def mockLoadUser(u, c): return MockUser() cmd = admin.IsPasswdCommand() cmd.loadUser = mockLoadUser model = cmd.invoke(self.clerk, u, password="******") assert model["message"] == "yes" model = cmd.invoke(self.clerk, u, password="******") assert model["message"] == "no" def test_JumpCommand(self): wanda = User(username="******") wanda.domains << Domain(domain="wtempy.com") self.clerk.store(wanda) invoke = lambda jumpto: (cmd.invoke( _sess=self.sess, _clerk=self.clerk, jumpto=jumpto)) # searching for wanda should succeed: cmd = admin.JumpCommand() self.assertRaises(Redirect, invoke, jumpto="wanda") assert self.sess["username"] == "wanda" # but since there's no one else, another search should fail: self.assertRaises(AssertionError, invoke, jumpto="invalid") # you should also be able to look her up by domain: del self.sess["username"] self.assertRaises(Redirect, invoke, jumpto="wtempy.com") assert self.sess["username"] == "wanda" def test_ReviewSignup(self): newbie = Signup(username="******") self.clerk.store(newbie) cmd = admin.ReviewSignup() model = cmd.invoke(self.clerk, ID=newbie.ID) assert model.signup.username == "newbie"
class BandwidthGruntTest(unittest.TestCase): def setUp(self): self.clerk = MockClerk(Schema(cornerhost.config.DBMAP)) self.rate = Decimal("1.23") # dollars per gig... :) self.bwg = BandwidthGrunt(self.clerk, self.rate) def test_findDueAccounts(self): # one due today, one tomorrow, one today but versionhost a = self.clerk.store(Account(account='a', nextDue=TODAY)) b = self.clerk.store(Account(account='b', nextDue=TODAY + 1)) c = self.clerk.store( Account(account='c', nextDue=TODAY, brand="versionhost")) due = self.bwg.findDueAccounts() assert due == [a] def test_findUsersForAccount(self): a = Account() u1 = self.clerk.store(User(account=a)) u2 = self.clerk.store(User(account=Account())) u3 = self.clerk.store(User(account=a)) users = self.bwg.findUsersForAccount(a) assert users == [u1, u3] def test_monthlyTraffic(self): arbdate = Date("2/29/2004") startdate = Date("1/29/2004") user = User() # this one is out of range: user.usage << Usage(whichday=startdate - 1, traffic=1 * GIGA) # this one is cool: user.usage << Usage(whichday=startdate, traffic=2 * GIGA) # these are good: user.usage << Usage(whichday=arbdate - 3, traffic=4 * GIGA) user.usage << Usage(whichday=arbdate - 2, traffic=8 * GIGA) user.usage << Usage(whichday=arbdate - 1, traffic=16 * GIGA) # this one is in the "future", since it # won't be recorded until "tonight": user.usage << Usage(whichday=arbdate, traffic=32 * GIGA) self.assertEquals(self.bwg.monthlyTraffic(user, arbdate), (2 + 4 + 8 + 16) * GIGA) def test_calcOverage(self): user = MockUser(bandused=3 * GIGA, bandquota=1 * GIGA) assert self.bwg.calcOverage(user, TODAY) == 2 * GIGA user = MockUser(bandused=2 * GIGA, bandquota=5 * GIGA) assert self.bwg.calcOverage(user, TODAY) == 0 def test_calcCharge(self): # overage: user = MockUser(bandused=3 * GIGA, bandquota=1 * GIGA) self.assertEquals(self.rate * 2, self.bwg.calcCharge(user, TODAY)) # partial charge: user = MockUser(bandused=3 * GIGA, bandquota=2.5 * GIGA) self.assertEquals(self.rate / 2, self.bwg.calcCharge(user, TODAY)) # no overage: user = MockUser(bandused=3 * GIGA, bandquota=50 * GIGA) self.assertEquals(0, self.bwg.calcCharge(user, TODAY)) def test_chargeAccounts(self): plan = Plan(bandquota=GIGA) u1 = User(plan=plan, account=Account(), username="******") u1.usage << Usage(whichday=TODAY - 1, traffic=1 * GIGA) u1.usage << Usage(whichday=TODAY - 2, traffic=1 * GIGA) u1 = self.clerk.store(u1) self.bwg.chargeAccounts(TODAY) self.assertEquals(self.rate, u1.account.balance()) assert u1.account.events[0].note == "bandwidth overage [ftempy]" u2 = self.clerk.store(User(plan=plan, account=Account())) self.bwg.chargeAccounts(TODAY) assert u2.account.balance() == 0 assert len(u2.account.events) == 0 # don't want $0.00 charges
class BandwidthGruntTest(unittest.TestCase): def setUp(self): self.clerk = MockClerk(Schema(cornerhost.config.DBMAP)) self.rate = Decimal("1.23") # dollars per gig... :) self.bwg = BandwidthGrunt(self.clerk, self.rate) def test_findDueAccounts(self): # one due today, one tomorrow, one today but versionhost a = self.clerk.store(Account(account='a', nextDue=TODAY)) b = self.clerk.store(Account(account='b', nextDue=TODAY+1)) c = self.clerk.store(Account(account='c', nextDue=TODAY, brand="versionhost")) due = self.bwg.findDueAccounts() assert due == [a] def test_findUsersForAccount(self): a = Account() u1 = self.clerk.store(User(account=a)) u2 = self.clerk.store(User(account=Account())) u3 = self.clerk.store(User(account=a)) users = self.bwg.findUsersForAccount(a) assert users == [u1, u3] def test_monthlyTraffic(self): arbdate = Date("2/29/2004") startdate = Date("1/29/2004") user = User() # this one is out of range: user.usage << Usage(whichday=startdate-1, traffic= 1 * GIGA) # this one is cool: user.usage << Usage(whichday=startdate, traffic= 2 * GIGA) # these are good: user.usage << Usage(whichday=arbdate-3, traffic= 4 * GIGA) user.usage << Usage(whichday=arbdate-2, traffic= 8 * GIGA) user.usage << Usage(whichday=arbdate-1, traffic= 16 * GIGA) # this one is in the "future", since it # won't be recorded until "tonight": user.usage << Usage(whichday=arbdate, traffic= 32 * GIGA) self.assertEquals(self.bwg.monthlyTraffic(user, arbdate), (2 + 4 + 8 + 16) * GIGA) def test_calcOverage(self): user = MockUser(bandused=3*GIGA, bandquota=1*GIGA) assert self.bwg.calcOverage(user, TODAY) == 2*GIGA user = MockUser(bandused=2*GIGA, bandquota=5*GIGA) assert self.bwg.calcOverage(user, TODAY) == 0 def test_calcCharge(self): # overage: user = MockUser(bandused=3*GIGA, bandquota=1*GIGA) self.assertEquals(self.rate * 2, self.bwg.calcCharge(user, TODAY)) # partial charge: user = MockUser(bandused=3*GIGA, bandquota=2.5*GIGA) self.assertEquals(self.rate / 2, self.bwg.calcCharge(user, TODAY)) # no overage: user = MockUser(bandused=3*GIGA, bandquota=50*GIGA) self.assertEquals(0, self.bwg.calcCharge(user, TODAY)) def test_chargeAccounts(self): plan = Plan(bandquota=GIGA) u1 = User(plan=plan, account=Account(), username="******") u1.usage << Usage(whichday=TODAY-1, traffic=1*GIGA) u1.usage << Usage(whichday=TODAY-2, traffic=1*GIGA) u1 = self.clerk.store(u1) self.bwg.chargeAccounts(TODAY) self.assertEquals(self.rate, u1.account.balance()) assert u1.account.events[0].note == "bandwidth overage [ftempy]" u2 = self.clerk.store(User(plan=plan, account=Account())) self.bwg.chargeAccounts(TODAY) assert u2.account.balance()==0 assert len(u2.account.events) == 0 # don't want $0.00 charges
class NewSiteGruntTest(unittest.TestCase): def setUp(self): self.clerk = MockClerk(Schema(cornerhost.config.DBMAP)) self.user = self.clerk.store(User(username="******", server=Server(name="mockium"))) def makeGrunt(self): return NewSiteGrunt(self.clerk, self.user) def test_storage(self): g = self.makeGrunt() assert g.user.username == "ftempy" assert g.user.ID == 1 g.addDomain("ftempy.com") d = self.clerk.fetch(Domain, 1) assert d.domain == "ftempy.com", "Domain record didn't get saved!" assert d.user.ID == 1, "didn't store domain.user.ID: %s" % d.user.ID assert d.ID == 1 assert d.site is None self.makeGrunt().addSite("ftempy.com") sites = self.clerk.match(Site) assert len(sites)== 1, "expectedd 1 site, got %s" % len(sites) s = sites[0] assert s.domain.ID==1, "wrong domainID: %s" % s.domain.ID assert s.user.ID == 1, "didn't store site.user.ID: %s" % s.user.ID assert s.ID == 1 d = self.clerk.fetch(Domain, 1) assert d.site.ID == 1, "didn't update domain.site.ID" def test_makeDirs(self): "it should just pass the call the the appropriate beaker" MockBeaker.log = [] mk = MockBeaker() self.makeGrunt().makeDirs("ftempy.com") assert mk.log == ["makeSiteDirs('ftempy','ftempy.com') "], \ "didn't relay makesite request to beaker!: %s" % mk.log def test_checkDomain(self): c = self.clerk def fails(d): try: self.makeGrunt().checkDomain(d) except ValueError: return True # invalid domains should fail: assert fails("sabren..com") # should fail if domain in the system: assert not fails("ftempy.com") u = c.fetch(User,1) u.domains << Domain(domain="ftempy.com") c.store(u) assert fails("ftempy.com") # www indicates subdomains, which we don't allow yet! assert fails("www.wtempy.com") # another subdomain check: assert not fails( "sub.rufustempy.com") assert fails("sub.ftempy.com") # finally, disallow *.sabren.com wildcard sudomains: assert fails("ftempy.sabren.com") assert fails("ftempy.ab.sabren.com") def test_addSub(self): grunt = self.makeGrunt() try: grunt.addSub("sub","domain.com") gotError = 0 except ValueError: gotError = 1 assert gotError, "should have failed! - domain.com is unknown" # but if we add it: grunt.addDomain("domain.com") # have to reload ftempy though: grunt = self.makeGrunt() grunt.addSub("sub","domain.com") # now make sure it got saved: d = self.clerk.match(Domain,domain="sub.domain.com")[0] assert d.rectype=="cname" assert d.location=="domain.com" assert d.zone.domain == "domain.com" assert d.user.ID == 1 assert not d.processmail # and make sure we can't add subs to subs # (instead just make a new subdomain with a dot in it) try: grunt = self.makeGrunt() grunt.addSub("sub","sub.domain.com") except: pass else: self.fail("should fail when nesting subdomains")