<s> import json <EOL> from hc . api . models import Channel , Check <EOL> from hc . test import BaseTestCase <EOL> from hc . accounts . models import Profile <EOL> class CreateCheckTestCase ( BaseTestCase ) : <EOL> def setUp ( self ) : <EOL> super ( CreateCheckTestCase , self ) . setUp ( ) <EOL> self . profile = Profile ( user = self . alice , api_key = "<STR_LIT:abc>" ) <EOL> self . profile . save ( ) <EOL> def post ( self , url , data ) : <EOL> return self . client . post ( url , json . dumps ( data ) , <EOL> content_type = "<STR_LIT:application/json>" ) <EOL> def test_it_works ( self ) : <EOL> r = self . post ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : "<STR_LIT:abc>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> <EOL> } ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> self . assertTrue ( "<STR_LIT>" in r . json ( ) ) <EOL> self . assertEqual ( Check . objects . count ( ) , <NUM_LIT:1> ) <EOL> check = Check . objects . get ( ) <EOL> self . assertEqual ( check . name , "<STR_LIT>" ) <EOL> self . assertEqual ( check . tags , "<STR_LIT>" ) <EOL> self . assertEqual ( check . timeout . total_seconds ( ) , <NUM_LIT> ) <EOL> self . assertEqual ( check . grace . total_seconds ( ) , <NUM_LIT> ) <EOL> def test_it_assigns_channels ( self ) : <EOL> channel = Channel ( user = self . alice ) <EOL> channel . save ( ) <EOL> r = self . post ( "<STR_LIT>" , { <EOL> "<STR_LIT>" : "<STR_LIT:abc>" , <EOL> "<STR_LIT>" : "<STR_LIT:*>" <EOL> } ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> check = Check . objects . get ( ) <EOL> self . assertEqual ( check . channel_set . get ( ) , channel ) <EOL> def test_it_handles_missing_request_body ( self ) : <EOL> r = self . client . post ( "<STR_LIT>" , <EOL> content_type = "<STR_LIT:application/json>" ) <EOL> self . assertEqual ( r . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( r . json ( ) [ "<STR_LIT:error>" ] , "<STR_LIT>" ) <EOL> def test_it_rejects_wrong_api_key ( self ) : <EOL> r = self . post ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assertEqual ( r . json ( ) [ "<STR_LIT:error>" ] , "<STR_LIT>" ) <EOL> def test_it_handles_invalid_json ( self ) : <EOL> r = self . client . post ( "<STR_LIT>" , "<STR_LIT>" , <EOL> content_type = "<STR_LIT:application/json>" ) <EOL> self . assertEqual ( r . json ( ) [ "<STR_LIT:error>" ] , "<STR_LIT>" ) <EOL> def test_it_rejects_small_timeout ( self ) : <EOL> r = self . post ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT:abc>" , "<STR_LIT>" : <NUM_LIT:0> } ) <EOL> self . assertEqual ( r . json ( ) [ "<STR_LIT:error>" ] , "<STR_LIT>" ) <EOL> def test_it_rejects_large_timeout ( self ) : <EOL> r = self . post ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT:abc>" , "<STR_LIT>" : <NUM_LIT> } ) <EOL> self . assertEqual ( r . json ( ) [ "<STR_LIT:error>" ] , "<STR_LIT>" ) <EOL> def test_it_rejects_non_number_timeout ( self ) : <EOL> r = self . post ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT:abc>" , "<STR_LIT>" : "<STR_LIT>" } ) <EOL> self . assertEqual ( r . json ( ) [ "<STR_LIT:error>" ] , "<STR_LIT>" ) <EOL> def test_it_rejects_non_string_name ( self ) : <EOL> r = self . post ( "<STR_LIT>" , { "<STR_LIT>" : "<STR_LIT:abc>" , "<STR_LIT:name>" : False } ) <EOL> self . assertEqual ( r . json ( ) [ "<STR_LIT:error>" ] , "<STR_LIT>" ) </s>
