Coverage for webapp / app.py: 98%

66 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2025-12-29 22:06 +0000

1""" 

2A Flask application for snapcraft.io. 

3 

4The web frontend for the snap store. 

5""" 

6 

7# We import the config module before anything else to make sure env vars are 

8# loaded properly and the FLASK_* prefix is stripped before they are parsed 

9import webapp.config # noqa: F401 

10 

11import sentry_sdk 

12 

13from canonicalwebteam.flask_base.app import FlaskBase 

14from webapp.blog.views import init_blog 

15from webapp.docs.views import init_docs 

16from webapp.extensions import csrf, vite 

17from webapp.handlers import set_handlers 

18from webapp.login.views import login 

19from webapp.login.oauth_views import oauth 

20from webapp.publisher.snaps.views import publisher_snaps 

21from webapp.publisher.github.views import publisher_github 

22from webapp.admin.views import admin 

23from webapp.publisher.views import account 

24from webapp.snapcraft.views import snapcraft_blueprint 

25from webapp.store.views import store_blueprint 

26from webapp.tutorials.views import init_tutorials 

27from webapp.packages.store_packages import store_packages 

28from webapp.endpoints.views import endpoints 

29from webapp.endpoints.signing_keys import signing_keys 

30from webapp.endpoints.models import models 

31from webapp.endpoints.members import members 

32from webapp.endpoints.snaps import snaps 

33from webapp.endpoints.snap_search import snap_search 

34from webapp.endpoints.validation_sets import validation_sets 

35from webapp.endpoints.invites import invites 

36from webapp.endpoints.settings import settings 

37from webapp.feeds.feeds import feeds 

38from webapp.config import SENTRY_DSN 

39 

40 

41def create_app(testing=False): 

42 sentry_sdk.init(dsn=SENTRY_DSN) 

43 

44 app = FlaskBase( 

45 __name__, 

46 "snapcraft.io", 

47 favicon_url="https://assets.ubuntu.com/v1/d4ca039f-favicon_16px.png", 

48 template_404="404.html", 

49 template_folder="../templates", 

50 static_folder="../static", 

51 ) 

52 app.config.from_object("webapp.config") 

53 app.name = "snapcraft" 

54 app.testing = testing 

55 

56 init_extensions(app) 

57 set_handlers(app) 

58 

59 app.register_blueprint(snapcraft_blueprint()) 

60 app.register_blueprint(store_packages) 

61 app.register_blueprint(login) 

62 app.register_blueprint(oauth) 

63 app.register_blueprint(store_blueprint()) 

64 app.register_blueprint(account, url_prefix="/account") 

65 app.register_blueprint(publisher_snaps) 

66 app.register_blueprint(publisher_github) 

67 app.register_blueprint(admin) 

68 app.register_blueprint(endpoints) 

69 app.register_blueprint(signing_keys) 

70 app.register_blueprint(models) 

71 app.register_blueprint(members) 

72 app.register_blueprint(snaps) 

73 app.register_blueprint(snap_search) 

74 app.register_blueprint(validation_sets) 

75 app.register_blueprint(invites) 

76 app.register_blueprint(settings) 

77 app.register_blueprint(feeds) 

78 init_docs(app, "/docs") 

79 init_blog(app, "/blog") 

80 init_tutorials(app, "/tutorials") 

81 

82 return app 

83 

84 

85def init_extensions(app: FlaskBase): 

86 vite.init_app(app) 

87 

88 if not app.testing: 

89 csrf.init_app(app) 

90 else: 

91 # add a helper for injecting a mock CSRF token 

92 @app.context_processor 

93 def inject_csrf_token(): 

94 return dict(csrf_token=lambda: "mocked_csrf_token")