def test_band_add_view_validating(self): """ add a band -- validating, be redirected to band_view """ from c3sar.views.band import band_add request = testing.DummyRequest( post={'form.submitted': True, 'band_name': u'testband', 'band_homepage': u'http://testba.nd', 'band_email': u'*****@*****.**', 'registrar_is_member': '1', }) request.user = self._makeUser() # a user to add the band request.user.username = u"newUser" self.dbsession.add(request.user) self.dbsession.flush() the_user_id = request.user.id self.config = testing.setUp(request=request) _registerRoutes(self.config) result = band_add(request) self.assertTrue(isinstance(result, HTTPFound), "expected redirect not seen") # check database contents from c3sar.models import Band max_band_id = Band.get_max_id() test_band = Band.get_by_band_id(max_band_id) self.assertEquals(test_band.id, max_band_id) self.assertEquals(test_band.name, 'testband') self.assertEquals(test_band.homepage, 'http://testba.nd') self.assertEquals(test_band.registrar_id, the_user_id) # no real user :-/ self.assertEquals(test_band.registrar, 'newUser')
def band_view(request): band_id = request.matchdict['band_id'] band = Band.get_by_band_id(band_id) # redirect if id does not exist in database if not isinstance(band, Band): request.session.flash('this id wasnt found in our database!') return HTTPFound(route_url('not_found', request)) #calculate for next/previous-navigation max_id = Band.get_max_id() #previous if band.id == 1: # if looking at first id prev_id = max_id # --> choose highest id else: # if looking at other id prev_id = band.id - 1 # --> choose previous # next if band.id != max_id: # if not on highest id next_id = band.id + 1 # --> choose next elif band.id == max_id: # if highest_id next_id = 1 # --> choose first id ('wrap around') # show who is watching. maybe we should log this ;-) viewer_username = authenticated_userid(request) return { 'band': band, 'viewer_username': viewer_username, 'prev_id': prev_id, 'next_id': next_id }
def test_track_list(self): """ track_list get number of bands from db, add two, check view and confirm resulting number """ from c3sar.models import Band band_count = Band.get_max_id() # how many in db? from c3sar.views.band import band_list instance1 = self._makeBand1() # add a band self.dbsession.add(instance1) instance2 = self._makeBand2() # add another band self.dbsession.add(instance2) request = testing.DummyRequest() self.config = testing.setUp(request=request) result = band_list(request) if DEBUG: # pragma: no cover print "result of test_band_list" pp.pprint(result) # check that there are two bandss in list self.assertEquals(len(result['bands']), band_count + 2, "wrong number of bands in list")
def test_band_view_one(self): """ view a band - just one """ from c3sar.views.band import band_view band_instance = self._makeBand1() self.dbsession.add(band_instance) self.dbsession.flush() the_id = band_instance.id # db id of that band from c3sar.models import Band max_id = Band.get_max_id() request = testing.DummyRequest() request.matchdict['band_id'] = the_id self.config = testing.setUp(request=request) result = band_view(request) self.assertTrue('band' in result, 'band was not seen.') self.assertTrue( result['prev_id'] is the_id - 1 or max_id, 'wrong nav id.') self.assertTrue( result['next_id'] is the_id + 1 or 1, 'wrong nav id.') self.assertTrue('viewer_username' in result, 'viewer_username was not seen.') self.assertTrue(result['viewer_username'] is None, 'viewer_username was not seen.')
def test_get_by_band_id(self): if DEBUG: # pragma: no cover print "----- this is BandModelTests.test_get_by_band_id" instance = self._makeOne() self.session.add(instance) self.session.flush() # to get the id from the db from c3sar.models import Band result = Band.get_by_band_id(1) self.assertEqual(instance.name, 'Some Band') self.assertEqual(result.name, 'Some Band')
def test_band_listing(self): instance = self._makeOne() self.session.add(instance) self.session.flush() # to get the id from the db from c3sar.models import Band result = Band.band_listing(order_by=None) #print "--- result: " + str(result) # [<c3sar.models.License object at 0x9fba10c>] #print "--- dir(result): " + str(dir(result)) # #print "--- license_listing: result.__len__(): " # + str(result.__len__()) # 1 self.assertEqual( result.__len__(), 1, "we expect the result list to have one entry")
def band_edit(request): band_id = request.matchdict['band_id'] band = Band.get_by_band_id(band_id) if not isinstance(band, Band): msg = "Band id not found in database" # TODO: check template! return HTTPFound(route_url('not_found', request)) # no change through form, so reuse old value (for now) band_registrar = band.registrar band_registrar_id = band.registrar_id form = Form(request, schema=BandEditSchema, obj=band) if 'form.submitted' in request.POST and not form.validate(): # form didn't validate request.session.flash('form does not validate!') #request.session.flash(form.data['name']) #request.session.flash(form.data['homepage']) #request.session.flash(form.data['email']) if 'form.submitted' in request.POST and form.validate(): if form.data['name'] != band.name: band.name = form.data['name'] if DEBUG: # pragma: no cover print "changing band name" request.session.flash('changing band name') if form.data['homepage'] != band.homepage: band.homepage = form.data['homepage'] if DEBUG: # pragma: no cover print "changing band homepage" request.session.flash('changing band homepage') if form.data['email'] != band.email: band.email = form.data['email'] if DEBUG: # pragma: no cover print "changing band email" request.session.flash('changing band email') # TODO: trigger email_verification process #request.session.flash(u'writing to database ...') dbsession.flush() # if all went well, redirect to band view return HTTPFound(route_url('band_view', request, band_id=band.id)) return { 'viewer_username': authenticated_userid(request), 'form': FormRenderer(form) }
def test_band_edit_submit(self): """ edit a band -- re-submit changed values in edit form """ from c3sar.views.band import band_edit # put some instance into database band_instance = self._makeBand1() self.dbsession.add(band_instance) self.dbsession.flush() # submit some other values: com --> ORG request = testing.DummyRequest( post={'form.submitted': True, 'homepage': u'http://band1.example.ORG', 'name': u'firstBandnameCHANGED', 'email': u'*****@*****.**'}) request.matchdict['band_id'] = 1 self.config = testing.setUp(request=request) _registerRoutes(self.config) result = band_edit(request) #pp.pprint(result) # test: redirect to band/view/1 self.assertTrue(isinstance(result, HTTPFound), "expected redirect not seen") self.assertTrue('band/view/1' in result.headerlist[2][1]) #print(result.headerlist[2][1]) self.assertEquals(result.headerlist.pop(), ('Location', 'http://example.com/band/view/1'), "not the redirect expected") from c3sar.models import Band db_band = Band.get_by_band_id(1) # test changed values in db? self.assertEquals(db_band.homepage, u'http://band1.example.ORG', "changed value didn't make it to database!?") self.assertEquals(db_band.name, u'firstBandnameCHANGED', "changed value didn't make it to database!?") self.assertEquals(db_band.email, u'*****@*****.**', "changed value didn't make it to database!?")
def band_list(request): bands = Band.band_listing(Band.id.desc()) return {'bands': bands}