@@ -57,3 +57,63 @@ fn following() {
5757 assert ! ( !is_following( ) ) ;
5858 assert_eq ! ( user. search( "following=1" ) . crates. len( ) , 0 ) ;
5959}
60+
61+ #[ test]
62+ fn disallow_api_token_auth_for_get_crate_following_status ( ) {
63+ let ( app, _, _, token) = TestApp :: init ( ) . with_token ( ) ;
64+ let api_token = token. as_model ( ) ;
65+
66+ let a_crate = "a_crate" ;
67+
68+ app. db ( |conn| {
69+ CrateBuilder :: new ( a_crate, api_token. user_id ) . expect_build ( conn) ;
70+ } ) ;
71+
72+ // Token auth on GET for get following status is disallowed
73+ token
74+ . get ( & format ! ( "/api/v1/crates/{}/following" , a_crate) )
75+ . assert_forbidden ( ) ;
76+ }
77+
78+ #[ test]
79+ fn getting_followed_crates_allows_api_token_auth ( ) {
80+ let ( app, _, user, token) = TestApp :: init ( ) . with_token ( ) ;
81+ let api_token = token. as_model ( ) ;
82+
83+ let crate_to_follow = "some_crate_to_follow" ;
84+ let crate_not_followed = "another_crate" ;
85+
86+ app. db ( |conn| {
87+ CrateBuilder :: new ( crate_to_follow, api_token. user_id ) . expect_build ( conn) ;
88+ CrateBuilder :: new ( crate_not_followed, api_token. user_id ) . expect_build ( conn) ;
89+ } ) ;
90+
91+ let is_following = |crate_name : & str | -> bool {
92+ #[ derive( Deserialize ) ]
93+ struct F {
94+ following : bool ,
95+ }
96+
97+ // Token auth on GET for get following status is disallowed
98+ user. get :: < F > ( & format ! ( "/api/v1/crates/{}/following" , crate_name) )
99+ . good ( )
100+ . following
101+ } ;
102+
103+ let follow = |crate_name : & str | {
104+ assert ! (
105+ token
106+ . put:: <OkBool >( & format!( "/api/v1/crates/{}/follow" , crate_name) , b"" )
107+ . good( )
108+ . ok
109+ ) ;
110+ } ;
111+
112+ follow ( crate_to_follow) ;
113+
114+ assert ! ( is_following( crate_to_follow) ) ;
115+ assert ! ( !is_following( crate_not_followed) ) ;
116+
117+ let json = token. search ( "following=1" ) ;
118+ assert_eq ! ( json. crates. len( ) , 1 ) ;
119+ }
0 commit comments