<s> import json <EOL> from aleph . core import db <EOL> from aleph . model import Source , Permission , Role <EOL> from aleph . tests . util import TestCase <EOL> class SourcesApiTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( SourcesApiTestCase , self ) . setUp ( ) <EOL> self . source = Source ( ) <EOL> self . source . foreign_id = "<STR_LIT:test>" <EOL> self . source . label = "<STR_LIT>" <EOL> self . source . category = "<STR_LIT>" <EOL> db . session . add ( self . source ) <EOL> db . session . flush ( ) <EOL> permission = Permission ( ) <EOL> permission . role_id = Role . system ( Role . SYSTEM_USER ) <EOL> permission . read = True <EOL> permission . write = True <EOL> permission . resource_id = self . source . id <EOL> permission . resource_type = Permission . SOURCE <EOL> db . session . add ( permission ) <EOL> db . session . commit ( ) <EOL> def test_index ( self ) : <EOL> res = self . client . get ( '<STR_LIT>' ) <EOL> assert res . status_code == <NUM_LIT:200> , res <EOL> assert res . json [ '<STR_LIT>' ] == <NUM_LIT:0> , res . json <EOL> self . login ( ) <EOL> res = self . client . get ( '<STR_LIT>' ) <EOL> assert res . status_code == <NUM_LIT:200> , res <EOL> assert res . json [ '<STR_LIT>' ] >= <NUM_LIT:1> , res . json <EOL> def test_view ( self ) : <EOL> res = self . client . get ( '<STR_LIT>' % self . source . id ) <EOL> assert res . status_code == <NUM_LIT> , res <EOL> self . login ( ) <EOL> res = self . client . get ( '<STR_LIT>' % self . source . id ) <EOL> assert res . status_code == <NUM_LIT:200> , res . json <EOL> data = res . json <EOL> assert data [ '<STR_LIT:label>' ] == self . source . label <EOL> def test_view_not_found ( self ) : <EOL> res = self . client . get ( '<STR_LIT>' ) <EOL> assert res . status_code == <NUM_LIT> , res <EOL> def test_update ( self ) : <EOL> self . login ( ) <EOL> res = self . client . get ( '<STR_LIT>' % self . source . id ) <EOL> data = res . json <EOL> data [ '<STR_LIT:label>' ] = '<STR_LIT>' % data [ '<STR_LIT:label>' ] <EOL> jdata = json . dumps ( data ) <EOL> res = self . client . post ( '<STR_LIT>' % data [ '<STR_LIT:id>' ] , data = jdata , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> assert res . status_code == <NUM_LIT:200> , ( res . json , data ) <EOL> assert res . json [ '<STR_LIT:label>' ] == data [ '<STR_LIT:label>' ] , res . json <EOL> assert res . json [ '<STR_LIT:id>' ] , res . json <EOL> def test_update_invalid_label ( self ) : <EOL> self . login ( ) <EOL> res = self . client . get ( '<STR_LIT>' % self . source . id ) <EOL> data = res . json <EOL> data [ '<STR_LIT:label>' ] = '<STR_LIT:H>' <EOL> jdata = json . dumps ( data ) <EOL> res = self . client . post ( '<STR_LIT>' % data [ '<STR_LIT:id>' ] , data = jdata , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> assert res . status_code == <NUM_LIT> , res <EOL> def test_update_invalid_category ( self ) : <EOL> self . login ( ) <EOL> res = self . client . get ( '<STR_LIT>' % self . source . id ) <EOL> data = res . json <EOL> data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> jdata = json . dumps ( data ) <EOL> res = self . client . post ( '<STR_LIT>' % data [ '<STR_LIT:id>' ] , data = jdata , <EOL> content_type = '<STR_LIT:application/json>' ) <EOL> assert res . status_code == <NUM_LIT> , res </s>
