def update_preferences(**kwargs): """ Can be used to update preferences """ pref = get_preferences() for k, v in kwargs.items(): if k in pref: pref[k] = v get_db().preferences.replace_one({"_id": pref["_id"]}, pref) g.pop("prefs")
def test_get_db_while_testing(app): app = create_app({"TESTING": True}) with app.app_context(): db = get_db() assert hasattr(db, "name") assert isinstance(db, mongomock.database.Database)
def test_links_delete_post(client, onelink): id = ObjectId("5b92b2cd5e378f694898087a") create_link(_id=ObjectId(id), **onelink) res = client.post(f"/links/delete/{id}/") assert res.status_code == 302 assert not get_db().links.find_one({"_id": id})
def test_update_preferences_updates_prefs_in_db(app): defaults = get_defaults() key = list(defaults)[0] update_preferences(**{key: "JAJA"}) prefs = get_db().preferences.find()[0] assert prefs[key] == "JAJA"
def create_note(**kwargs): kwargs.pop("csrf_token", None) kwargs["created_at"] = datetime.now() title = kwargs.get("title") if not title: kwargs["title"] = "untitled" return get_db().notes.insert_one(kwargs)
def test_notes_create_post_is_creating(client, onenote): onenote["title"] = "creation" res = client.post("/notes/create/", data=onenote) assert res.status_code == 302 fetched = list(get_db().notes.find())[0] assert fetched["title"] == "creation"
def archive_detail(id): fs = gridfs.GridFS(get_db("archive")) try: doc = fs.get(ObjectId(id)) except gridfs.errors.NoFile: return "No such file found", 404 return send_file(doc, attachment_filename=doc.filename)
def test_links_create_post_is_creating(client, onelink): onelink["title"] = "creation" onelink["make_a_copy"] = None res = client.post("/links/create/", data=onelink) assert res.status_code == 302 fetched = list(get_db().links.find())[0] assert fetched["title"] == "creation"
def test_create_defaults(app): db = get_db() assert not list(db.preferences.find()) create_defaults() saved_prefs = list(db.preferences.find()) assert saved_prefs assert len(saved_prefs) assert saved_prefs[0]["_id"] == "1"
def get_preferences(): if "prefs" in g: return g.prefs prefs = get_db().preferences.find_one("1") if not prefs: prefs = create_defaults() g.prefs = prefs return g.prefs
def test_create_link_doesnt_put_pdf_prefix_if_disabled_in_prefs(app, onelink): # this creates a default preferences get_preferences() update_preferences(prepend_pdf_in_title=False) onelink["url"] = "https://example.com/sample.pdf" create_link(**onelink) fetched = list(get_db().links.find())[0] assert not fetched["title"].startswith("[PDF] ")
def test_links_update_post(client, onelink): id = ObjectId("5b92b2cd5e378f694898087d") create_link(_id=ObjectId(id), **onelink) onelink["url"] = "https://google.com/asdf/" res = client.post(f"/links/update/{id}/", data=onelink) assert res.status_code == 302 fetched = get_db().links.find_one({"_id": id}) assert fetched["url"] == "https://google.com/asdf/"
def test_notes_update_post(client, onenote): id = ObjectId("5b92b2cd5e378f694898087d") create_note(_id=ObjectId(id), **onenote) onenote["title"] = "random_title" res = client.post(f"/notes/update/{id}/", data=onenote) assert res.status_code == 302 fetched = get_db().notes.find_one({"_id": id}) assert fetched["title"] == "random_title"
def test_update_defaults_removes_stale_pref_key(app, monkeypatch): create_defaults() defaults = get_defaults() stale_key = list(defaults)[0] defaults.pop(stale_key) monkeypatch.setattr(firefly.preferences, "get_defaults", lambda: defaults) update_defaults() saved_config = get_db().preferences.find_one("1") assert stale_key not in saved_config
def test_update_defaults_adds_new_pref_key(app, monkeypatch): create_defaults() new_key = "new_key_yay" defaults = get_defaults() defaults[new_key] = "new_value" monkeypatch.setattr(firefly.preferences, "get_defaults", lambda: defaults) update_defaults() saved_config = get_db().preferences.find_one("1") assert new_key in saved_config assert saved_config[new_key] == "new_value"
def update_defaults(): defaults = get_defaults() config = get_preferences() updated = {} # insert newly added config keys for k, v in defaults.items(): if k not in config: updated[k] = v # remove stale config keys for k, v in config.copy().items(): if k == "_id": continue if k not in defaults: del config[k] config.update(updated) get_db().preferences.replace_one({"_id": config["_id"]}, config) g.pop("prefs")
def notes_detail(id): collection = get_db().notes note = collection.find_one({"_id": ObjectId(id)}) if not note: return "Note not found", 404 note["html"] = markdown( note["description"], extras=["fenced-code-blocks", "break-on-newline", "cuddled-lists"], ) return render_template("notes_detail.html", note=note)
def save_file(url, link_id): session = HTMLSession() response = session.get(url) content_type = response.headers["content-type"].split(";")[0] extension = mimetypes.guess_extension(content_type) if extension is None: extension = ".html" filename = f"{link_id}{extension}" fs = gridfs.GridFS(get_db("archive")) return fs.put(response.content, filename=filename)
def test_update_defaults_leaves_unchanged_keys_intact(app, monkeypatch): create_defaults() new_key = "new_key_yay" defaults = get_defaults() defaults[new_key] = "new_value" monkeypatch.setattr(firefly.preferences, "get_defaults", lambda: defaults) update_defaults() saved_config = get_db().preferences.find_one("1") saved_config.pop("_id") assert len(saved_config) == len(defaults) assert len(set(saved_config).intersection(defaults)) == len(defaults)
def save_links(app): links = get_db().links links.insert_many([ { "url": "https://example.com", "created_at": datetime(2018, 1, 1, 0, 0, 0), "tags": ["tag1", "tag2"], }, { "url": "https://www.example1.com", "created_at": datetime(2018, 1, 1, 0, 0, 0), "tags": ["tag3", "tag2"], }, ])
def create_link(**kwargs): kwargs.pop("csrf_token", None) kwargs["created_at"] = datetime.now() title = kwargs.get("title", kwargs["url"]) prefs = get_preferences() if prefs["prepend_pdf_in_title"]: if kwargs["url"].lower().endswith(".pdf") and "[PDF]" not in title: title = f"[PDF] {title}" kwargs["title"] = title result = get_db().links.insert_one(kwargs) if kwargs.pop("make_a_copy", False): make_copy_of_url(kwargs["url"], result.inserted_id)
def save_notes(app): notes = get_db().notes notes.insert_many([ { "title": "something.md", "description": "hello world", "created_at": datetime(2018, 1, 1, 0, 0, 0), "tags": ["tag1", "tag2"], }, { "title": "anything.md", "description": "any world", "created_at": datetime(2018, 1, 1, 0, 0, 0), "tags": ["tag3", "tag2"], }, ])
def links_delete(id): collection = get_db().links link = collection.find_one({"_id": ObjectId(id)}) if not link: return "Link not found", 404 if request.method == "GET": return render_template( "confirm_delete.html", item=link, back_url=url_for("links.links"), page_title="deleting link", ) collection.remove(link) return redirect(url_for("links.links"))
def notes_delete(id): collection = get_db().notes note = collection.find_one({"_id": ObjectId(id)}) if not note: return "Note not found", 404 if request.method == "GET": return render_template( "confirm_delete.html", item=note, back_url=url_for("notes.notes"), page_title="deleting note", ) collection.remove(note) return redirect(url_for("notes.notes"))
def notes_update(id): collection = get_db().notes note = collection.find_one({"_id": ObjectId(id)}) if not note: return "Note not found", 404 page_title = "updating note" if request.method == "GET": form = NoteForm(data=note) return render_template("form.html", form=form, page_title=page_title) # disable csrf for now, to make it easy to test this URL form = NoteForm(request.form, meta={"csrf": False}) if form.validate_on_submit(): update_note(note, **form.data) return redirect(url_for("notes.notes_detail", id=id)) return render_template("form.html", form=form, page_title=page_title), 400
def links_update(id): collection = get_db().links link = collection.find_one({"_id": ObjectId(id)}) if not link: return "Link not found", 404 page_title = "updating link" if request.method == "GET": form = LinkForm(data=link) return render_template("form.html", form=form, page_title=page_title) # disable csrf for now, to make it easy to test this URL form = LinkForm(request.form, meta={"csrf": False}) if form.validate_on_submit(): update_link(link, **form.data) return redirect(url_for("links.links")) return render_template("form.html", form=form, page_title=page_title), 400
def test_create_link_puts_url_in_if_title_is_not_provided(app, onelink): onelink.pop("title") create_link(**onelink) fetched = list(get_db().links.find())[0] assert fetched["title"] == onelink["url"]
def test_create_link(app, onelink): create_link(**onelink) fetched = list(get_db().links.find())[0] assert "csrf_token" not in fetched, "should exclude csrf_token" assert "created_at" in fetched, "should add created_at"
def test_create_note(app, onenote): create_note(**onenote) fetched = list(get_db().notes.find())[0] assert "csrf_token" not in fetched, "should exclude csrf_token" assert "created_at" in fetched, "should add created_at"
def test_create_note_puts_untitled_in_if_title_is_not_provided(app, onenote): onenote.pop("title") create_note(**onenote) fetched = list(get_db().notes.find())[0] assert fetched["title"] == "untitled"