Coverage for webapp/publisher/snaps/unregister.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-25 22:09 +0000

1import time 

2 

3import flask 

4from canonicalwebteam.store_api.publishergw import PublisherGW 

5 

6from webapp import authentication 

7from webapp.helpers import api_publisher_session 

8 

9publisher_gateway = PublisherGW("snap", api_publisher_session) 

10 

11 

12def is_pending_snap_unregister_valid(pending, authorization_id): 

13 publisher = flask.session.get("publisher", {}) 

14 return ( 

15 pending 

16 and authentication.is_authenticated(flask.session) 

17 and pending["authorization_id"] == authorization_id 

18 and pending["expires_at"] > time.time() 

19 and pending["identity_url"] == publisher.get("identity_url") 

20 ) 

21 

22 

23def complete_pending_snap_unregister(resp, authorization_id): 

24 pending = flask.session.pop("pending_snap_unregister", None) 

25 if ( 

26 not is_pending_snap_unregister_valid(pending, authorization_id) 

27 or resp.identity_url != pending["identity_url"] 

28 or "root_macaroon" not in pending 

29 or "macaroon" not in resp.extensions 

30 ): 

31 flask.flash( 

32 "Unregister authorization is invalid or expired. " 

33 "Please try again using the same account.", 

34 "negative", 

35 ) 

36 return flask.redirect("/snaps") 

37 

38 response = publisher_gateway.unregister_package_name( 

39 { 

40 "macaroon_root": pending["root_macaroon"], 

41 "macaroon_discharge": resp.extensions["macaroon"].discharge, 

42 }, 

43 pending["snap_name"], 

44 ) 

45 if response.status_code == 200: 

46 flask.flash( 

47 f'{pending["snap_name"]} has been unregistered.', "positive" 

48 ) 

49 else: 

50 flask.flash(response.json()["error-list"][0]["message"], "negative") 

51 return flask.redirect("/snaps")