diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d953aedb..ac44b4f3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,30 +11,11 @@ jobs: build: runs-on: ubuntu-22.04 - services: - postgres: - image: postgres:14 - env: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: postgres - POSTGRES_DB: dnote_test - POSTGRES_PORT: 5432 - # Wait until postgres has started - options: >- - --health-cmd pg_isready - --health-interval 10s - --health-timeout 5s - --health-retries 5 - - # Expose port to the host - ports: - - 5432:5432 - steps: - - uses: actions/checkout@v3 - - uses: actions/setup-go@v3 + - uses: actions/checkout@v5 + - uses: actions/setup-go@v6 with: - go-version: '1.20.0' + go-version: '>=1.25.0' - name: Install dependencies run: | @@ -47,3 +28,7 @@ jobs: - name: Test app run: | make test-api + + - name: Test e2e + run: | + make test-e2e diff --git a/.github/workflows/release-cli.yml b/.github/workflows/release-cli.yml new file mode 100644 index 00000000..81a2ea5e --- /dev/null +++ b/.github/workflows/release-cli.yml @@ -0,0 +1,77 @@ +name: Release CLI + +on: + push: + tags: + - 'cli-v*' + +jobs: + release: + runs-on: ubuntu-22.04 + permissions: + contents: write + + steps: + - uses: actions/checkout@v5 + with: + fetch-depth: 0 + - uses: actions/setup-go@v6 + with: + go-version: '>=1.25.0' + + - name: Extract version from tag + id: version + run: | + TAG=${GITHUB_REF#refs/tags/cli-v} + echo "version=$TAG" >> $GITHUB_OUTPUT + echo "Releasing version: $TAG" + + - name: Install dependencies + run: make install + + - name: Run CLI tests + run: make test-cli + + - name: Run E2E tests + run: make test-e2e + + - name: Build CLI + run: make version=${{ steps.version.outputs.version }} build-cli + + - name: Generate changelog + run: | + VERSION="${{ steps.version.outputs.version }}" + TAG="cli-v${VERSION}" + + # Find previous CLI tag + PREV_TAG=$(git tag --sort=-version:refname | grep "^cli-" | grep -v "^${TAG}$" | head -n 1) + + if [ -z "$PREV_TAG" ]; then + echo "Error: No previous CLI tag found" + echo "This appears to be the first release." + exit 1 + fi + + ./scripts/generate-changelog.sh cli "$TAG" "$PREV_TAG" > /tmp/changelog.txt + cat /tmp/changelog.txt + + - name: Create GitHub release + env: + GH_TOKEN: ${{ github.token }} + run: | + VERSION="${{ steps.version.outputs.version }}" + TAG="cli-v${VERSION}" + + # Determine if prerelease (version not matching major.minor.patch) + FLAGS="" + if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + FLAGS="--prerelease" + fi + + gh release create "$TAG" \ + build/cli/*.tar.gz \ + build/cli/*_checksums.txt \ + $FLAGS \ + --title="$TAG" \ + --notes-file=/tmp/changelog.txt \ + --draft diff --git a/.github/workflows/release-server.yml b/.github/workflows/release-server.yml new file mode 100644 index 00000000..a5f52933 --- /dev/null +++ b/.github/workflows/release-server.yml @@ -0,0 +1,109 @@ +name: Release Server + +on: + push: + tags: + - 'server-v*' + +jobs: + release: + runs-on: ubuntu-22.04 + permissions: + contents: write + + steps: + - uses: actions/checkout@v5 + with: + fetch-depth: 0 + - uses: actions/setup-go@v6 + with: + go-version: '>=1.25.0' + - uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Extract version from tag + id: version + run: | + TAG=${GITHUB_REF#refs/tags/server-v} + echo "version=$TAG" >> $GITHUB_OUTPUT + echo "Releasing version: $TAG" + + - name: Install dependencies + run: make install + + - name: Run tests + run: make test + + - name: Build server + run: make version=${{ steps.version.outputs.version }} build-server + + - name: Generate changelog + run: | + VERSION="${{ steps.version.outputs.version }}" + TAG="server-v${VERSION}" + + # Find previous server tag + PREV_TAG=$(git tag --sort=-version:refname | grep "^server-" | grep -v "^${TAG}$" | head -n 1) + + if [ -z "$PREV_TAG" ]; then + echo "Error: No previous server tag found" + echo "This appears to be the first release." + exit 1 + fi + + ./scripts/generate-changelog.sh server "$TAG" "$PREV_TAG" > /tmp/changelog.txt + cat /tmp/changelog.txt + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Prepare Docker build context + run: | + VERSION="${{ steps.version.outputs.version }}" + cp build/server/dnote_server_${VERSION}_linux_amd64.tar.gz host/docker/ + cp build/server/dnote_server_${VERSION}_linux_arm64.tar.gz host/docker/ + cp build/server/dnote_server_${VERSION}_linux_arm.tar.gz host/docker/ + cp build/server/dnote_server_${VERSION}_linux_386.tar.gz host/docker/ + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_TOKEN }} + + - name: Build and push Docker image + uses: docker/build-push-action@v6 + with: + context: ./host/docker + push: true + platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/386 + tags: | + dnote/dnote:${{ steps.version.outputs.version }} + dnote/dnote:latest + build-args: | + version=${{ steps.version.outputs.version }} + + - name: Create GitHub release + env: + GH_TOKEN: ${{ github.token }} + run: | + VERSION="${{ steps.version.outputs.version }}" + TAG="server-v${VERSION}" + + # Determine if prerelease (version not matching major.minor.patch) + FLAGS="" + if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + FLAGS="--prerelease" + fi + + gh release create "$TAG" \ + build/server/*.tar.gz \ + build/server/*_checksums.txt \ + $FLAGS \ + --title="$TAG" \ + --notes-file=/tmp/changelog.txt \ + --draft diff --git a/.gitignore b/.gitignore index 9ed8a65d..2847e75d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ *.log node_modules /test +tmp +*.db +/server diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 9f82a7ba..00000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,276 +0,0 @@ -# CHANAGELOG - -All notable changes to the projects under this repository will be documented in this file. - -* [Server](#server) -* [CLI](#cli) -* [Browser Extensions](#browser-extensions) - -## Server - -The following log documents the history of the server project. - -### Unreleased - -None - -### 2.0.0 2021-05-09 - -#### Removed - -- The web interface for managing notes and books (#594) - -### 1.0.4 2020-05-23 - -#### Removed - -- Simplify the bundle by removing unnecessary payment logic - -#### Fixed - -- Fix timestamp in the note content view -- Invalidate existing sessions when password is changed - -### 1.0.3 2020-05-03 - -#### Fixed - -- Fix timeline grouping notes by added time rather than updated time. - -#### Changed - -- Sort notes by last activity to make it easier to see the most recently accessed information. - -### 1.0.2 2020-05-03 - -#### Changed - -* Support arm64. - -### 1.0.1 - 2020-03-29 - -- Fix fresh install running migrations against tables that no longer exists. - -### 1.0.0 - 2020-03-22 - -#### Fixed - -- Fix unsubscribe link from the inactive reminder (#433) - -#### Removed - -- Remove the deprecated features related to digests and repetition rules (#432) -- Remove the migration for the deprecated, encrypted Dnote (#433) - -#### Changed - -- Please set `OnPremise` environment to `true` in order to automatically use the Pro version. - -### 0.5.0 - 2020-02-06 - -#### Changed - -- **Deprecated** the digest and digest emails (#397) -- **Deprecated** the repetition rules (#397) - -#### Fixed - -- Fix refocusing to the end of the textarea input (#405) - -### 0.4.0 - 2020-01-09 - -#### Added - -- A web-based digest (#380) - -#### Fixed - -- Send inactive reminders with a correct email type (#385) -- Wrap words in note content (#389) - -### 0.3.4 - 2019-12-24 - -#### Added - -- Remind when the knowledge base stops growing (#375) -- Alert when a password is changed (#375) - -#### Fixed - -- Implement syntax highlighting for code blocks ($377) - -### 0.3.3 - 2019-12-17 - -#### Added - -- Send welcome email with login instructions upon reigstering (#352) -- Add an option to disable registration (#365) - -#### Changed - -- Send emails from the domain that hosts the application for on premise installations (#355) -- For on premise installations, automatically upgrade user accounts (#361) - -### 0.3.2 - 2019-11-20 - -#### Fixed - -- Fix server crash upon landing on a note page (#324). -- Allow to synchronize a large number of records (#321) - -### 0.3.1 - 2019-11-12 - -#### Fixed - -- Fix static files not being embedded in the binary. (#309) -- Fix mobile menu not covering the whole screen. (#308) - -### 0.3.0 - 2019-11-12 - -#### Added - -- Share notes (#300) -- Allow to recover from a missed repetition processing (#305) - -### 0.2.1 - 2019-11-04 - -#### Upgrade Guide - -* Please define the follwoing new environment variables: - - - `WebURL`: the URL to your Dnote server, without the trailing slash. (e.g. `https://my-server.com`) (Please see #290) - - `SmtpPort`: the SMTP port. (e.g. `465`) optional - required *if you want to configure email* - -#### Added - -- Display version number in the settings (#293) -- Allow unsecure database connection in production (#276) - -#### Fixed - -- Allow to customize the app URL in the emails (#290) -- Allow to customize the SMTP port (#292) - -### 0.2.0 - 2019-10-28 - -#### Added - -- Specify spaced repetition rule (#280) - -#### Changed - -- Treat a linebreak as a new line in the preview (#261) -- Allow to have multiple editor states for adding and editing notes (#260) - -#### Fixed - -- Fix jumping focus on editor (#265) - -### 0.1.1 - 2019-09-30 - -#### Fixed - -- Fix asset loading (#257) - - -### 0.1.0 - 2019-09-30 - -#### Added - -- Full-text search (#254) -- Password recovery (#254) -- Embedded notes in the digest emails (#254) - -#### Removed - -- **Breaking Change**: End-to-end encryption was removed. Existing users need to go to `/classic` and follow the automated migration steps. (#254) -- **Breaking Change**: `v1` and `v2` API endpoints were removed, and `v3` API was added as a replacement. - -#### Migration guide - -- In your application, navigate to `/classic` and follow the automated migration steps. - - -## CLI - -The following log documentes the history of the CLI project - -### Unreleased - -None - -### 0.12.0 - 2020-01-03 - -#### Upgrade guide - -* **On Linux or macOS** Please move your Dnote files to new directories based on the XDG base directory specfication. **On Windows**, no action is required. - -``` -# Move the database file -mv ~/.dnote/dnote.db ~/.local/share/dnote/dnote.db - -# Move the config file -mv ~/.dnote/dnoterc ~/.config/dnote/dnoterc - -# Delete ~/.dnote. (it is safe to delete DNOTE_TMPCONTENT.md files, if they exist.) -rm -rf ~/.dnote -``` - -If `~/.dnote` directory exists, dnote will continue to use that directory for backward compatibility until the next major release. - -#### Added - -- Add `--content-only` flag to print the note content only (#528) - -#### Changed - -- Use XDG base directory on Linux and macOS (#527) - -### 0.11.1 - 2020-04-25 - -#### Fixed - -- Fix upgrade URL (#453) - -#### Changed - -- Display hostname of the self-hosted instance while logging in (#454) -- Display helpful error if endpoint is misconfigured (#455) - -### 0.11.0 - 2020-02-05 - -#### Added - -- Allow to pass credentials through flags while logging in (#403) - -### 0.10.0 - 2019-09-30 - -#### Removed - -- **Breaking Change**: End-to-end encryption was removed. Previous versions will no longer be able to interact with the web API, because `v1` and `v2` endpoints were replaced by a new `v3` endpoint to remove encryption. - -#### Migration guide - -- If you are using Dnote Pro, change the value of `apiEndpoint` in `~/.dnote/dnoterc` to `https://api.getdnote.com`. - -## Browser Extensions - -The following log documentes the history of the browser extensions project - -### [Unreleased] - -N/A - -### 2.0.0 - 2019-10-29 - -- Allow to customize API and web URLs (#285) - -### 1.1.1 - 2019-10-02 - -- Fix failing requests (#263) - -### 1.1.0 - 2019-09-30 - -#### Removed - -- **Breaking Change**: End-to-end encryption was removed. Previous versions will no longer be able to interact with the web API, because `v1` and `v2` endpoints were replaced by a new `v3` endpoint to remove encryption. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fb41959d..b0fa33eb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,89 +8,61 @@ Dnote is an open source project. ## Setting up -Dnote uses [Vagrant](https://github.com/hashicorp/vagrant) to provision a consistent development environment. +The CLI and server are single single binary files with SQLite embedded - no databases to install, no containers to run, no VMs required. -*Prerequisites* +**Prerequisites** -* Vagrant ([Download](https://www.vagrantup.com/downloads.html)) -* VirtualBox ([Download](https://www.virtualbox.org/)) +* Go 1.25+ ([Download](https://go.dev/dl/)) +* Node.js 18+ ([Download](https://nodejs.org/) - only needed for building frontend assets) -Run the following command from the project root. It starts the virtual machine and bootstraps the project. +**Quick Start** -``` -vagrant up -``` +1. Clone the repository +2. Install dependencies: + ```bash + make install + ``` +3. Start developing! Run tests: + ```bash + make test + ``` + Or start the dev server: + ```bash + make dev-server + ``` -*Workflow* - -* You can make changes to the source code from the host machine. -* Any commands need to be run inside the virtual machine. You can connect to it by running `vagrant ssh`. -* If you want to run tests in a WATCH mode, please do so from the host machine. We cannot watch file changes due to the limitation of file system used in a virtual machine. +That's it. You're ready to contribute. ## Server -The server consists of the frontend web application and a web server. - -### Development - -* Run `make dev-server` to start a local server. -* You can access the server on `localhost:3000` on your machine. - -### Test - ```bash -# Run tests for app +# Start dev server (runs on localhost:3001) +make dev-server + +# Run tests make test-api -# Run in watch mode +# Run tests in watch mode WATCH=true make test-api ``` - ## Command Line Interface -### Build +```bash +# Run tests +make test-cli -You can build either a development version or a production version: - -``` -# Build a development version for your platform and place it in your `PATH`. +# Build dev version (places in your PATH) make debug=true build-cli -# Build a production version for all platforms +# Build production version for all platforms make version=v0.1.0 build-cli -# Build a production version for a specific platform +# Build for a specific platform # Note: You cannot cross-compile using this method because Dnote uses CGO # and requires the OS specific headers. GOOS=[insert OS] GOARCH=[insert arch] make version=v0.1.0 build-cli -``` -### Test - -* Run all tests for the command line interface: - -``` -make test-cli -``` - -### Debug - -Run Dnote with `DNOTE_DEBUG=1` to print debugging statements. For instance: - -``` +# Debug mode DNOTE_DEBUG=1 dnote sync ``` - -### Release - -* Run `make version=v0.1.0 release-cli` to achieve the following: - * Build for all target platforms, create a git tag, push all tags to the repository - * Create a release on GitHub and [Dnote Homebrew tap](https://github.com/dnote/homebrew-dnote). - -**Note** - -- If a release is not stable, - - disable the homebrew release by commenting out relevant code in the release script. - - mark release as pre-release on GitHub release - diff --git a/LICENSE b/LICENSE index 0c667b35..6b0b1270 100644 --- a/LICENSE +++ b/LICENSE @@ -1,8 +1,203 @@ -Source code in this repository is variously licensed under the GNU Affero General Public -License v3.0 (GNU AGPLv3), and GNU General Public License v3.0 (GNU GPLv3). A copy of each -license can be found in the licenses directory. The Source code for the cli is licensed under -GNU GPLv3. The source code for the server and the web is licensed under GNU AGPLv3. Unless -otherwise noted, source code in a given file is licensed under the GNU AGPLv3. -Unless otherwise noted at the beginning of the file, the copyright belongs to -Monomax Software Pty Ltd. + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/Makefile b/Makefile index 494fafbe..c38819c0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NPM := $(shell command -v npm 2> /dev/null) -HUB := $(shell command -v hub 2> /dev/null) +GH := $(shell command -v gh 2> /dev/null) currentDir = $(shell pwd) serverOutputDir = ${currentDir}/build/server @@ -30,10 +30,10 @@ endif .PHONY: install-js ## test -test: test-cli test-api +test: test-cli test-api test-e2e .PHONY: test -test-cli: +test-cli: generate-cli-schema @echo "==> running CLI test" @(${currentDir}/scripts/cli/test.sh) .PHONY: test-cli @@ -43,11 +43,10 @@ test-api: @(${currentDir}/scripts/server/test-local.sh) .PHONY: test-api -test-selfhost: - @echo "==> running a smoke test for self-hosting" - - @${currentDir}/host/smoketest/run_test.sh ${tarballPath} -.PHONY: test-selfhost +test-e2e: + @echo "==> running E2E test" + @(${currentDir}/scripts/e2e/test.sh) +.PHONY: test-e2e # development dev-server: @@ -60,11 +59,31 @@ ifndef version $(error version is required. Usage: make version=0.1.0 build-server) endif + @echo "==> building server assets" + @(cd "${currentDir}/pkg/server/assets/" && ./styles/build.sh) + @(cd "${currentDir}/pkg/server/assets/" && ./js/build.sh) + @echo "==> building server" @${currentDir}/scripts/server/build.sh $(version) .PHONY: build-server -build-cli: +build-server-docker: build-server +ifndef version + $(error version is required. Usage: make version=0.1.0 [platform=linux/amd64] build-server-docker) +endif + + @echo "==> building Docker image" + @(cd ${currentDir}/host/docker && ./build.sh $(version) $(platform)) +.PHONY: build-server-docker + +generate-cli-schema: + @echo "==> generating CLI database schema" + @mkdir -p pkg/cli/database + @touch pkg/cli/database/schema.sql + @go run -tags fts5 ./pkg/cli/database/schema +.PHONY: generate-cli-schema + +build-cli: generate-cli-schema ifeq ($(debug), true) @echo "==> building cli in dev mode" @${currentDir}/scripts/cli/dev.sh @@ -79,53 +98,6 @@ endif endif .PHONY: build-cli -## release -release-cli: clean build-cli -ifndef version - $(error version is required. Usage: make version=0.1.0 release-cli) -endif -ifndef HUB - $(error please install hub) -endif - - if [ ! -d ${cliHomebrewDir} ]; then \ - @echo "homebrew-dnote not found locally. did you clone it?"; \ - @exit 1; \ - fi - - @echo "==> releasing cli" - @${currentDir}/scripts/release.sh cli $(version) ${cliOutputDir} - - @echo "===> releading on Homebrew" - @(cd "${cliHomebrewDir}" && \ - ./release.sh "$(version)" "${cliOutputDir}/dnote_$(version)_darwin_amd64.tar.gz") -.PHONY: release-cli - -release-server: -ifndef version - $(error version is required. Usage: make version=0.1.0 release-server) -endif -ifndef HUB - $(error please install hub) -endif - - @echo "==> releasing server" - @${currentDir}/scripts/release.sh server $(version) ${serverOutputDir} - - @echo "==> building and releasing docker image" - @(cd ${currentDir}/host/docker && ./build.sh $(version)) - @(cd ${currentDir}/host/docker && ./release.sh $(version)) -.PHONY: release-server - -# migrations -create-migration: -ifndef filename - $(error filename is required. Usage: make filename=your-filename create-migration) -endif - - @(cd ${currentDir}/pkg/server/database && ./scripts/create-migration.sh $(filename)) -.PHONY: create-migration - clean: @git clean -f @rm -rf build diff --git a/README.md b/README.md index e458b9f7..7ab0063f 100644 --- a/README.md +++ b/README.md @@ -3,38 +3,62 @@ ![Build Status](https://github.com/dnote/dnote/actions/workflows/ci.yml/badge.svg) -Dnote is a simple command line notebook for programmers. +Dnote is a simple command line notebook. Single binary, no dependencies. Since 2017. -It **keeps you focused** by providing a way of effortlessly capturing and retrieving information **without leaving your terminal**. It also offers a seamless **multi-device sync**. +Your notes are stored in **one SQLite file** - portable, searchable, and completely under your control. Optional sync between devices via a self-hosted server with REST API access. -![A demo of Dnote command line interface](assets/cli.gif "Dnote command line interface") +```sh +# Add a note (or omit -c to launch your editor) +dnote add linux -c "Check disk usage with df -h" + +# View notes in a book +dnote view linux + +# Full-text search +dnote find "disk usage" + +# Sync notes +dnote sync +``` ## Installation -On macOS, you can install using Homebrew: +```bash +# Linux, macOS, FreeBSD, Windows +curl -s https://www.getdnote.com/install | sh -```sh -brew tap dnote/dnote +# macOS with Homebrew brew install dnote ``` -On Linux or macOS, you can use the installation script: +Or [download binary](https://github.com/dnote/dnote/releases). - curl -s https://www.getdnote.com/install | sh +## Server (Optional) -Otherwise, you can download the binary for your platform manually from the [releases page](https://github.com/dnote/dnote/releases). +Server is a binary with SQLite embedded. No database setup is required. -## Server +If using docker, create a compose.yml: -The quickest way to experience the Dnote server is to use [Dnote Cloud](https://app.getdnote.com). +```yaml +services: + dnote: + image: dnote/dnote:latest + container_name: dnote + ports: + - 3001:3001 + volumes: + - ./dnote_data:/data + restart: unless-stopped +``` -Or you can install it on your server by [using Docker](https://github.com/dnote/dnote/blob/master/host/docker/README.md), or [using a binary](https://github.com/dnote/dnote/blob/master/SELF_HOSTING.md). +Then run: + +```bash +docker-compose up -d +``` + +Or see the [guide](https://www.getdnote.com/docs/server/manual) for binary installation. ## Documentation -Please see [Dnote wiki](https://github.com/dnote/dnote/wiki) for the documentation. - -## See Also - -- [Homepage](https://www.getdnote.com) -- [I Wrote Down Everything I Learned While Programming for a Month](https://www.getdnote.com/blog/writing-everything-i-learn-coding-for-a-month/) +See the [Dnote doc](https://www.getdnote.com/docs). diff --git a/SELF_HOSTING.md b/SELF_HOSTING.md index c60c06b8..e67df891 100644 --- a/SELF_HOSTING.md +++ b/SELF_HOSTING.md @@ -1,183 +1,43 @@ -# Installing Dnote Server +# Self-Hosting Dnote Server -This guide documents the steps for installing the Dnote server on your own machine. If you prefer Docker, please see [the Docker guide](https://github.com/dnote/dnote/blob/master/host/docker/README.md). +Please see the [doc](https://www.getdnote.com/docs/server) for more. -## Overview +## Docker Installation -Dnote server comes as a single binary file that you can simply download and run. It uses Postgres as the database. +1. Install [Docker](https://docs.docker.com/install/). +2. Install Docker [Compose plugin](https://docs.docker.com/compose/install/linux/). +3. Create a `compose.yml` file with the following content: -## Installation +```yaml +services: + dnote: + image: dnote/dnote:latest + container_name: dnote + ports: + - 3001:3001 + volumes: + - ./dnote_data:/data + restart: unless-stopped +``` -1. Install Postgres 11+. -2. Create a `dnote` database by running `createdb dnote` -3. Download the official Dnote server release from the [release page](https://github.com/dnote/dnote/releases). -4. Extract the archive and move the `dnote-server` executable to `/usr/local/bin`. +4. Run the following to download the image and start the container + +``` +docker compose up -d +``` + +Visit http://localhost:3001 in your browser to see Dnote running. + +## Manual Installation + +Download from [releases](https://github.com/dnote/dnote/releases), extract, and run: ```bash tar -xzf dnote-server-$version-$os.tar.gz mv ./dnote-server /usr/local/bin +dnote-server start --baseUrl=https://your.server ``` -4. Run Dnote +You're up and running. Database: `~/.local/share/dnote/server.db` (customize with `--dbPath`). Run `dnote-server start --help` for options. -```bash -GO_ENV=PRODUCTION \ -OnPremise=true \ -DBHost=localhost \ -DBPort=5432 \ -DBName=dnote \ -DBUser=$user \ -DBPassword=$password \ -WebURL=$webURL \ -SmtpHost=$SmtpHost \ -SmtpPort=$SmtpPort \ -SmtpUsername=$SmtpUsername \ -SmtpPassword=$SmtpPassword \ -DisableRegistration=false - dnote-server start -``` - -Replace `$user`, `$password` with the credentials of the Postgres user that owns the `dnote` database. - -Replace `$webURL` with the full URL to your server, without a trailing slash (e.g. `https://your.server`). - -Replace `$SmtpHost`, `SmtpPort`, `$SmtpUsername`, `$SmtpPassword` with actual values, if you would like to receive spaced repetition through email. - -Replace `DisableRegistration` to `true` if you would like to disable user registrations. - -By default, dnote server will run on the port 3000. - -## Configuration - -By now, Dnote is fully functional in your machine. The API, frontend app, and the background tasks are all in the single binary. Let's take a few more steps to configure Dnote. - -### Configure Nginx - -To make it accessible from the Internet, you need to configure Nginx. - -1. Install nginx. -2. Create a new file in `/etc/nginx/sites-enabled/dnote` with the following contents: - -``` -server { - server_name my-dnote-server.com; - - location / { - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $remote_addr; - proxy_set_header Host $host; - proxy_pass http://127.0.0.1:3000; - } -} -``` -3. Replace `my-dnote-server.com` with the URL for your server. -4. Reload the nginx configuration by running the following: - -``` -sudo service nginx reload -``` - -### Configure Apache2 - -1. Install Apache2 and install/enable mod_proxy. -2. Create a new file in `/etc/apache2/sites-available/dnote.conf` with the following contents: - -``` - - ServerName notes.example.com - - ProxyRequests Off - ProxyPreserveHost On - ProxyPass / http://127.0.0.1:3000/ keepalive=On - ProxyPassReverse / http://127.0.0.1:3000/ - RequestHeader set X-Forwarded-HTTPS "0" - -``` - -3. Enable the dnote site and restart the Apache2 service by running the following: - -``` -a2ensite dnote -sudo service apache2 restart -``` - -Now you can access the Dnote frontend application on `/`, and the API on `/api`. - -### Configure TLS by using LetsEncrypt - -It is recommended to use HTTPS. Obtain a certificate using LetsEncrypt and configure TLS in Nginx. - -In the future versions of the Dnote Server, HTTPS will be required at all times. - -### Run Dnote As a Daemon - -We can use `systemd` to run Dnote in the background as a Daemon, and automatically start it on system reboot. - -1. Create a new file at `/etc/systemd/system/dnote.service` with the following content: - -``` -[Unit] -Description=Starts the dnote server -Requires=network.target -After=network.target - -[Service] -Type=simple -User=$user -Restart=always -RestartSec=3 -WorkingDirectory=/home/$user -ExecStart=/usr/local/bin/dnote-server start -Environment=GO_ENV=PRODUCTION -Environment=OnPremise=true -Environment=DBHost=localhost -Environment=DBPort=5432 -Environment=DBName=dnote -Environment=DBUser=$DBUser -Environment=DBPassword=$DBPassword -Environment=DBSkipSSL=true -Environment=WebURL=$WebURL -Environment=SmtpHost= -Environment=SmtpPort= -Environment=SmtpUsername= -Environment=SmtpPassword= - -[Install] -WantedBy=multi-user.target -``` - -Replace `$user`, `$WebURL`, `$DBUser`, and `$DBPassword` with the actual values. - -Optionally, if you would like to send spaced repetitions throught email, populate `SmtpHost`, `SmtpPort`, `SmtpUsername`, and `SmtpPassword`. - -2. Reload the change by running `sudo systemctl daemon-reload`. -3. Enable the Daemon by running `sudo systemctl enable dnote`.` -4. Start the Daemon by running `sudo systemctl start dnote` - -### Configure clients - -Let's configure Dnote clients to connect to the self-hosted web API endpoint. - -#### CLI - -We need to modify the configuration file for the CLI. It should have been generated at `~/.config/dnote/dnoterc` upon running the CLI for the first time. - -The following is an example configuration: - -```yaml -editor: nvim -apiEndpoint: https://api.getdnote.com -``` - -Simply change the value for `apiEndpoint` to a full URL to the self-hosted instance, followed by '/api', and save the configuration file. - -e.g. - -```yaml -editor: nvim -apiEndpoint: my-dnote-server.com/api -``` - -#### Browser extension - -Navigate into the 'Settings' tab and set the values for 'API URL', and 'Web URL'. +Set `apiEndpoint: https://your.server/api` in `~/.config/dnote/dnoterc` to connect your CLI to the server. diff --git a/Vagrantfile b/Vagrantfile deleted file mode 100644 index 65dd3db1..00000000 --- a/Vagrantfile +++ /dev/null @@ -1,20 +0,0 @@ -# -*- mode: ruby -*- - -Vagrant.configure("2") do |config| - config.vm.box = "ubuntu/bionic64" - config.vm.synced_folder '.', '/go/src/github.com/dnote/dnote' - config.vm.network "forwarded_port", guest: 3000, host: 3000 - config.vm.network "forwarded_port", guest: 8080, host: 8080 - config.vm.network "forwarded_port", guest: 5432, host: 5433 - - config.vm.provision 'shell', path: './scripts/vagrant/install_utils.sh' - config.vm.provision 'shell', path: './scripts/vagrant/install_go.sh', privileged: false - config.vm.provision 'shell', path: './scripts/vagrant/install_node.sh', privileged: false - config.vm.provision 'shell', path: './scripts/vagrant/install_postgres.sh', privileged: false - config.vm.provision 'shell', path: './scripts/vagrant/bootstrap.sh', privileged: false - - config.vm.provider "virtualbox" do |v| - v.memory = 4000 - v.cpus = 2 - end -end diff --git a/assets/cli.gif b/assets/cli.gif deleted file mode 100644 index 8925e131..00000000 Binary files a/assets/cli.gif and /dev/null differ diff --git a/assets/devices.png b/assets/devices.png deleted file mode 100644 index 5d40ee10..00000000 Binary files a/assets/devices.png and /dev/null differ diff --git a/go.mod b/go.mod index 649f95ef..48dfc85c 100644 --- a/go.mod +++ b/go.mod @@ -1,47 +1,43 @@ module github.com/dnote/dnote -go 1.17 +go 1.25 require ( - github.com/aymerick/douceur v0.2.0 github.com/dnote/actions v0.2.0 - github.com/dnote/color v1.7.0 - github.com/google/go-cmp v0.5.8 + github.com/fatih/color v1.18.0 + github.com/google/go-cmp v0.7.0 github.com/google/go-github v17.0.0+incompatible - github.com/google/uuid v1.3.0 - github.com/gorilla/csrf v1.7.1 - github.com/gorilla/mux v1.8.0 - github.com/gorilla/schema v1.2.0 - github.com/jinzhu/gorm v1.9.16 - github.com/joho/godotenv v1.4.0 - github.com/lib/pq v1.10.5 - github.com/mattn/go-sqlite3 v1.14.12 + github.com/google/uuid v1.6.0 + github.com/gorilla/csrf v1.7.3 + github.com/gorilla/mux v1.8.1 + github.com/gorilla/schema v1.4.1 + github.com/mattn/go-sqlite3 v1.14.32 github.com/pkg/errors v0.9.1 github.com/radovskyb/watcher v1.0.7 - github.com/robfig/cron v1.2.0 - github.com/rubenv/sql-migrate v1.1.1 - github.com/sergi/go-diff v1.1.0 - github.com/spf13/cobra v1.4.0 - golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122 - golang.org/x/time v0.0.0-20220411224347-583f2d630306 + github.com/sergi/go-diff v1.3.1 + github.com/spf13/cobra v1.10.1 + golang.org/x/crypto v0.45.0 + golang.org/x/time v0.13.0 gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df gopkg.in/yaml.v2 v2.4.0 + gorm.io/driver/sqlite v1.6.0 + gorm.io/gorm v1.30.0 ) require ( - github.com/PuerkitoBio/goquery v1.8.0 // indirect - github.com/andybalholm/cascadia v1.3.1 // indirect - github.com/go-gorp/gorp/v3 v3.0.2 // indirect github.com/google/go-querystring v1.1.0 // indirect - github.com/gorilla/css v1.0.0 // indirect - github.com/gorilla/securecookie v1.1.1 // indirect - github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/gorilla/securecookie v1.1.2 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect - github.com/mattn/go-colorable v0.1.12 // indirect - github.com/mattn/go-isatty v0.0.14 // indirect - github.com/spf13/pflag v1.0.5 // indirect - golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect - golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect - golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/kr/pretty v0.3.1 // indirect + github.com/mattn/go-colorable v0.1.14 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/spf13/pflag v1.0.10 // indirect + github.com/stretchr/testify v1.8.1 // indirect + golang.org/x/sys v0.38.0 // indirect + golang.org/x/term v0.37.0 // indirect + golang.org/x/text v0.31.0 // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect ) diff --git a/go.sum b/go.sum index c9d55c07..8b3c653a 100644 --- a/go.sum +++ b/go.sum @@ -1,725 +1,103 @@ -cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= -cloud.google.com/go v0.44.1/go.mod h1:iSa0KzasP4Uvy3f1mN/7PiObzGgflwredwwASm/v6AU= -cloud.google.com/go v0.44.2/go.mod h1:60680Gw3Yr4ikxnPRS/oxxkBccT6SA1yMk63TGekxKY= -cloud.google.com/go v0.45.1/go.mod h1:RpBamKRgapWJb87xiFSdk4g1CME7QZg3uwTez+TSTjc= -cloud.google.com/go v0.46.3/go.mod h1:a6bKKbmY7er1mI7TEI4lsAkts/mkhTSZK8w33B4RAg0= -cloud.google.com/go v0.50.0/go.mod h1:r9sluTvynVuxRIOHXQEHMFffphuXHOMZMycpNR5e6To= -cloud.google.com/go v0.52.0/go.mod h1:pXajvRH/6o3+F9jDHZWQ5PbGhn+o8w9qiu/CffaVdO4= -cloud.google.com/go v0.53.0/go.mod h1:fp/UouUEsRkN6ryDKNW/Upv/JBKnv6WDthjR6+vze6M= -cloud.google.com/go v0.54.0/go.mod h1:1rq2OEkV3YMf6n/9ZvGWI3GWw0VoqH/1x2nd8Is/bPc= -cloud.google.com/go v0.56.0/go.mod h1:jr7tqZxxKOVYizybht9+26Z/gUq7tiRzu+ACVAMbKVk= -cloud.google.com/go v0.57.0/go.mod h1:oXiQ6Rzq3RAkkY7N6t3TcE6jE+CIBBbA36lwQ1JyzZs= -cloud.google.com/go v0.62.0/go.mod h1:jmCYTdRCQuc1PHIIJ/maLInMho30T/Y0M4hTdTShOYc= -cloud.google.com/go v0.65.0/go.mod h1:O5N8zS7uWy9vkA9vayVHs65eM1ubvY4h553ofrNHObY= -cloud.google.com/go v0.72.0/go.mod h1:M+5Vjvlc2wnp6tjzE102Dw08nGShTscUx2nZMufOKPI= -cloud.google.com/go v0.74.0/go.mod h1:VV1xSbzvo+9QJOxLDaJfTjx5e+MePCpCWwvftOeQmWk= -cloud.google.com/go v0.78.0/go.mod h1:QjdrLG0uq+YwhjoVOLsS1t7TW8fs36kLs4XO5R5ECHg= -cloud.google.com/go v0.79.0/go.mod h1:3bzgcEeQlzbuEAYu4mrWhKqWjmpprinYgKJLgKHnbb8= -cloud.google.com/go v0.81.0/go.mod h1:mk/AM35KwGk/Nm2YSeZbxXdrNK3KZOYHmLkOqC2V6E0= -cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o= -cloud.google.com/go/bigquery v1.3.0/go.mod h1:PjpwJnslEMmckchkHFfq+HTD2DmtT67aNFKH1/VBDHE= -cloud.google.com/go/bigquery v1.4.0/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvftPBK2Dvzc= -cloud.google.com/go/bigquery v1.5.0/go.mod h1:snEHRnqQbz117VIFhE8bmtwIDY80NLUZUMb4Nv6dBIg= -cloud.google.com/go/bigquery v1.7.0/go.mod h1://okPTzCYNXSlb24MZs83e2Do+h+VXtc4gLoIoXIAPc= -cloud.google.com/go/bigquery v1.8.0/go.mod h1:J5hqkt3O0uAFnINi6JXValWIb1v0goeZM77hZzJN/fQ= -cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE= -cloud.google.com/go/datastore v1.1.0/go.mod h1:umbIZjpQpHh4hmRpGhH4tLFup+FVzqBi1b3c64qFpCk= -cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqClKRT5SZwBmk= -cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= -cloud.google.com/go/pubsub v1.1.0/go.mod h1:EwwdRX2sKPjnvnqCa270oGRyludottCI76h+R3AArQw= -cloud.google.com/go/pubsub v1.2.0/go.mod h1:jhfEVHT8odbXTkndysNHCcx0awwzvfOlguIAii9o8iA= -cloud.google.com/go/pubsub v1.3.1/go.mod h1:i+ucay31+CNRpDW4Lu78I4xXG+O1r/MAHgjpRVR+TSU= -cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= -cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0ZeosJ0Rtdos= -cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk= -cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= -cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= -dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= -github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= -github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o= -github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= -github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U= -github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI= -github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= -github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c= -github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA= -github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= -github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= -github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= -github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= -github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= -github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= -github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= -github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM= -github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= -github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= -github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= -github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= -github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= -github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= -github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= -github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= -github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= -github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= -github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= -github.com/denisenkom/go-mssqldb v0.9.0 h1:RSohk2RsiZqLZ0zCjtfn3S4Gp4exhpBWHyQ7D0yGjAk= -github.com/denisenkom/go-mssqldb v0.9.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= github.com/dnote/actions v0.2.0 h1:P1ut2/QRKwfAzIIB374vN9A4IanU94C/payEocvngYo= github.com/dnote/actions v0.2.0/go.mod h1:bBIassLhppVQdbC3iaE92SHBpM1HOVe+xZoAlj9ROxw= -github.com/dnote/color v1.7.0 h1:8/QGLQKSU8/zcWQaHbMyC1hJRkKO/Uu9M89sH76ecHE= -github.com/dnote/color v1.7.0/go.mod h1:75UcP/TH7CNvjQ5pwDumkUS3vkPdGggy7/3fT8MlxHM= -github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= -github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= -github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= -github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po= -github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= -github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= -github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= -github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= -github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= -github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= -github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= -github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= -github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= -github.com/go-gorp/gorp/v3 v3.0.2 h1:ULqJXIekoqMx29FI5ekXXFoH1dT2Vc8UhnRzBg+Emz4= -github.com/go-gorp/gorp/v3 v3.0.2/go.mod h1:BJ3q1ejpV8cVALtcXvXaXyTOlMmJhWDxTmncaR6rwBY= -github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= -github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= -github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= -github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= -github.com/gobuffalo/logger v1.0.6 h1:nnZNpxYo0zx+Aj9RfMPBm+x9zAU2OayFh/xrAWi34HU= -github.com/gobuffalo/logger v1.0.6/go.mod h1:J31TBEHR1QLV2683OXTAItYIg8pv2JMHnF/quuAbMjs= -github.com/gobuffalo/packd v1.0.1 h1:U2wXfRr4E9DH8IdsDLlRFwTZTK7hLfq9qT/QHXGVe/0= -github.com/gobuffalo/packd v1.0.1/go.mod h1:PP2POP3p3RXGz7Jh6eYEf93S7vA2za6xM7QT85L4+VY= -github.com/gobuffalo/packr/v2 v2.8.3 h1:xE1yzvnO56cUC0sTpKR3DIbxZgB54AftTFMhB2XEWlY= -github.com/gobuffalo/packr/v2 v2.8.3/go.mod h1:0SahksCVcx4IMnigTjiFuyldmTrdTctXsOdiU5KwbKc= -github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/godror/godror v0.24.2/go.mod h1:wZv/9vPiUib6tkoDl+AZ/QLf5YZgMravZ7jxH2eQWAE= -github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= -github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= -github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= -github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= -github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= -github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= -github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= -github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= -github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= -github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= -github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= -github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= -github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= -github.com/golang/protobuf v1.3.5/go.mod h1:6O5/vntMXwX2lRkT1hjjk0nAC1IDOTvTlVgjlRvqsdk= -github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= -github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= -github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= -github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= -github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= -github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= -github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.1/go.mod h1:DopwsBzvsk0Fs44TXzsVbJyPhcCPeIwnvohx4u74HPM= -github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= -github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= -github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM= +github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= -github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= -github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= -github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= -github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= -github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= -github.com/google/pprof v0.0.0-20191218002539-d4f498aebedc/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= -github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= -github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= -github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= -github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= -github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= -github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= -github.com/google/pprof v0.0.0-20210122040257-d980be63207e/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= -github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= -github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= -github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= -github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= -github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= -github.com/gorilla/csrf v1.7.1 h1:Ir3o2c1/Uzj6FBxMlAUB6SivgVMy1ONXwYgXn+/aHPE= -github.com/gorilla/csrf v1.7.1/go.mod h1:+a/4tCmqhG6/w4oafeAZ9pEa3/NZOWYVbD9fV0FwIQA= -github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= -github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= -github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= -github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= -github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc= -github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= -github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ= -github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= -github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= -github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q= -github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8= -github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= -github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80= -github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= -github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= -github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= -github.com/hashicorp/go-rootcerts v1.0.0/go.mod h1:K6zTfqpRlCUIjkwsN4Z+hiSfzSTQa6eBIzfwKfwNnHU= -github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU= -github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= -github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= -github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= -github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= -github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= -github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= -github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= -github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= -github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= -github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= -github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= -github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= -github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= -github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o= -github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs= +github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= +github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/csrf v1.7.3 h1:BHWt6FTLZAb2HtWT5KDBf6qgpZzvtbp9QWDRKZMXJC0= +github.com/gorilla/csrf v1.7.3/go.mod h1:F1Fj3KG23WYHE6gozCmBAezKookxbIvUJT+121wTuLk= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +github.com/gorilla/schema v1.4.1 h1:jUg5hUjCSDZpNGLuXQOgIWGdlgrIdYvgQ0wZtdK1M3E= +github.com/gorilla/schema v1.4.1/go.mod h1:Dg5SSm5PV60mhF2NFaTV1xuYYj8tV8NOPRo4FggUMnM= +github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA= +github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= -github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M= -github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= -github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= -github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= -github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= -github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= -github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= -github.com/karrick/godirwalk v1.16.1 h1:DynhcF+bztK8gooS0+NDJFrdNZjJ3gzVzC545UNA9iw= -github.com/karrick/godirwalk v1.16.1/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk= -github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= -github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/kortschak/utter v1.0.1/go.mod h1:vSmSjbyrlKjjsL71193LmzBOKgwePk9DH6uFaWHIInc= -github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.10.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= -github.com/lib/pq v1.10.5 h1:J+gdV2cUmX7ZqL2B0lFcW0m+egaHC2V3lpO8nWxyYiQ= -github.com/lib/pq v1.10.5/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= -github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= -github.com/markbates/errx v1.1.0 h1:QDFeR+UP95dO12JgW+tgi2UVfo0V8YBHiUIOaeBPiEI= -github.com/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc= -github.com/markbates/oncer v1.0.0 h1:E83IaVAHygyndzPimgUYJjbshhDTALZyXxvk9FOlQRY= -github.com/markbates/oncer v1.0.0/go.mod h1:Z59JA581E9GP6w96jai+TGqafHPW+cPfRxz2aSZ0mcI= -github.com/markbates/safe v1.0.1 h1:yjZkbvRM6IzKj9tlu/zMJLS0n/V351OZWRnF3QfaUxI= -github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= -github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= -github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= -github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= -github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= -github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= -github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= -github.com/mattn/go-oci8 v0.1.1/go.mod h1:wjDx6Xm9q7dFtHJvIlrI99JytznLw5wQ4R+9mNXJwGI= -github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= -github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= -github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= -github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= -github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0= -github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= -github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= -github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= -github.com/mitchellh/cli v1.1.2/go.mod h1:6iaV0fGdElS6dPBx0EApTxHrcWvmJphyh2n8YBLPPZ4= -github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= -github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= -github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI= -github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= -github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= -github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= -github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= -github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= -github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= +github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs= +github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= -github.com/poy/onpar v0.0.0-20190519213022-ee068f8ea4d1 h1:oL4IBbcqwhhNWh31bjOX8C/OCy0zs9906d/VUru+bqg= -github.com/poy/onpar v0.0.0-20190519213022-ee068f8ea4d1/go.mod h1:nSbFQvMj97ZyhFRSJYtut+msi4sOY6zJDGCdSc+/rZU= -github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE= github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg= -github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ= -github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k= -github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= -github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= -github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE= -github.com/rubenv/sql-migrate v1.1.1 h1:haR5Hn8hbW9/SpAICrXoZqXnywS7Q5WijwkQENPeNWY= -github.com/rubenv/sql-migrate v1.1.1/go.mod h1:/7TZymwxN8VWumcIxw1jjHEcR1djpdkMHQPT4FWdnbQ= -github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= -github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= -github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= -github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= -github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= -github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= -github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= -github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= -github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= -github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= -github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= -github.com/spf13/cobra v1.2.1/go.mod h1:ExllRjgxM/piMAM+3tAZvg8fsklGAf3tPfi+i8t68Nk= -github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q= -github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= -github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= -github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= -github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns= +github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= +github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= +github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s= +github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0= +github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= +github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= -github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -github.com/ziutek/mymysql v1.5.4 h1:GB0qdRGsTwQSBVYuVShFBKaXSnSnYYC2d9knnE1LHFs= -github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0= -go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= -go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g= -go.etcd.io/etcd/client/v2 v2.305.0/go.mod h1:h9puh54ZTgAKtEbut2oe9P4L/oqKCVB6xsXlzd7alYQ= -go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= -go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= -go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= -go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E= -go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= -go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo= -golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122 h1:NvGWuYG8dkDHFSKksI1P9faiVJ9rayE6l0+ouWVIDs8= -golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= -golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm080zCjGlMRFzhUhsZKEZO7MGek= -golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY= -golang.org/x/exp v0.0.0-20191129062945-2f5052295587/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= -golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= -golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= -golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= -golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= -golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= -golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= -golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= -golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= -golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= -golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20190909230951-414d861bb4ac/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs= -golang.org/x/lint v0.0.0-20200130185559-910be7a94367/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= -golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o= -golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= -golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= -golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= -golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= -golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20200501053045-e0ff5e5a1de5/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= -golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 h1:HVyaeDAYux4pnY+D/SiwmLOR36ewZ4iGQIIrtnuCjFA= -golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= -golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= -golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= -golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= -golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= -golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= -golang.org/x/oauth2 v0.0.0-20210220000619-9bb904979d93/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= -golang.org/x/oauth2 v0.0.0-20210313182246-cd4f82c27b84/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= -golang.org/x/oauth2 v0.0.0-20210402161424-2e8d93401602/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= -golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200331124033-c3d80250170d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200501052902-10377860bb8e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210104204734-6f8348627aad/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210305230114-8fe3ee5dd75b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 h1:nonptSpoQ4vQjyraW20DXPAglgQfVnM9ZC6MmNLMR60= -golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 h1:EH1Deb8WZJ0xc0WK//leUHXcX9aLE5SymusoTmMZye8= -golang.org/x/term v0.0.0-20220411215600-e5f449aeb171/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/time v0.0.0-20220411224347-583f2d630306 h1:+gHMid33q6pen7kv9xvT+JRinntgeXO2AeZVd0AWD3w= -golang.org/x/time v0.0.0-20220411224347-583f2d630306/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= -golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= -golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= -golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191113191852-77e3bb0ad9e7/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191115202509-3a792d9c32b2/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191130070609-6e064ea0cf2d/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191216173652-a0e659d51361/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20191227053925-7b8e75db28f4/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200117161641-43d50277825c/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200122220014-bf1340f18c4a/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200204074204-1cc6d1ef6c74/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200207183749-b753a1ba74fa/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200212150539-ea181f53ac56/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200224181240-023911ca70b2/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200227222343-706bc42d1f0d/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.0.0-20200304193943-95d2e580d8eb/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw= -golang.org/x/tools v0.0.0-20200312045724-11d5b4c81c7d/go.mod h1:o4KQGtdN14AW+yjsvvwRTJJuXz8XRtIHtEnmAXLyFUw= -golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= -golang.org/x/tools v0.0.0-20200501065659-ab2804fb9c9d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20200512131952-2bc93b1c0c88/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20200515010526-7d3b6ebf133d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= -golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= -golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= -golang.org/x/tools v0.0.0-20200904185747-39188db58858/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE= -golang.org/x/tools v0.0.0-20201110124207-079ba7bd75cd/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/tools v0.0.0-20201201161351-ac6f37ff4c2a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/tools v0.0.0-20201208233053-a543418bbed2/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= -golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= -golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q= +golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc= +golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/term v0.37.0 h1:8EGAD0qCmHYZg6J17DvsMy9/wJ7/D/4pV/wfnld5lTU= +golang.org/x/term v0.37.0/go.mod h1:5pB4lxRNYYVZuTLmy8oR2BH8dflOR+IbTYFD8fi3254= +golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM= +golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM= +golang.org/x/time v0.13.0 h1:eUlYslOIt32DgYD6utsuUeHs4d7AsEYLuIAdg7FlYgI= +golang.org/x/time v0.13.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= -google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= -google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= -google.golang.org/api v0.9.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= -google.golang.org/api v0.13.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= -google.golang.org/api v0.14.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= -google.golang.org/api v0.15.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= -google.golang.org/api v0.17.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= -google.golang.org/api v0.18.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= -google.golang.org/api v0.19.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= -google.golang.org/api v0.20.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= -google.golang.org/api v0.22.0/go.mod h1:BwFmGc8tA3vsd7r/7kR8DY7iEEGSU04BFxCo5jP/sfE= -google.golang.org/api v0.24.0/go.mod h1:lIXQywCXRcnZPGlsd8NbLnOjtAoL6em04bJ9+z0MncE= -google.golang.org/api v0.28.0/go.mod h1:lIXQywCXRcnZPGlsd8NbLnOjtAoL6em04bJ9+z0MncE= -google.golang.org/api v0.29.0/go.mod h1:Lcubydp8VUV7KeIHD9z2Bys/sm/vGKnG1UHuDBSrHWM= -google.golang.org/api v0.30.0/go.mod h1:QGmEvQ87FHZNiUVJkT14jQNYJ4ZJjdRF23ZXz5138Fc= -google.golang.org/api v0.35.0/go.mod h1:/XrVsuzM0rZmrsbjJutiuftIzeuTQcEeaYcSk/mQ1dg= -google.golang.org/api v0.36.0/go.mod h1:+z5ficQTmoYpPn8LCUNVpK5I7hwkpjbcgqA7I34qYtE= -google.golang.org/api v0.40.0/go.mod h1:fYKFpnQN0DsDSKRVRcQSDQNtqWPfM9i+zNPxepjRCQ8= -google.golang.org/api v0.41.0/go.mod h1:RkxM5lITDfTzmyKFPt+wGrCJbVfniCr2ool8kTBzRTU= -google.golang.org/api v0.43.0/go.mod h1:nQsDGjRXMo4lvh5hP0TKqF244gqhGcr/YSIykhUk/94= -google.golang.org/api v0.44.0/go.mod h1:EBOGZqzyhtvMDoxwS97ctnh0zUmYY6CxqXsc1AvkYD8= -google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= -google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= -google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= -google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= -google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= -google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= -google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= -google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= -google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= -google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= -google.golang.org/genproto v0.0.0-20190502173448-54afdca5d873/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= -google.golang.org/genproto v0.0.0-20190801165951-fa694d86fc64/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= -google.golang.org/genproto v0.0.0-20190911173649-1774047e7e51/go.mod h1:IbNlFCBrqXvoKpeg0TB2l7cyZUmoaFKYIwrEpbDKLA8= -google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= -google.golang.org/genproto v0.0.0-20191115194625-c23dd37a84c9/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= -google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= -google.golang.org/genproto v0.0.0-20191230161307-f3c370f40bfb/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= -google.golang.org/genproto v0.0.0-20200115191322-ca5a22157cba/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= -google.golang.org/genproto v0.0.0-20200122232147-0452cf42e150/go.mod h1:n3cpQtvxv34hfy77yVDNjmbRyujviMdxYliBSkLhpCc= -google.golang.org/genproto v0.0.0-20200204135345-fa8e72b47b90/go.mod h1:GmwEX6Z4W5gMy59cAlVYjN9JhxgbQH6Gn+gFDQe2lzA= -google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200228133532-8c2c7df3a383/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200305110556-506484158171/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200312145019-da6875a35672/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200331122359-1ee6d9798940/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200430143042-b979b6f78d84/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200515170657-fc4c6c6a6587/go.mod h1:YsZOwe1myG/8QRHRsmBRE1LrgQY60beZKjly0O1fX9U= -google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= -google.golang.org/genproto v0.0.0-20200618031413-b414f8b61790/go.mod h1:jDfRM7FcilCzHH/e9qn6dsT145K34l5v+OpcnNgKAAA= -google.golang.org/genproto v0.0.0-20200729003335-053ba62fc06f/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20200804131852-c06518451d9c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20200825200019-8632dd797987/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20201109203340-2640f1f9cdfb/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20201201144952-b05cb90ed32e/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20201210142538-e3217bee35cc/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20201214200347-8c77b98c765d/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20210222152913-aa3ee6e6a81c/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20210303154014-9728d6b83eeb/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20210310155132-4ce2db91004e/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20210319143718-93e7006c17a6/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto v0.0.0-20210402141018-6c239bbf2bb1/go.mod h1:9lPAdzaEmUacj36I+k7YKbEc5CXzPIeORRgDAUOu28A= -google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c/go.mod h1:UODoCrxHCcBojKKwX1terBiRUaqAsFqJiF615XL43r0= -google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= -google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= -google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= -google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= -google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= -google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.28.0/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKal+60= -google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= -google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= -google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= -google.golang.org/grpc v1.31.1/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= -google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= -google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= -google.golang.org/grpc v1.34.0/go.mod h1:WotjhfgOW/POjDeRt8vscBtXq+2VjORFy659qA51WJ8= -google.golang.org/grpc v1.35.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= -google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= -google.golang.org/grpc v1.36.1/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= -google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM= -google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= -google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= -google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= -google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= -google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= -google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= -google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= -gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= -honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= -honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= -honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= -rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= -rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= -rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ= +gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8= +gorm.io/gorm v1.30.0 h1:qbT5aPv1UH8gI99OsRlvDToLxW5zR7FzS9acZDOZcgs= +gorm.io/gorm v1.30.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE= diff --git a/host/docker/Dockerfile b/host/docker/Dockerfile index e9c949af..71b3d1fa 100644 --- a/host/docker/Dockerfile +++ b/host/docker/Dockerfile @@ -1,20 +1,40 @@ -FROM alpine:latest +FROM busybox:glibc -ARG tarballName -RUN test -n "$tarballName" +ARG TARGETPLATFORM +ARG version -# add dependency to execute a golang binary with dynamical linking. -RUN apk add --no-cache \ - libc6-compat +RUN test -n "$TARGETPLATFORM" || (echo "TARGETPLATFORM is required" && exit 1) +RUN test -n "$version" || (echo "version is required" && exit 1) -WORKDIR dnote +WORKDIR /tmp/tarballs -COPY "$tarballName" . -RUN tar -xvzf "$tarballName" +# Copy all architecture tarballs +COPY dnote_server_*.tar.gz ./ + +# Select and extract the correct tarball based on target platform +RUN case "$TARGETPLATFORM" in \ + "linux/amd64") ARCH="amd64" ;; \ + "linux/arm64") ARCH="arm64" ;; \ + "linux/arm/v7") ARCH="arm" ;; \ + "linux/386") ARCH="386" ;; \ + *) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \ + esac && \ + TARBALL="dnote_server_${version}_linux_${ARCH}.tar.gz" && \ + echo "Extracting $TARBALL for $TARGETPLATFORM" && \ + mkdir -p /dnote && \ + tar -xvzf "$TARBALL" -C /dnote + +WORKDIR /dnote + +# Set default database path for all processes (main server, docker exec, shells) +ENV DBPath=/data/dnote.db COPY entrypoint.sh . ENTRYPOINT ["./entrypoint.sh"] CMD ./dnote-server start -EXPOSE 3000 +EXPOSE 3001 + +HEALTHCHECK --interval=30s --timeout=3s --start-period=10s \ + CMD wget --no-verbose --tries=1 -O /dev/null http://localhost:3001/health || exit 1 diff --git a/host/docker/README.md b/host/docker/README.md deleted file mode 100644 index 95c5a469..00000000 --- a/host/docker/README.md +++ /dev/null @@ -1,30 +0,0 @@ -# Dnote Docker Image - -The official Dnote docker image. - -## Installing Dnote Server Using Docker - -1. Install [Docker](https://docs.docker.com/install/). -2. Install Docker [Compose](https://docs.docker.com/compose/install/). -3. Download the [docker-compose.yml](https://raw.githubusercontent.com/dnote/dnote/master/host/docker/docker-compose.yml) file by running: - -``` -curl https://raw.githubusercontent.com/dnote/dnote/master/host/docker/docker-compose.yml > docker-compose.yml -``` - -4. Run the following to download the images and run the containers - -``` -docker-compose pull -docker-compose up -d -``` - -Visit http://localhost:3000 in your browser to see Dnote running. - -Please see [the installation guide](https://github.com/dnote/dnote/blob/master/SELF_HOSTING.md) for further configuration. - -## Supported platform - -Currently, the official Docker image for Dnote supports Linux running AMD64 CPU architecture. - -If you run ARM64, please install Dnote server by downloading a binary distribution by following [the guide](https://github.com/dnote/dnote/blob/master/SELF_HOSTING.md). diff --git a/host/docker/build.sh b/host/docker/build.sh index 3ea9c143..add55578 100755 --- a/host/docker/build.sh +++ b/host/docker/build.sh @@ -1,13 +1,85 @@ #!/usr/bin/env bash +# Build Docker image for local testing +# +# Usage: +# # builds for host platform (auto-detected) +# ./build.sh 1.0.0 +# +# # builds arm64 +# ./build.sh 1.0.0 linux/arm64 +# +# # builds multiple platforms +# ./build.sh 1.0.0 "linux/amd64,linux/arm64,linux/arm/v7,linux/386" set -eux version=$1 +# Detect host platform if not specified +if [ -z "${2:-}" ]; then + HOST_ARCH=$(uname -m) + case "$HOST_ARCH" in + x86_64) platform="linux/amd64" ;; + aarch64|arm64) platform="linux/arm64" ;; + armv7l) platform="linux/arm/v7" ;; + i386|i686) platform="linux/386" ;; + *) + echo "Warning: Unsupported architecture: $HOST_ARCH, defaulting to linux/amd64" + platform="linux/amd64" + ;; + esac + echo "Auto-detected platform: $platform" +else + platform=$2 +fi + dir=$(dirname "${BASH_SOURCE[0]}") projectDir="$dir/../.." -tarballName="dnote_server_${version}_linux_amd64.tar.gz" -# copy over the build artifact to the Docker build context -cp "$projectDir/build/server/$tarballName" "$dir" +# Copy all Linux tarballs to Docker build context +cp "$projectDir/build/server/dnote_server_${version}_linux_amd64.tar.gz" "$dir/" +cp "$projectDir/build/server/dnote_server_${version}_linux_arm64.tar.gz" "$dir/" +cp "$projectDir/build/server/dnote_server_${version}_linux_arm.tar.gz" "$dir/" +cp "$projectDir/build/server/dnote_server_${version}_linux_386.tar.gz" "$dir/" -docker build --network=host -t dnote/dnote:"$version" --build-arg tarballName="$tarballName" . +# Count platforms (check for comma) +if [[ "$platform" == *","* ]]; then + echo "Building for multiple platforms: $platform" + + # Check if multiarch builder exists, create if not + if ! docker buildx ls | grep -q "multiarch"; then + echo "Creating multiarch builder for multi-platform builds..." + docker buildx create --name multiarch --use + docker buildx inspect --bootstrap + else + echo "Using existing multiarch builder" + docker buildx use multiarch + fi + echo "" + + docker buildx build \ + --platform "$platform" \ + -t dnote/dnote:"$version" \ + -t dnote/dnote:latest \ + --build-arg version="$version" \ + "$dir" + + # Switch back to default builder + docker buildx use default +else + echo "Building for single platform: $platform" + echo "Image will be loaded to local Docker daemon" + + docker buildx build \ + --platform "$platform" \ + -t dnote/dnote:"$version" \ + -t dnote/dnote:latest \ + --build-arg version="$version" \ + --load \ + "$dir" +fi + +echo "" +echo "Build complete!" +if [[ "$platform" != *","* ]]; then + echo "Test with: docker run --rm dnote/dnote:$version ./dnote-server version" +fi diff --git a/host/docker/compose.yml b/host/docker/compose.yml new file mode 100644 index 00000000..2869033a --- /dev/null +++ b/host/docker/compose.yml @@ -0,0 +1,9 @@ +services: + dnote: + image: dnote/dnote:latest + container_name: dnote + ports: + - 3001:3001 + volumes: + - ./dnote_data:/data + restart: unless-stopped diff --git a/host/docker/docker-compose.yml b/host/docker/docker-compose.yml deleted file mode 100644 index c10efe37..00000000 --- a/host/docker/docker-compose.yml +++ /dev/null @@ -1,35 +0,0 @@ -version: "3" - -services: - postgres: - image: postgres:14-alpine - environment: - POSTGRES_USER: dnote - POSTGRES_PASSWORD: dnote - POSTGRES_DB: dnote - volumes: - - ./dnote_data:/var/lib/postgresql/data - restart: always - - dnote: - image: dnote/dnote:latest - environment: - GO_ENV: PRODUCTION - DBSkipSSL: "true" - DBHost: postgres - DBPort: 5432 - DBName: dnote - DBUser: dnote - DBPassword: dnote - WebURL: localhost:3000 - OnPremise: "true" - SmtpHost: - SmtpPort: - SmtpUsername: - SmtpPassword: - DisableRegistration: "false" - ports: - - 3000:3000 - depends_on: - - postgres - restart: always diff --git a/host/docker/entrypoint.sh b/host/docker/entrypoint.sh index 8fb62de9..0fee185c 100755 --- a/host/docker/entrypoint.sh +++ b/host/docker/entrypoint.sh @@ -1,25 +1,6 @@ #!/bin/sh -wait_for_db() { - HOST=${DBHost:-postgres} - PORT=${DBPort:-5432} - echo "Waiting for the database connection..." - - attempts=0 - max_attempts=10 - while [ $attempts -lt $max_attempts ]; do - nc -z "${HOST}" "${PORT}" 2>/dev/null && break - echo "Waiting for db at ${HOST}:${PORT}..." - sleep 5 - attempts=$((attempts+1)) - done - - if [ $attempts -eq $max_attempts ]; then - echo "Timed out while waiting for db at ${HOST}:${PORT}" - exit 1 - fi -} - -wait_for_db +# Set default DBPath to /data if not specified +export DBPath=${DBPath:-/data/dnote.db} exec "$@" diff --git a/host/docker/release.sh b/host/docker/release.sh deleted file mode 100755 index 4c2be4af..00000000 --- a/host/docker/release.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env bash -set -eux - -version=$1 - -docker login - -# tag the release -docker tag dnote/dnote:"$version" dnote/dnote:latest - -# publish -docker push dnote/dnote:"$version" -docker push dnote/dnote:latest diff --git a/host/smoketest/.gitignore b/host/smoketest/.gitignore deleted file mode 100644 index 463ebfd4..00000000 --- a/host/smoketest/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/volume diff --git a/host/smoketest/README.md b/host/smoketest/README.md deleted file mode 100644 index 9025f533..00000000 --- a/host/smoketest/README.md +++ /dev/null @@ -1 +0,0 @@ -This directory contains a smoke test for running a self-hosted instance using a virtual machine. diff --git a/host/smoketest/Vagrantfile b/host/smoketest/Vagrantfile deleted file mode 100644 index 54b28fcf..00000000 --- a/host/smoketest/Vagrantfile +++ /dev/null @@ -1,9 +0,0 @@ -# -*- mode: ruby -*- - -Vagrant.configure("2") do |config| - config.vm.box = "ubuntu/bionic64" - config.vm.synced_folder './volume', '/vagrant' - config.vm.network "forwarded_port", guest: 2300, host: 2300 - - config.vm.provision 'shell', path: './setup.sh', privileged: false -end diff --git a/host/smoketest/run_test.sh b/host/smoketest/run_test.sh deleted file mode 100755 index 8137dfcd..00000000 --- a/host/smoketest/run_test.sh +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env bash -# run_test.sh builds a fresh server image, and mounts it on a fresh -# virtual machine and runs a smoke test. If a tarball path is not provided, -# this script builds a new version and uses it. -set -ex - -# tarballPath is an absolute path to a release tarball containing the dnote server. -tarballPath=$1 - -dir=$(dirname "${BASH_SOURCE[0]}") -projectDir="$dir/../.." - -# build -if [ -z "$tarballPath" ]; then - pushd "$projectDir" - make version=integration_test build-server - popd - tarballPath="$projectDir/build/server/dnote_server_integration_test_linux_amd64.tar.gz" -fi - -pushd "$dir" - -# start a virtual machine -volume="$dir/volume" -rm -rf "$volume" -mkdir -p "$volume" -cp "$tarballPath" "$volume" -cp "$dir/testsuite.sh" "$volume" - -vagrant up - -# run tests -set +e -if ! vagrant ssh -c "/vagrant/testsuite.sh"; then - echo "Test failed. Please see the output." - vagrant halt - exit 1 -fi -set -e - -vagrant halt -popd diff --git a/host/smoketest/setup.sh b/host/smoketest/setup.sh deleted file mode 100755 index 589524ee..00000000 --- a/host/smoketest/setup.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env bash -set -ex - -sudo apt-get install wget ca-certificates -wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - -sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list' - -sudo apt-get update -sudo apt-get install -y postgresql-14 - -# set up database -sudo -u postgres createdb dnote -# allow connection from host and allow to connect without password -sudo sed -i "/port*/a listen_addresses = '*'" /etc/postgresql/14/main/postgresql.conf -sudo sed -i 's/host.*all.*.all.*md5/# &/' /etc/postgresql/14/main/pg_hba.conf -sudo sed -i "$ a host all all all trust" /etc/postgresql/14/main/pg_hba.conf -sudo service postgresql restart diff --git a/host/smoketest/testsuite.sh b/host/smoketest/testsuite.sh deleted file mode 100755 index 83b5e4b0..00000000 --- a/host/smoketest/testsuite.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env bash -# testsuite.sh runs the smoke tests for a self-hosted instance. -# It is meant to be run inside a virtual machine which has been -# set up by an entry script. -set -eu - -echo 'Running a smoke test' - -sudo -u postgres dropdb dnote -sudo -u postgres createdb dnote - -cd /vagrant - -tar -xvf dnote_server_integration_test_linux_amd64.tar.gz - -GO_ENV=PRODUCTION \ - DBHost=localhost \ - DBPort=5432 \ - DBName=dnote \ - DBUser=postgres \ - DBPassword="" \ - WebURL=localhost:3000 \ - ./dnote-server -port 2300 start & sleep 3 - -assert_http_status() { - url=$1 - expected=$2 - - echo "======== [TEST CASE] asserting response status code for $url ========" - - got=$(curl --write-out %"{http_code}" --silent --output /dev/null "$url") - - if [ "$got" != "$expected" ]; then - echo "======== ASSERTION FAILED ========" - echo "status code for $url: expected: $expected got: $got" - echo "==================================" - exit 1 - fi -} - -assert_http_status http://localhost:2300 "302" -assert_http_status http://localhost:2300/health "200" - -echo "======== [SUCCESS] TEST PASSED! ========" diff --git a/install.sh b/install.sh index 6537232e..bb09bd8f 100755 --- a/install.sh +++ b/install.sh @@ -68,9 +68,14 @@ uname_os() { uname_arch() { arch=$(uname -m) - case $arch in + case $arch in x86_64) arch="amd64" ;; aarch64) arch="arm64" ;; + arm64) arch="arm64" ;; + armv7l) arch="arm" ;; + armv6l) arch="arm" ;; + armv5l) arch="arm" ;; + arm) arch="arm" ;; x86) arch="386" ;; i686) arch="386" ;; i386) arch="386" ;; @@ -86,9 +91,17 @@ check_platform() { found=1 case "$platform" in - darwin/amd64) found=0;; + # Linux linux/amd64) found=0 ;; linux/arm64) found=0 ;; + linux/arm) found=0 ;; + # macOS + darwin/amd64) found=0 ;; + darwin/arm64) found=0 ;; + # Windows + windows/amd64) found=0 ;; + # FreeBSD + freebsd/amd64) found=0 ;; esac return $found diff --git a/licenses/AGPLv3.txt b/licenses/AGPLv3.txt deleted file mode 100644 index 0ad25db4..00000000 --- a/licenses/AGPLv3.txt +++ /dev/null @@ -1,661 +0,0 @@ - GNU AFFERO GENERAL PUBLIC LICENSE - Version 3, 19 November 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU Affero General Public License is a free, copyleft license for -software and other kinds of works, specifically designed to ensure -cooperation with the community in the case of network server software. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -our General Public Licenses are intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - Developers that use our General Public Licenses protect your rights -with two steps: (1) assert copyright on the software, and (2) offer -you this License which gives you legal permission to copy, distribute -and/or modify the software. - - A secondary benefit of defending all users' freedom is that -improvements made in alternate versions of the program, if they -receive widespread use, become available for other developers to -incorporate. Many developers of free software are heartened and -encouraged by the resulting cooperation. However, in the case of -software used on network servers, this result may fail to come about. -The GNU General Public License permits making a modified version and -letting the public access it on a server without ever releasing its -source code to the public. - - The GNU Affero General Public License is designed specifically to -ensure that, in such cases, the modified source code becomes available -to the community. It requires the operator of a network server to -provide the source code of the modified version running there to the -users of that server. Therefore, public use of a modified version, on -a publicly accessible server, gives the public access to the source -code of the modified version. - - An older license, called the Affero General Public License and -published by Affero, was designed to accomplish similar goals. This is -a different license, not a version of the Affero GPL, but Affero has -released a new version of the Affero GPL which permits relicensing under -this license. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU Affero General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Remote Network Interaction; Use with the GNU General Public License. - - Notwithstanding any other provision of this License, if you modify the -Program, your modified version must prominently offer all users -interacting with it remotely through a computer network (if your version -supports such interaction) an opportunity to receive the Corresponding -Source of your version by providing access to the Corresponding Source -from a network server at no charge, through some standard or customary -means of facilitating copying of software. This Corresponding Source -shall include the Corresponding Source for any work covered by version 3 -of the GNU General Public License that is incorporated pursuant to the -following paragraph. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the work with which it is combined will remain governed by version -3 of the GNU General Public License. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU Affero General Public License from time to time. Such new versions -will be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU Affero General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU Affero General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU Affero General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If your software can interact with users remotely through a computer -network, you should also make sure that it provides a way for users to -get its source. For example, if your program is a web application, its -interface could display a "Source" link that leads users to an archive -of the code. There are many ways you could offer source, and different -solutions will be better for different programs; see section 13 for the -specific requirements. - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU AGPL, see -. diff --git a/licenses/GPLv3.txt b/licenses/GPLv3.txt deleted file mode 100644 index f288702d..00000000 --- a/licenses/GPLv3.txt +++ /dev/null @@ -1,674 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU General Public License is a free, copyleft license for -software and other kinds of works. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - To protect your rights, we need to prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must pass on to the recipients the same -freedoms that you received. You must make sure that they, too, receive -or can get the source code. And you must show them these terms so they -know their rights. - - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. - - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. - - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Use with the GNU Affero General Public License. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. diff --git a/pkg/assert/assert.go b/pkg/assert/assert.go index 6af4da34..bb98fce0 100644 --- a/pkg/assert/assert.go +++ b/pkg/assert/assert.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package assert provides functions to assert a condition in tests @@ -22,7 +19,7 @@ package assert import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "reflect" "runtime/debug" @@ -138,7 +135,7 @@ func EqualJSON(t *testing.T, a, b, message string) { // expected func StatusCodeEquals(t *testing.T, res *http.Response, expected int, message string) { if res.StatusCode != expected { - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { t.Fatal(errors.Wrap(err, "reading body")) } diff --git a/pkg/assert/prompt.go b/pkg/assert/prompt.go new file mode 100644 index 00000000..c21e6a42 --- /dev/null +++ b/pkg/assert/prompt.go @@ -0,0 +1,84 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package assert + +import ( + "bufio" + "io" + "strings" + "time" + + "github.com/pkg/errors" +) + +// WaitForPrompt waits for an expected prompt to appear in stdout with a timeout. +// Returns an error if the prompt is not found within the timeout period. +// Handles prompts with or without newlines by reading character by character. +func WaitForPrompt(stdout io.Reader, expectedPrompt string, timeout time.Duration) error { + type result struct { + found bool + err error + } + resultCh := make(chan result, 1) + + go func() { + reader := bufio.NewReader(stdout) + var buffer strings.Builder + found := false + + for { + b, err := reader.ReadByte() + if err != nil { + resultCh <- result{found: found, err: err} + return + } + + buffer.WriteByte(b) + if strings.Contains(buffer.String(), expectedPrompt) { + found = true + break + } + } + + resultCh <- result{found: found, err: nil} + }() + + select { + case res := <-resultCh: + if res.err != nil && res.err != io.EOF { + return errors.Wrap(res.err, "reading stdout") + } + if !res.found { + return errors.Errorf("expected prompt '%s' not found in stdout", expectedPrompt) + } + return nil + case <-time.After(timeout): + return errors.Errorf("timeout waiting for prompt '%s'", expectedPrompt) + } +} + +// RespondToPrompt is a helper that waits for a prompt and sends a response. +func RespondToPrompt(stdout io.Reader, stdin io.WriteCloser, expectedPrompt, response string, timeout time.Duration) error { + if err := WaitForPrompt(stdout, expectedPrompt, timeout); err != nil { + return err + } + + if _, err := io.WriteString(stdin, response); err != nil { + return errors.Wrap(err, "writing response to stdin") + } + + return nil +} diff --git a/pkg/cli/COMMANDS.md b/pkg/cli/COMMANDS.md index 1bdcadb6..c3402152 100644 --- a/pkg/cli/COMMANDS.md +++ b/pkg/cli/COMMANDS.md @@ -94,20 +94,14 @@ dnote find "merge sort" -b algorithm ## dnote sync -_Dnote Pro only_ - _alias: s_ -Sync notes with Dnote server. All your data is encrypted before being sent to the server. +Sync notes with Dnote server. ## dnote login -_Dnote Pro only_ - Start a login prompt. ## dnote logout -_Dnote Pro only_ - Log out of Dnote. diff --git a/pkg/cli/client/client.go b/pkg/cli/client/client.go index 701e5dd3..2be0006d 100644 --- a/pkg/cli/client/client.go +++ b/pkg/cli/client/client.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package client provides interfaces for interacting with the Dnote server @@ -23,7 +20,7 @@ package client import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strconv" @@ -33,6 +30,7 @@ import ( "github.com/dnote/dnote/pkg/cli/context" "github.com/dnote/dnote/pkg/cli/log" "github.com/pkg/errors" + "golang.org/x/time/rate" ) // ErrInvalidLogin is an error for invalid credentials for login @@ -41,18 +39,84 @@ var ErrInvalidLogin = errors.New("wrong credentials") // ErrContentTypeMismatch is an error for invalid credentials for login var ErrContentTypeMismatch = errors.New("content type mismatch") +// HTTPError represents an HTTP error response from the server +type HTTPError struct { + StatusCode int + Message string +} + +func (e *HTTPError) Error() string { + return fmt.Sprintf(`response %d "%s"`, e.StatusCode, e.Message) +} + +// IsConflict returns true if the error is a 409 Conflict error +func (e *HTTPError) IsConflict() bool { + return e.StatusCode == 409 +} + var contentTypeApplicationJSON = "application/json" var contentTypeNone = "" -// requestOptions contians options for requests +// requestOptions contains options for requests type requestOptions struct { HTTPClient *http.Client // ExpectedContentType is the Content-Type that the client is expecting from the server ExpectedContentType *string } -var defaultRequestOptions = requestOptions{ - ExpectedContentType: &contentTypeApplicationJSON, +const ( + // clientRateLimitPerSecond is the max requests per second the client will make + clientRateLimitPerSecond = 50 + // clientRateLimitBurst is the burst capacity for rate limiting + clientRateLimitBurst = 100 +) + +// rateLimitedTransport wraps an http.RoundTripper with rate limiting +type rateLimitedTransport struct { + transport http.RoundTripper + limiter *rate.Limiter +} + +func (t *rateLimitedTransport) RoundTrip(req *http.Request) (*http.Response, error) { + // Wait for rate limiter to allow the request + if err := t.limiter.Wait(req.Context()); err != nil { + return nil, err + } + return t.transport.RoundTrip(req) +} + +// NewRateLimitedHTTPClient creates an HTTP client with rate limiting +func NewRateLimitedHTTPClient() *http.Client { + // Calculate interval from rate: 1 second / requests per second + interval := time.Second / time.Duration(clientRateLimitPerSecond) + + transport := &rateLimitedTransport{ + transport: http.DefaultTransport, + limiter: rate.NewLimiter(rate.Every(interval), clientRateLimitBurst), + } + return &http.Client{ + Transport: transport, + } +} + +func getHTTPClient(ctx context.DnoteCtx, options *requestOptions) *http.Client { + if options != nil && options.HTTPClient != nil { + return options.HTTPClient + } + + if ctx.HTTPClient != nil { + return ctx.HTTPClient + } + + return &http.Client{} +} + +func getExpectedContentType(options *requestOptions) string { + if options != nil && options.ExpectedContentType != nil { + return *options.ExpectedContentType + } + + return contentTypeApplicationJSON } func getReq(ctx context.DnoteCtx, path, method, body string) (*http.Request, error) { @@ -72,22 +136,6 @@ func getReq(ctx context.DnoteCtx, path, method, body string) (*http.Request, err return req, nil } -func getHTTPClient(options *requestOptions) http.Client { - if options != nil && options.HTTPClient != nil { - return *options.HTTPClient - } - - return http.Client{} -} - -func getExpectedContentType(options *requestOptions) string { - if options != nil && options.ExpectedContentType != nil { - return *options.ExpectedContentType - } - - return contentTypeApplicationJSON -} - // checkRespErr checks if the given http response indicates an error. It returns a boolean indicating // if the response is an error, and a decoded error message. func checkRespErr(res *http.Response) error { @@ -95,13 +143,16 @@ func checkRespErr(res *http.Response) error { return nil } - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { return errors.Wrapf(err, "server responded with %d but client could not read the response body", res.StatusCode) } bodyStr := string(body) - return errors.Errorf(`response %d "%s"`, res.StatusCode, strings.TrimRight(bodyStr, "\n")) + return &HTTPError{ + StatusCode: res.StatusCode, + Message: strings.TrimRight(bodyStr, "\n"), + } } func checkContentType(res *http.Response, options *requestOptions) error { @@ -122,15 +173,15 @@ func doReq(ctx context.DnoteCtx, method, path, body string, options *requestOpti return nil, errors.Wrap(err, "getting request") } - log.Debug("HTTP request: %+v\n", req) + log.Debug("HTTP %s %s\n", method, path) - hc := getHTTPClient(options) + hc := getHTTPClient(ctx, options) res, err := hc.Do(req) if err != nil { return res, errors.Wrap(err, "making http request") } - log.Debug("HTTP response: %+v\n", res) + log.Debug("HTTP %d %s\n", res.StatusCode, res.Status) if err = checkRespErr(res); err != nil { return res, errors.Wrap(err, "server responded with an error") @@ -169,7 +220,7 @@ func GetSyncState(ctx context.DnoteCtx) (GetSyncStateResp, error) { return ret, errors.Wrap(err, "constructing http request") } - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { return ret, errors.Wrap(err, "reading the response body") } @@ -192,7 +243,6 @@ type SyncFragNote struct { AddedOn int64 `json:"added_on"` EditedOn int64 `json:"edited_on"` Body string `json:"content"` - Public bool `json:"public"` Deleted bool `json:"deleted"` } @@ -232,8 +282,11 @@ func GetSyncFragment(ctx context.DnoteCtx, afterUSN int) (GetSyncFragmentResp, e path := fmt.Sprintf("/v3/sync/fragment?%s", queryStr) res, err := doAuthorizedReq(ctx, "GET", path, "", nil) + if err != nil { + return GetSyncFragmentResp{}, errors.Wrap(err, "making the request") + } - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { return GetSyncFragmentResp{}, errors.Wrap(err, "reading the response body") } @@ -404,7 +457,6 @@ func CreateNote(ctx context.DnoteCtx, bookUUID, content string) (CreateNoteResp, type updateNotePayload struct { BookUUID *string `json:"book_uuid"` Body *string `json:"content"` - Public *bool `json:"public"` } // UpdateNoteResp is the response from create book api @@ -414,11 +466,10 @@ type UpdateNoteResp struct { } // UpdateNote updates a note in the server -func UpdateNote(ctx context.DnoteCtx, uuid, bookUUID, content string, public bool) (UpdateNoteResp, error) { +func UpdateNote(ctx context.DnoteCtx, uuid, bookUUID, content string) (UpdateNoteResp, error) { payload := updateNotePayload{ BookUUID: &bookUUID, Body: &content, - Public: &public, } b, err := json.Marshal(payload) if err != nil { @@ -525,10 +576,12 @@ func Signin(ctx context.DnoteCtx, email, password string) (SigninResponse, error return SigninResponse{}, errors.Wrap(err, "marshaling payload") } res, err := doReq(ctx, "POST", "/v3/signin", string(b), nil) - - if res.StatusCode == http.StatusUnauthorized { - return SigninResponse{}, ErrInvalidLogin - } else if err != nil { + if err != nil { + // Check if this is a 401 Unauthorized error + var httpErr *HTTPError + if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusUnauthorized { + return SigninResponse{}, ErrInvalidLogin + } return SigninResponse{}, errors.Wrap(err, "making http request") } @@ -542,15 +595,27 @@ func Signin(ctx context.DnoteCtx, email, password string) (SigninResponse, error // Signout deletes a user session on the server side func Signout(ctx context.DnoteCtx, sessionKey string) error { - hc := http.Client{ - // No need to follow redirect - CheckRedirect: func(req *http.Request, via []*http.Request) error { - return http.ErrUseLastResponse - }, + // Create a client that shares the transport (and thus rate limiter) from ctx.HTTPClient + // but doesn't follow redirects + var hc *http.Client + if ctx.HTTPClient != nil { + hc = &http.Client{ + Transport: ctx.HTTPClient.Transport, + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, + } + } else { + log.Warnf("No HTTP client configured for signout - falling back\n") + hc = &http.Client{ + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, + } } opts := requestOptions{ - HTTPClient: &hc, + HTTPClient: hc, ExpectedContentType: &contentTypeNone, } _, err := doAuthorizedReq(ctx, "POST", "/v3/signout", "", &opts) diff --git a/pkg/cli/client/client_test.go b/pkg/cli/client/client_test.go index 10ea6f37..f83a0e6f 100644 --- a/pkg/cli/client/client_test.go +++ b/pkg/cli/client/client_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package client @@ -23,12 +20,15 @@ import ( "fmt" "net/http" "net/http/httptest" + "sync/atomic" "testing" + "time" "github.com/dnote/dnote/pkg/assert" "github.com/dnote/dnote/pkg/cli/context" "github.com/dnote/dnote/pkg/cli/testutils" "github.com/pkg/errors" + "golang.org/x/time/rate" ) // startCommonTestServer starts a test HTTP server that simulates a common set of senarios @@ -57,7 +57,7 @@ func TestSignIn(t *testing.T) { err := json.NewDecoder(r.Body).Decode(&payload) if err != nil { - t.Fatalf(errors.Wrap(err, "decoding payload in the test server").Error()) + t.Fatal(errors.Wrap(err, "decoding payload in the test server").Error()) return } @@ -82,9 +82,10 @@ func TestSignIn(t *testing.T) { defer commonTs.Close() correctEndpoint := fmt.Sprintf("%s/api", ts.URL) + testClient := NewRateLimitedHTTPClient() t.Run("success", func(t *testing.T) { - result, err := Signin(context.DnoteCtx{APIEndpoint: correctEndpoint}, "alice@example.com", "pass1234") + result, err := Signin(context.DnoteCtx{APIEndpoint: correctEndpoint, HTTPClient: testClient}, "alice@example.com", "pass1234") if err != nil { t.Errorf("got signin request error: %+v", err.Error()) } @@ -94,7 +95,7 @@ func TestSignIn(t *testing.T) { }) t.Run("failure", func(t *testing.T) { - result, err := Signin(context.DnoteCtx{APIEndpoint: correctEndpoint}, "alice@example.com", "incorrectpassword") + result, err := Signin(context.DnoteCtx{APIEndpoint: correctEndpoint, HTTPClient: testClient}, "alice@example.com", "incorrectpassword") assert.Equal(t, err, ErrInvalidLogin, "err mismatch") assert.Equal(t, result.Key, "", "Key mismatch") @@ -103,7 +104,7 @@ func TestSignIn(t *testing.T) { t.Run("server error", func(t *testing.T) { endpoint := fmt.Sprintf("%s/bad-api", ts.URL) - result, err := Signin(context.DnoteCtx{APIEndpoint: endpoint}, "alice@example.com", "pass1234") + result, err := Signin(context.DnoteCtx{APIEndpoint: endpoint, HTTPClient: testClient}, "alice@example.com", "pass1234") if err == nil { t.Error("error should have been returned") } @@ -114,12 +115,24 @@ func TestSignIn(t *testing.T) { t.Run("accidentally pointing to a catch-all handler", func(t *testing.T) { endpoint := fmt.Sprintf("%s", ts.URL) - result, err := Signin(context.DnoteCtx{APIEndpoint: endpoint}, "alice@example.com", "pass1234") + result, err := Signin(context.DnoteCtx{APIEndpoint: endpoint, HTTPClient: testClient}, "alice@example.com", "pass1234") assert.Equal(t, errors.Cause(err), ErrContentTypeMismatch, "error cause mismatch") assert.Equal(t, result.Key, "", "Key mismatch") assert.Equal(t, result.ExpiresAt, int64(0), "ExpiresAt mismatch") }) + + t.Run("network error", func(t *testing.T) { + // Use an invalid endpoint that will fail to connect + endpoint := "http://localhost:99999/api" + result, err := Signin(context.DnoteCtx{APIEndpoint: endpoint, HTTPClient: testClient}, "alice@example.com", "pass1234") + + if err == nil { + t.Error("error should have been returned for network failure") + } + assert.Equal(t, result.Key, "", "Key mismatch") + assert.Equal(t, result.ExpiresAt, int64(0), "ExpiresAt mismatch") + }) } func TestSignOut(t *testing.T) { @@ -134,17 +147,18 @@ func TestSignOut(t *testing.T) { defer commonTs.Close() correctEndpoint := fmt.Sprintf("%s/api", ts.URL) + testClient := NewRateLimitedHTTPClient() t.Run("success", func(t *testing.T) { - err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: correctEndpoint}, "alice@example.com") + err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: correctEndpoint, HTTPClient: testClient}, "alice@example.com") if err != nil { - t.Errorf("got signin request error: %+v", err.Error()) + t.Errorf("got signout request error: %+v", err.Error()) } }) t.Run("server error", func(t *testing.T) { endpoint := fmt.Sprintf("%s/bad-api", commonTs.URL) - err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: endpoint}, "alice@example.com") + err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: endpoint, HTTPClient: testClient}, "alice@example.com") if err == nil { t.Error("error should have been returned") } @@ -152,8 +166,69 @@ func TestSignOut(t *testing.T) { t.Run("accidentally pointing to a catch-all handler", func(t *testing.T) { endpoint := fmt.Sprintf("%s", commonTs.URL) - err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: endpoint}, "alice@example.com") + err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: endpoint, HTTPClient: testClient}, "alice@example.com") assert.Equal(t, errors.Cause(err), ErrContentTypeMismatch, "error cause mismatch") }) + + // Gracefully handle a case where http client was not initialized in the context. + t.Run("nil HTTPClient", func(t *testing.T) { + err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: correctEndpoint}, "alice@example.com") + if err != nil { + t.Errorf("got signout request error: %+v", err.Error()) + } + }) +} + +func TestRateLimitedTransport(t *testing.T) { + var requestCount atomic.Int32 + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requestCount.Add(1) + w.WriteHeader(http.StatusOK) + })) + defer ts.Close() + + transport := &rateLimitedTransport{ + transport: http.DefaultTransport, + limiter: rate.NewLimiter(10, 5), + } + client := &http.Client{Transport: transport} + + // Make 10 requests + start := time.Now() + numRequests := 10 + for i := range numRequests { + req, _ := http.NewRequest("GET", ts.URL, nil) + resp, err := client.Do(req) + if err != nil { + t.Fatalf("Request %d failed: %v", i, err) + } + resp.Body.Close() + } + elapsed := time.Since(start) + + // Burst of 5, then 5 more at 10 req/s = 500ms minimum + if elapsed < 500*time.Millisecond { + t.Errorf("Rate limit not enforced: 10 requests took %v, expected >= 500ms", elapsed) + } + + assert.Equal(t, int(requestCount.Load()), 10, "request count mismatch") +} + +func TestHTTPError(t *testing.T) { + t.Run("IsConflict returns true for 409", func(t *testing.T) { + conflictErr := &HTTPError{ + StatusCode: 409, + Message: "Conflict", + } + + assert.Equal(t, conflictErr.IsConflict(), true, "IsConflict() should return true for 409") + + notFoundErr := &HTTPError{ + StatusCode: 404, + Message: "Not Found", + } + + assert.Equal(t, notFoundErr.IsConflict(), false, "IsConflict() should return false for 404") + }) } diff --git a/pkg/cli/cmd/add/add.go b/pkg/cli/cmd/add/add.go index 22d4a1ad..3e6d089d 100644 --- a/pkg/cli/cmd/add/add.go +++ b/pkg/cli/cmd/add/add.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package add @@ -134,7 +131,7 @@ func newRun(ctx context.DnoteCtx) infra.RunEFunc { return err } - output.NoteInfo(info) + output.NoteInfo(os.Stdout, info) if err := upgrade.Check(ctx); err != nil { log.Error(errors.Wrap(err, "automatically checking updates").Error()) @@ -173,7 +170,7 @@ func writeNote(ctx context.DnoteCtx, bookLabel string, content string, ts int64) return 0, errors.Wrap(err, "generating uuid") } - n := database.NewNote(noteUUID, bookUUID, content, ts, 0, 0, false, false, true) + n := database.NewNote(noteUUID, bookUUID, content, ts, 0, 0, false, true) err = n.Insert(tx) if err != nil { diff --git a/pkg/cli/cmd/cat/cat.go b/pkg/cli/cmd/cat/cat.go deleted file mode 100644 index a82d0635..00000000 --- a/pkg/cli/cmd/cat/cat.go +++ /dev/null @@ -1,98 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . - */ - -package cat - -import ( - "strconv" - - "github.com/dnote/dnote/pkg/cli/context" - "github.com/dnote/dnote/pkg/cli/database" - "github.com/dnote/dnote/pkg/cli/infra" - "github.com/dnote/dnote/pkg/cli/log" - "github.com/dnote/dnote/pkg/cli/output" - "github.com/pkg/errors" - "github.com/spf13/cobra" -) - -var example = ` - * See the notes with index 2 from a book 'javascript' - dnote cat javascript 2 - ` - -var deprecationWarning = `and "view" will replace it in the future version. - - Run "dnote view --help" for more information. -` - -func preRun(cmd *cobra.Command, args []string) error { - if len(args) != 2 { - return errors.New("Incorrect number of arguments") - } - - return nil -} - -// NewCmd returns a new cat command -func NewCmd(ctx context.DnoteCtx) *cobra.Command { - cmd := &cobra.Command{ - Use: "cat ", - Aliases: []string{"c"}, - Short: "See a note", - Example: example, - RunE: NewRun(ctx, false), - PreRunE: preRun, - Deprecated: deprecationWarning, - } - - return cmd -} - -// NewRun returns a new run function -func NewRun(ctx context.DnoteCtx, contentOnly bool) infra.RunEFunc { - return func(cmd *cobra.Command, args []string) error { - var noteRowIDArg string - - if len(args) == 2 { - log.Plain(log.ColorYellow.Sprintf("DEPRECATED: you no longer need to pass book name to the view command. e.g. `dnote view 123`.\n\n")) - - noteRowIDArg = args[1] - } else { - noteRowIDArg = args[0] - } - - noteRowID, err := strconv.Atoi(noteRowIDArg) - if err != nil { - return errors.Wrap(err, "invalid rowid") - } - - db := ctx.DB - info, err := database.GetNoteInfo(db, noteRowID) - if err != nil { - return err - } - - if contentOnly { - output.NoteContent(info) - } else { - output.NoteInfo(info) - } - - return nil - } -} diff --git a/pkg/cli/cmd/edit/book.go b/pkg/cli/cmd/edit/book.go index 422bd0bf..ab326e7f 100644 --- a/pkg/cli/cmd/edit/book.go +++ b/pkg/cli/cmd/edit/book.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package edit diff --git a/pkg/cli/cmd/edit/edit.go b/pkg/cli/cmd/edit/edit.go index c2b3c5cd..16a7c413 100644 --- a/pkg/cli/cmd/edit/edit.go +++ b/pkg/cli/cmd/edit/edit.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package edit diff --git a/pkg/cli/cmd/edit/note.go b/pkg/cli/cmd/edit/note.go index 3ead62d2..cb837e11 100644 --- a/pkg/cli/cmd/edit/note.go +++ b/pkg/cli/cmd/edit/note.go @@ -1,26 +1,23 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package edit import ( "database/sql" - "io/ioutil" + "os" "strconv" "github.com/dnote/dnote/pkg/cli/context" @@ -45,7 +42,7 @@ func waitEditorNoteContent(ctx context.DnoteCtx, note database.Note) (string, er return "", errors.Wrap(err, "getting temporarily content file path") } - if err := ioutil.WriteFile(fpath, []byte(note.Body), 0644); err != nil { + if err := os.WriteFile(fpath, []byte(note.Body), 0644); err != nil { return "", errors.Wrap(err, "preparing tmp content file") } @@ -169,7 +166,7 @@ func runNote(ctx context.DnoteCtx, rowIDArg string) error { } log.Success("edited the note\n") - output.NoteInfo(noteInfo) + output.NoteInfo(os.Stdout, noteInfo) return nil } diff --git a/pkg/cli/cmd/find/find.go b/pkg/cli/cmd/find/find.go index dce3ad57..4723bcb4 100644 --- a/pkg/cli/cmd/find/find.go +++ b/pkg/cli/cmd/find/find.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package find diff --git a/pkg/cli/cmd/find/lexer.go b/pkg/cli/cmd/find/lexer.go index 88c81dc5..6115becc 100644 --- a/pkg/cli/cmd/find/lexer.go +++ b/pkg/cli/cmd/find/lexer.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package find diff --git a/pkg/cli/cmd/find/lexer_test.go b/pkg/cli/cmd/find/lexer_test.go index 3f7933aa..3a74ad5a 100644 --- a/pkg/cli/cmd/find/lexer_test.go +++ b/pkg/cli/cmd/find/lexer_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package find diff --git a/pkg/cli/cmd/login/login.go b/pkg/cli/cmd/login/login.go index a383bd6a..c9b1dfd5 100644 --- a/pkg/cli/cmd/login/login.go +++ b/pkg/cli/cmd/login/login.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package login @@ -37,7 +34,7 @@ import ( var example = ` dnote login` -var usernameFlag, passwordFlag string +var usernameFlag, passwordFlag, apiEndpointFlag string // NewCmd returns a new login command func NewCmd(ctx context.DnoteCtx) *cobra.Command { @@ -51,6 +48,7 @@ func NewCmd(ctx context.DnoteCtx) *cobra.Command { f := cmd.Flags() f.StringVarP(&usernameFlag, "username", "u", "", "email address for authentication") f.StringVarP(&passwordFlag, "password", "p", "", "password for authentication") + f.StringVar(&apiEndpointFlag, "apiEndpoint", "", "API endpoint to connect to (defaults to value in config)") return cmd } @@ -126,10 +124,6 @@ func getBaseURL(rawURL string) (string, error) { } func getServerDisplayURL(ctx context.DnoteCtx) string { - if ctx.APIEndpoint == "https://api.getdnote.com" { - return "https://www.getdnote.com" - } - baseURL, err := getBaseURL(ctx.APIEndpoint) if err != nil { return "" @@ -139,7 +133,7 @@ func getServerDisplayURL(ctx context.DnoteCtx) string { } func getGreeting(ctx context.DnoteCtx) string { - base := "Welcome to Dnote Pro" + base := "Welcome to Dnote" serverURL := getServerDisplayURL(ctx) if serverURL == "" { @@ -151,6 +145,11 @@ func getGreeting(ctx context.DnoteCtx) string { func newRun(ctx context.DnoteCtx) infra.RunEFunc { return func(cmd *cobra.Command, args []string) error { + // Override APIEndpoint if flag was provided + if apiEndpointFlag != "" { + ctx.APIEndpoint = apiEndpointFlag + } + greeting := getGreeting(ctx) log.Plain(greeting) diff --git a/pkg/cli/cmd/login/login_test.go b/pkg/cli/cmd/login/login_test.go index 22030d4b..6d5917cc 100644 --- a/pkg/cli/cmd/login/login_test.go +++ b/pkg/cli/cmd/login/login_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package login @@ -31,10 +28,6 @@ func TestGetServerDisplayURL(t *testing.T) { apiEndpoint string expected string }{ - { - apiEndpoint: "https://api.getdnote.com", - expected: "https://www.getdnote.com", - }, { apiEndpoint: "https://dnote.mydomain.com/api", expected: "https://dnote.mydomain.com", diff --git a/pkg/cli/cmd/logout/logout.go b/pkg/cli/cmd/logout/logout.go index e1800ee2..98cd69eb 100644 --- a/pkg/cli/cmd/logout/logout.go +++ b/pkg/cli/cmd/logout/logout.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package logout @@ -37,6 +34,8 @@ var ErrNotLoggedIn = errors.New("not logged in") var example = ` dnote logout` +var apiEndpointFlag string + // NewCmd returns a new logout command func NewCmd(ctx context.DnoteCtx) *cobra.Command { cmd := &cobra.Command{ @@ -46,6 +45,9 @@ func NewCmd(ctx context.DnoteCtx) *cobra.Command { RunE: newRun(ctx), } + f := cmd.Flags() + f.StringVar(&apiEndpointFlag, "apiEndpoint", "", "API endpoint to connect to (defaults to value in config)") + return cmd } @@ -84,6 +86,11 @@ func Do(ctx context.DnoteCtx) error { func newRun(ctx context.DnoteCtx) infra.RunEFunc { return func(cmd *cobra.Command, args []string) error { + // Override APIEndpoint if flag was provided + if apiEndpointFlag != "" { + ctx.APIEndpoint = apiEndpointFlag + } + err := Do(ctx) if err == ErrNotLoggedIn { log.Error("not logged in\n") diff --git a/pkg/cli/cmd/remove/remove.go b/pkg/cli/cmd/remove/remove.go index 82394db4..18224c0f 100644 --- a/pkg/cli/cmd/remove/remove.go +++ b/pkg/cli/cmd/remove/remove.go @@ -1,25 +1,23 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package remove import ( "fmt" + "os" "strconv" "github.com/dnote/dnote/pkg/cli/context" @@ -132,7 +130,7 @@ func runNote(ctx context.DnoteCtx, rowIDArg string) error { return err } - output.NoteInfo(noteInfo) + output.NoteInfo(os.Stdout, noteInfo) ok, err := maybeConfirm("remove this note?", false) if err != nil { diff --git a/pkg/cli/cmd/root/root.go b/pkg/cli/cmd/root/root.go index be8d2ed4..6da096bf 100644 --- a/pkg/cli/cmd/root/root.go +++ b/pkg/cli/cmd/root/root.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package root @@ -22,11 +19,30 @@ import ( "github.com/spf13/cobra" ) +var dbPathFlag string + var root = &cobra.Command{ Use: "dnote", Short: "Dnote - a simple command line notebook", SilenceErrors: true, SilenceUsage: true, + CompletionOptions: cobra.CompletionOptions{ + DisableDefaultCmd: true, + }, +} + +func init() { + root.PersistentFlags().StringVar(&dbPathFlag, "dbPath", "", "the path to the database file (defaults to standard location)") +} + +// GetRoot returns the root command +func GetRoot() *cobra.Command { + return root +} + +// GetDBPathFlag returns the value of the --dbPath flag +func GetDBPathFlag() string { + return dbPathFlag } // Register adds a new command diff --git a/pkg/cli/cmd/sync/main_test.go b/pkg/cli/cmd/sync/main_test.go deleted file mode 100644 index b499e905..00000000 --- a/pkg/cli/cmd/sync/main_test.go +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . - */ - -package sync - -import ( - "github.com/dnote/dnote/pkg/cli/context" - "path/filepath" -) - -var testDir = "../../tmp" - -var paths context.Paths = context.Paths{ - Home: testDir, - Cache: testDir, - Config: testDir, - Data: testDir, -} - -var dbPath = filepath.Join(testDir, "test.db") diff --git a/pkg/cli/cmd/sync/merge.go b/pkg/cli/cmd/sync/merge.go index c191426d..0e4d15be 100644 --- a/pkg/cli/cmd/sync/merge.go +++ b/pkg/cli/cmd/sync/merge.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package sync diff --git a/pkg/cli/cmd/sync/merge_test.go b/pkg/cli/cmd/sync/merge_test.go index d335a4b8..e6f4839e 100644 --- a/pkg/cli/cmd/sync/merge_test.go +++ b/pkg/cli/cmd/sync/merge_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package sync @@ -131,9 +128,12 @@ fuuz expected: `foo <<<<<<< Local quz -baz ======= quzz +>>>>>>> Server +<<<<<<< Local +baz +======= bazz >>>>>>> Server bar diff --git a/pkg/cli/cmd/sync/sync.go b/pkg/cli/cmd/sync/sync.go index c6b0d698..6cb506ae 100644 --- a/pkg/cli/cmd/sync/sync.go +++ b/pkg/cli/cmd/sync/sync.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package sync @@ -29,6 +26,7 @@ import ( "github.com/dnote/dnote/pkg/cli/infra" "github.com/dnote/dnote/pkg/cli/log" "github.com/dnote/dnote/pkg/cli/migrate" + "github.com/dnote/dnote/pkg/cli/ui" "github.com/dnote/dnote/pkg/cli/upgrade" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -43,6 +41,7 @@ var example = ` dnote sync` var isFullSync bool +var apiEndpointFlag string // NewCmd returns a new sync command func NewCmd(ctx context.DnoteCtx) *cobra.Command { @@ -56,6 +55,7 @@ func NewCmd(ctx context.DnoteCtx) *cobra.Command { f := cmd.Flags() f.BoolVarP(&isFullSync, "full", "f", false, "perform a full sync instead of incrementally syncing only the changed data.") + f.StringVar(&apiEndpointFlag, "apiEndpoint", "", "API endpoint to connect to (defaults to value in config)") return cmd } @@ -87,6 +87,7 @@ type syncList struct { ExpungedNotes map[string]bool ExpungedBooks map[string]bool MaxUSN int + UserMaxUSN int // Server's actual max USN (for distinguishing empty fragment vs empty server) MaxCurrentTime int64 } @@ -94,14 +95,14 @@ func (l syncList) getLength() int { return len(l.Notes) + len(l.Books) + len(l.ExpungedNotes) + len(l.ExpungedBooks) } -// processFragments categorizes items in sync fragments into a sync list. It also decrypts any -// encrypted data in sync fragments. +// processFragments categorizes items in sync fragments into a sync list. func processFragments(fragments []client.SyncFragment) (syncList, error) { notes := map[string]client.SyncFragNote{} books := map[string]client.SyncFragBook{} expungedNotes := map[string]bool{} expungedBooks := map[string]bool{} var maxUSN int + var userMaxUSN int var maxCurrentTime int64 for _, fragment := range fragments { @@ -121,6 +122,9 @@ func processFragments(fragments []client.SyncFragment) (syncList, error) { if fragment.FragMaxUSN > maxUSN { maxUSN = fragment.FragMaxUSN } + if fragment.UserMaxUSN > userMaxUSN { + userMaxUSN = fragment.UserMaxUSN + } if fragment.CurrentTime > maxCurrentTime { maxCurrentTime = fragment.CurrentTime } @@ -132,6 +136,7 @@ func processFragments(fragments []client.SyncFragment) (syncList, error) { ExpungedNotes: expungedNotes, ExpungedBooks: expungedBooks, MaxUSN: maxUSN, + UserMaxUSN: userMaxUSN, MaxCurrentTime: maxCurrentTime, } @@ -178,11 +183,68 @@ func getSyncFragments(ctx context.DnoteCtx, afterUSN int) ([]client.SyncFragment } } - log.Debug("received sync fragments: %+v\n", buf) + log.Debug("received sync fragments: %+v\n", redactSyncFragments(buf)) return buf, nil } +// redactSyncFragments returns a deep copy of sync fragments with sensitive fields (note body, book label) removed for safe logging +func redactSyncFragments(fragments []client.SyncFragment) []client.SyncFragment { + redacted := make([]client.SyncFragment, len(fragments)) + for i, frag := range fragments { + // Create new notes with redacted bodies + notes := make([]client.SyncFragNote, len(frag.Notes)) + for j, note := range frag.Notes { + notes[j] = client.SyncFragNote{ + UUID: note.UUID, + BookUUID: note.BookUUID, + USN: note.USN, + CreatedAt: note.CreatedAt, + UpdatedAt: note.UpdatedAt, + AddedOn: note.AddedOn, + EditedOn: note.EditedOn, + Body: func() string { + if note.Body != "" { + return "" + } + return "" + }(), + Deleted: note.Deleted, + } + } + + // Create new books with redacted labels + books := make([]client.SyncFragBook, len(frag.Books)) + for j, book := range frag.Books { + books[j] = client.SyncFragBook{ + UUID: book.UUID, + USN: book.USN, + CreatedAt: book.CreatedAt, + UpdatedAt: book.UpdatedAt, + AddedOn: book.AddedOn, + Label: func() string { + if book.Label != "" { + return "" + } + return "" + }(), + Deleted: book.Deleted, + } + } + + redacted[i] = client.SyncFragment{ + FragMaxUSN: frag.FragMaxUSN, + UserMaxUSN: frag.UserMaxUSN, + CurrentTime: frag.CurrentTime, + Notes: notes, + Books: books, + ExpungedNotes: frag.ExpungedNotes, + ExpungedBooks: frag.ExpungedBooks, + } + } + return redacted +} + // resolveLabel resolves a book label conflict by repeatedly appending an increasing integer // to the label until it finds a unique label. It returns the first non-conflicting label. func resolveLabel(tx *database.DB, label string) (string, error) { @@ -219,7 +281,7 @@ func mergeBook(tx *database.DB, b client.SyncFragBook, mode int) error { return errors.Wrap(err, "getting a new book label for conflict resolution") } - if _, err := tx.Exec("UPDATE books SET label = ?, dirty = ? WHERE label = ?", newLabel, true, b.Label); err != nil { + if _, err := tx.Exec("UPDATE books SET label = ?, dirty = ? WHERE label = ? AND uuid != ?", newLabel, true, b.Label, b.UUID); err != nil { return errors.Wrap(err, "resolving duplicate book label") } } @@ -278,8 +340,8 @@ func mergeNote(tx *database.DB, serverNote client.SyncFragNote, localNote databa // if the local copy is deleted, and it was edited on the server, override with server values and mark it not dirty. if localNote.Deleted { - if _, err := tx.Exec("UPDATE notes SET usn = ?, book_uuid = ?, body = ?, edited_on = ?, deleted = ?, public = ?, dirty = ? WHERE uuid = ?", - serverNote.USN, serverNote.BookUUID, serverNote.Body, serverNote.EditedOn, serverNote.Deleted, serverNote.Public, false, serverNote.UUID); err != nil { + if _, err := tx.Exec("UPDATE notes SET usn = ?, book_uuid = ?, body = ?, edited_on = ?, deleted = ?, dirty = ? WHERE uuid = ?", + serverNote.USN, serverNote.BookUUID, serverNote.Body, serverNote.EditedOn, serverNote.Deleted, false, serverNote.UUID); err != nil { return errors.Wrapf(err, "updating local note %s", serverNote.UUID) } @@ -309,7 +371,7 @@ func stepSyncNote(tx *database.DB, n client.SyncFragNote) error { // if note exists in the server and does not exist in the client, insert the note. if err == sql.ErrNoRows { - note := database.NewNote(n.UUID, n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Public, n.Deleted, false) + note := database.NewNote(n.UUID, n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Deleted, false) if err := note.Insert(tx); err != nil { return errors.Wrapf(err, "inserting note with uuid %s", n.UUID) @@ -333,7 +395,7 @@ func fullSyncNote(tx *database.DB, n client.SyncFragNote) error { // if note exists in the server and does not exist in the client, insert the note. if err == sql.ErrNoRows { - note := database.NewNote(n.UUID, n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Public, n.Deleted, false) + note := database.NewNote(n.UUID, n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Deleted, false) if err := note.Insert(tx); err != nil { return errors.Wrapf(err, "inserting note with uuid %s", n.UUID) @@ -538,6 +600,8 @@ func fullSync(ctx context.DnoteCtx, tx *database.DB) error { log.Debug("performing a full sync\n") log.Info("resolving delta.") + log.DebugNewline() + list, err := getSyncList(ctx, 0) if err != nil { return errors.Wrap(err, "getting sync list") @@ -545,6 +609,8 @@ func fullSync(ctx context.DnoteCtx, tx *database.DB) error { fmt.Printf(" (total %d).", list.getLength()) + log.DebugNewline() + // clean resources that are in erroneous states if err := cleanLocalNotes(tx, &list); err != nil { return errors.Wrap(err, "cleaning up local notes") @@ -575,7 +641,7 @@ func fullSync(ctx context.DnoteCtx, tx *database.DB) error { } } - err = saveSyncState(tx, list.MaxCurrentTime, list.MaxUSN) + err = saveSyncState(tx, list.MaxCurrentTime, list.MaxUSN, list.UserMaxUSN) if err != nil { return errors.Wrap(err, "saving sync state") } @@ -590,6 +656,8 @@ func stepSync(ctx context.DnoteCtx, tx *database.DB, afterUSN int) error { log.Info("resolving delta.") + log.DebugNewline() + list, err := getSyncList(ctx, afterUSN) if err != nil { return errors.Wrap(err, "getting sync list") @@ -619,7 +687,7 @@ func stepSync(ctx context.DnoteCtx, tx *database.DB, afterUSN int) error { } } - err = saveSyncState(tx, list.MaxCurrentTime, list.MaxUSN) + err = saveSyncState(tx, list.MaxCurrentTime, list.MaxUSN, list.UserMaxUSN) if err != nil { return errors.Wrap(err, "saving sync state") } @@ -629,6 +697,20 @@ func stepSync(ctx context.DnoteCtx, tx *database.DB, afterUSN int) error { return nil } +// isConflictError checks if an error is a 409 Conflict error from the server +func isConflictError(err error) bool { + if err == nil { + return false + } + + var httpErr *client.HTTPError + if errors.As(err, &httpErr) { + return httpErr.IsConflict() + } + + return false +} + func sendBooks(ctx context.DnoteCtx, tx *database.DB) (bool, error) { isBehind := false @@ -661,7 +743,9 @@ func sendBooks(ctx context.DnoteCtx, tx *database.DB) (bool, error) { } else { resp, err := client.CreateBook(ctx, book.Label) if err != nil { - return isBehind, errors.Wrap(err, "creating a book") + log.Debug("error creating book (will retry after stepSync): %v\n", err) + isBehind = true + continue } _, err = tx.Exec("UPDATE notes SET book_uuid = ? WHERE book_uuid = ?", resp.Book.UUID, book.UUID) @@ -733,10 +817,92 @@ func sendBooks(ctx context.DnoteCtx, tx *database.DB) (bool, error) { return isBehind, nil } +// findOrphanedNotes returns a list of all orphaned notes +func findOrphanedNotes(db *database.DB) (int, []struct{ noteUUID, bookUUID string }, error) { + var orphanCount int + err := db.QueryRow(` + SELECT COUNT(*) FROM notes n + WHERE NOT EXISTS ( + SELECT 1 FROM books b + WHERE b.uuid = n.book_uuid + AND NOT b.deleted + ) + `).Scan(&orphanCount) + if err != nil { + return 0, nil, err + } + + if orphanCount == 0 { + return 0, nil, nil + } + + rows, err := db.Query(` + SELECT n.uuid, n.book_uuid + FROM notes n + WHERE NOT EXISTS ( + SELECT 1 FROM books b + WHERE b.uuid = n.book_uuid + AND NOT b.deleted + ) + `) + if err != nil { + return orphanCount, nil, err + } + defer rows.Close() + + var orphans []struct{ noteUUID, bookUUID string } + for rows.Next() { + var noteUUID, bookUUID string + if err := rows.Scan(¬eUUID, &bookUUID); err != nil { + continue + } + orphans = append(orphans, struct{ noteUUID, bookUUID string }{noteUUID, bookUUID}) + } + + return orphanCount, orphans, nil +} + +func warnOrphanedNotes(tx *database.DB) { + count, orphans, err := findOrphanedNotes(tx) + if err != nil { + log.Debug("error checking orphaned notes: %v\n", err) + return + } + + if count == 0 { + return + } + + log.Debug("Found %d orphaned notes (book doesn't exist locally):\n", count) + for _, o := range orphans { + log.Debug("note %s (book %s)\n", o.noteUUID, o.bookUUID) + } +} + +// checkPostSyncIntegrity checks for data integrity issues after sync and warns the user +func checkPostSyncIntegrity(db *database.DB) { + count, orphans, err := findOrphanedNotes(db) + if err != nil { + log.Debug("error checking orphaned notes: %v\n", err) + return + } + + if count == 0 { + return + } + + log.Warnf("Found %d orphaned notes (referencing non-existent or deleted books):\n", count) + for _, o := range orphans { + log.Plainf(" - note %s (missing book: %s)\n", o.noteUUID, o.bookUUID) + } +} + func sendNotes(ctx context.DnoteCtx, tx *database.DB) (bool, error) { isBehind := false - rows, err := tx.Query("SELECT uuid, book_uuid, body, public, deleted, usn, added_on FROM notes WHERE dirty") + warnOrphanedNotes(tx) + + rows, err := tx.Query("SELECT uuid, book_uuid, body, deleted, usn, added_on FROM notes WHERE dirty") if err != nil { return isBehind, errors.Wrap(err, "getting syncable notes") } @@ -745,11 +911,11 @@ func sendNotes(ctx context.DnoteCtx, tx *database.DB) (bool, error) { for rows.Next() { var note database.Note - if err = rows.Scan(¬e.UUID, ¬e.BookUUID, ¬e.Body, ¬e.Public, ¬e.Deleted, ¬e.USN, ¬e.AddedOn); err != nil { + if err = rows.Scan(¬e.UUID, ¬e.BookUUID, ¬e.Body, ¬e.Deleted, ¬e.USN, ¬e.AddedOn); err != nil { return isBehind, errors.Wrap(err, "scanning a syncable note") } - log.Debug("sending note %s\n", note.UUID) + log.Debug("sending note %s (book: %s)\n", note.UUID, note.BookUUID) var respUSN int @@ -766,7 +932,9 @@ func sendNotes(ctx context.DnoteCtx, tx *database.DB) (bool, error) { } else { resp, err := client.CreateNote(ctx, note.BookUUID, note.Body) if err != nil { - return isBehind, errors.Wrap(err, "creating a note") + log.Debug("failed to create note %s (book: %s): %v\n", note.UUID, note.BookUUID, err) + isBehind = true + continue } note.Dirty = false @@ -797,7 +965,7 @@ func sendNotes(ctx context.DnoteCtx, tx *database.DB) (bool, error) { respUSN = resp.Result.USN } else { - resp, err := client.UpdateNote(ctx, note.UUID, note.BookUUID, note.Body, note.Public) + resp, err := client.UpdateNote(ctx, note.UUID, note.BookUUID, note.Body) if err != nil { return isBehind, errors.Wrap(err, "updating a note") } @@ -841,6 +1009,8 @@ func sendChanges(ctx context.DnoteCtx, tx *database.DB) (bool, error) { fmt.Printf(" (total %d).", delta) + log.DebugNewline() + behind1, err := sendBooks(ctx, tx) if err != nil { return behind1, errors.Wrap(err, "sending books") @@ -874,10 +1044,24 @@ func updateLastSyncAt(tx *database.DB, val int64) error { return nil } -func saveSyncState(tx *database.DB, serverTime int64, serverMaxUSN int) error { - if err := updateLastMaxUSN(tx, serverMaxUSN); err != nil { - return errors.Wrap(err, "updating last max usn") +func saveSyncState(tx *database.DB, serverTime int64, serverMaxUSN int, userMaxUSN int) error { + // Handle last_max_usn update based on server state: + // - If serverMaxUSN > 0: we got data, update to serverMaxUSN + // - If serverMaxUSN == 0 && userMaxUSN > 0: empty fragment (caught up), preserve existing + // - If serverMaxUSN == 0 && userMaxUSN == 0: empty server, reset to 0 + if serverMaxUSN > 0 { + if err := updateLastMaxUSN(tx, serverMaxUSN); err != nil { + return errors.Wrap(err, "updating last max usn") + } + } else if userMaxUSN == 0 { + // Server is empty, reset to 0 + if err := updateLastMaxUSN(tx, 0); err != nil { + return errors.Wrap(err, "updating last max usn") + } } + // else: empty fragment but server has data, preserve existing last_max_usn + + // Always update last_sync_at (we did communicate with server) if err := updateLastSyncAt(tx, serverTime); err != nil { return errors.Wrap(err, "updating last sync at") } @@ -885,8 +1069,33 @@ func saveSyncState(tx *database.DB, serverTime int64, serverMaxUSN int) error { return nil } +// prepareEmptyServerSync marks all local books and notes as dirty when syncing to an empty server. +// This is typically used when switching to a new empty server but wanting to upload existing local data. +// Returns true if preparation was done, false otherwise. +func prepareEmptyServerSync(tx *database.DB) error { + // Mark all books and notes as dirty and reset USN to 0 + if _, err := tx.Exec("UPDATE books SET usn = 0, dirty = 1 WHERE deleted = 0"); err != nil { + return errors.Wrap(err, "marking books as dirty") + } + if _, err := tx.Exec("UPDATE notes SET usn = 0, dirty = 1 WHERE deleted = 0"); err != nil { + return errors.Wrap(err, "marking notes as dirty") + } + + // Reset lastMaxUSN to 0 to match the server + if err := updateLastMaxUSN(tx, 0); err != nil { + return errors.Wrap(err, "resetting last max usn") + } + + return nil +} + func newRun(ctx context.DnoteCtx) infra.RunEFunc { return func(cmd *cobra.Command, args []string) error { + // Override APIEndpoint if flag was provided + if apiEndpointFlag != "" { + ctx.APIEndpoint = apiEndpointFlag + } + if ctx.SessionKey == "" { return errors.New("not logged in") } @@ -915,6 +1124,74 @@ func newRun(ctx context.DnoteCtx) infra.RunEFunc { log.Debug("lastSyncAt: %d, lastMaxUSN: %d, syncState: %+v\n", lastSyncAt, lastMaxUSN, syncState) + // Handle a case where server has MaxUSN=0 but local has data (server switch) + var bookCount, noteCount int + if err := tx.QueryRow("SELECT count(*) FROM books WHERE deleted = 0").Scan(&bookCount); err != nil { + return errors.Wrap(err, "counting local books") + } + if err := tx.QueryRow("SELECT count(*) FROM notes WHERE deleted = 0").Scan(¬eCount); err != nil { + return errors.Wrap(err, "counting local notes") + } + + // If a client has previously synced (lastMaxUSN > 0) but the server was never synced to (MaxUSN = 0), + // and the client has undeleted books or notes, allow to upload all data to the server. + // The client might have switched servers or the server might need to be restored for any reasons. + if syncState.MaxUSN == 0 && lastMaxUSN > 0 && (bookCount > 0 || noteCount > 0) { + log.Debug("empty server detected: server.MaxUSN=%d, local.MaxUSN=%d, books=%d, notes=%d\n", + syncState.MaxUSN, lastMaxUSN, bookCount, noteCount) + + log.Warnf("The server is empty but you have local data. Maybe you switched servers?\n") + log.Debug("server state: MaxUSN = 0 (empty)\n") + log.Debug("local state: %d books, %d notes (MaxUSN = %d)\n", bookCount, noteCount, lastMaxUSN) + + confirmed, err := ui.Confirm(fmt.Sprintf("Upload %d books and %d notes to the server?", bookCount, noteCount), false) + if err != nil { + tx.Rollback() + return errors.Wrap(err, "getting user confirmation") + } + + if !confirmed { + tx.Rollback() + return errors.New("sync cancelled by user") + } + + fmt.Println() // Add newline after confirmation. + + if err := prepareEmptyServerSync(tx); err != nil { + return errors.Wrap(err, "preparing for empty server sync") + } + + // Re-fetch lastMaxUSN after prepareEmptyServerSync + lastMaxUSN, err = getLastMaxUSN(tx) + if err != nil { + return errors.Wrap(err, "getting the last max_usn after prepare") + } + + log.Debug("prepared empty server sync: marked %d books and %d notes as dirty\n", bookCount, noteCount) + } + + // If full sync will be triggered by FullSyncBefore (not manual --full flag), + // and client has more data than server, prepare local data for upload to avoid orphaning notes. + // The lastMaxUSN > syncState.MaxUSN check prevents duplicate uploads when switching + // back to a server that already has our data. + if !isFullSync && lastSyncAt < syncState.FullSyncBefore && lastMaxUSN > syncState.MaxUSN { + log.Debug("full sync triggered by FullSyncBefore: preparing local data for upload\n") + log.Debug("server.FullSyncBefore=%d, local.lastSyncAt=%d, local.MaxUSN=%d, server.MaxUSN=%d, books=%d, notes=%d\n", + syncState.FullSyncBefore, lastSyncAt, lastMaxUSN, syncState.MaxUSN, bookCount, noteCount) + + if err := prepareEmptyServerSync(tx); err != nil { + return errors.Wrap(err, "preparing local data for full sync") + } + + // Re-fetch lastMaxUSN after prepareEmptyServerSync + lastMaxUSN, err = getLastMaxUSN(tx) + if err != nil { + return errors.Wrap(err, "getting the last max_usn after prepare") + } + + log.Debug("prepared for full sync: marked %d books and %d notes as dirty\n", bookCount, noteCount) + } + var syncErr error if isFullSync || lastSyncAt < syncState.FullSyncBefore { syncErr = fullSync(ctx, tx) @@ -953,12 +1230,24 @@ func newRun(ctx context.DnoteCtx) infra.RunEFunc { tx.Rollback() return errors.Wrap(err, "performing the follow-up step sync") } + + // After syncing server changes (which resolves conflicts), send local changes again + // This uploads books/notes that were skipped due to 409 conflicts + _, err = sendChanges(ctx, tx) + if err != nil { + tx.Rollback() + return errors.Wrap(err, "sending changes after conflict resolution") + } } - tx.Commit() + if err := tx.Commit(); err != nil { + return errors.Wrap(err, "committing transaction") + } log.Success("success\n") + checkPostSyncIntegrity(ctx.DB) + if err := upgrade.Check(ctx); err != nil { log.Error(errors.Wrap(err, "automatically checking updates").Error()) } diff --git a/pkg/cli/cmd/sync/sync_test.go b/pkg/cli/cmd/sync/sync_test.go index 41142c63..ae3b94ff 100644 --- a/pkg/cli/cmd/sync/sync_test.go +++ b/pkg/cli/cmd/sync/sync_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package sync @@ -70,7 +67,7 @@ func TestProcessFragments(t *testing.T) { // exec sl, err := processFragments(fragments) if err != nil { - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } expected := syncList{ @@ -97,6 +94,7 @@ func TestProcessFragments(t *testing.T) { ExpungedNotes: map[string]bool{}, ExpungedBooks: map[string]bool{}, MaxUSN: 10, + UserMaxUSN: 10, MaxCurrentTime: 1550436136, } @@ -106,19 +104,18 @@ func TestProcessFragments(t *testing.T) { func TestGetLastSyncAt(t *testing.T) { // set up - db := database.InitTestDB(t, "../../tmp/.dnote", nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, "setting up last_sync_at", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastSyncAt, 1541108743) // exec tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } got, err := getLastSyncAt(tx) if err != nil { - t.Fatalf(errors.Wrap(err, "getting last_sync_at").Error()) + t.Fatal(errors.Wrap(err, "getting last_sync_at").Error()) } tx.Commit() @@ -129,19 +126,18 @@ func TestGetLastSyncAt(t *testing.T) { func TestGetLastMaxUSN(t *testing.T) { // set up - db := database.InitTestDB(t, "../../tmp/.dnote", nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, "setting up last_max_usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 20001) // exec tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } got, err := getLastMaxUSN(tx) if err != nil { - t.Fatalf(errors.Wrap(err, "getting last_max_usn").Error()) + t.Fatal(errors.Wrap(err, "getting last_max_usn").Error()) } tx.Commit() @@ -176,8 +172,7 @@ func TestResolveLabel(t *testing.T) { for idx, tc := range testCases { func() { // set up - db := database.InitTestDB(t, "../../tmp/.dnote", nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b1-uuid", "js") database.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b2-uuid", "css_2") @@ -189,12 +184,12 @@ func TestResolveLabel(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } got, err := resolveLabel(tx, tc.input) if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Rollback() @@ -206,18 +201,17 @@ func TestResolveLabel(t *testing.T) { func TestSyncDeleteNote(t *testing.T) { t.Run("exists on server only", func(t *testing.T) { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if err := syncDeleteNote(tx, "nonexistent-note-uuid"); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -235,8 +229,7 @@ func TestSyncDeleteNote(t *testing.T) { b1UUID := testutils.MustGenerateUUID(t) // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") database.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, true) @@ -254,12 +247,12 @@ func TestSyncDeleteNote(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction for test case").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction for test case").Error()) } if err := syncDeleteNote(tx, "n1-uuid"); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -305,8 +298,7 @@ func TestSyncDeleteNote(t *testing.T) { b1UUID := testutils.MustGenerateUUID(t) // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") database.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, false) @@ -324,12 +316,12 @@ func TestSyncDeleteNote(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction for test case").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction for test case").Error()) } if err := syncDeleteNote(tx, "n1-uuid"); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -361,8 +353,7 @@ func TestSyncDeleteNote(t *testing.T) { func TestSyncDeleteBook(t *testing.T) { t.Run("exists on server only", func(t *testing.T) { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b1-uuid", "b1-label") var b1 database.Book @@ -373,12 +364,12 @@ func TestSyncDeleteBook(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if err := syncDeleteBook(tx, "nonexistent-book-uuid"); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -406,8 +397,7 @@ func TestSyncDeleteBook(t *testing.T) { b1UUID := testutils.MustGenerateUUID(t) // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", b1UUID, "b1-label", 12, true) database.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, true) @@ -424,12 +414,12 @@ func TestSyncDeleteBook(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction for test case").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction for test case").Error()) } if err := syncDeleteBook(tx, b1UUID); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -472,8 +462,7 @@ func TestSyncDeleteBook(t *testing.T) { b2UUID := testutils.MustGenerateUUID(t) // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") database.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, false) @@ -492,12 +481,12 @@ func TestSyncDeleteBook(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction for test case").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction for test case").Error()) } if err := syncDeleteBook(tx, b1UUID); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -538,8 +527,7 @@ func TestSyncDeleteBook(t *testing.T) { b1UUID := testutils.MustGenerateUUID(t) // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") database.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, true) @@ -547,12 +535,12 @@ func TestSyncDeleteBook(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction for test case").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction for test case").Error()) } if err := syncDeleteBook(tx, b1UUID); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -590,8 +578,7 @@ func TestSyncDeleteBook(t *testing.T) { func TestFullSyncNote(t *testing.T) { t.Run("exists on server only", func(t *testing.T) { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") @@ -599,7 +586,7 @@ func TestFullSyncNote(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } n := client.SyncFragNote{ @@ -614,7 +601,7 @@ func TestFullSyncNote(t *testing.T) { if err := fullSyncNote(tx, n); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -822,8 +809,7 @@ n1 body edited for idx, tc := range testCases { func() { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") database.MustExec(t, fmt.Sprintf("inserting b2 for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "b2-label") @@ -834,7 +820,7 @@ n1 body edited // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } // update all fields but uuid and bump usn @@ -850,7 +836,7 @@ n1 body edited if err := fullSyncNote(tx, n); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -884,8 +870,7 @@ n1 body edited func TestFullSyncBook(t *testing.T) { t.Run("exists on server only", func(t *testing.T) { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b1UUID, 555, "b1-label", true, false) @@ -893,7 +878,7 @@ func TestFullSyncBook(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } b2UUID := testutils.MustGenerateUUID(t) @@ -907,7 +892,7 @@ func TestFullSyncBook(t *testing.T) { if err := fullSyncBook(tx, b); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -1023,8 +1008,7 @@ func TestFullSyncBook(t *testing.T) { for idx, tc := range testCases { func() { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b1UUID, tc.clientUSN, tc.clientLabel, tc.clientDirty, tc.clientDeleted) @@ -1032,7 +1016,7 @@ func TestFullSyncBook(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } // update all fields but uuid and bump usn @@ -1045,7 +1029,7 @@ func TestFullSyncBook(t *testing.T) { if err := fullSyncBook(tx, b); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -1076,8 +1060,7 @@ func TestFullSyncBook(t *testing.T) { func TestStepSyncNote(t *testing.T) { t.Run("exists on server only", func(t *testing.T) { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") @@ -1085,7 +1068,7 @@ func TestStepSyncNote(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } n := client.SyncFragNote{ @@ -1100,7 +1083,7 @@ func TestStepSyncNote(t *testing.T) { if err := stepSyncNote(tx, n); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -1234,8 +1217,7 @@ n1 body edited for idx, tc := range testCases { func() { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") database.MustExec(t, fmt.Sprintf("inserting b2 for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "b2-label") @@ -1246,7 +1228,7 @@ n1 body edited // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } // update all fields but uuid and bump usn @@ -1262,7 +1244,7 @@ n1 body edited if err := stepSyncNote(tx, n); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -1296,8 +1278,7 @@ n1 body edited func TestStepSyncBook(t *testing.T) { t.Run("exists on server only", func(t *testing.T) { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b1UUID, 555, "b1-label", true, false) @@ -1305,7 +1286,7 @@ func TestStepSyncBook(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } b2UUID := testutils.MustGenerateUUID(t) @@ -1319,7 +1300,7 @@ func TestStepSyncBook(t *testing.T) { if err := stepSyncBook(tx, b); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -1419,8 +1400,7 @@ func TestStepSyncBook(t *testing.T) { for idx, tc := range testCases { func() { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b1UUID, tc.clientUSN, tc.clientLabel, tc.clientDirty, tc.clientDeleted) @@ -1430,7 +1410,7 @@ func TestStepSyncBook(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } // update all fields but uuid and bump usn @@ -1443,7 +1423,7 @@ func TestStepSyncBook(t *testing.T) { if err := fullSyncBook(tx, b); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -1483,13 +1463,12 @@ func TestStepSyncBook(t *testing.T) { func TestMergeBook(t *testing.T) { t.Run("insert, no duplicates", func(t *testing.T) { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) // test tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } b1 := client.SyncFragBook{ @@ -1502,7 +1481,7 @@ func TestMergeBook(t *testing.T) { if err := mergeBook(tx, b1, modeInsert); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -1527,14 +1506,13 @@ func TestMergeBook(t *testing.T) { t.Run("insert, 1 duplicate", func(t *testing.T) { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b1-uuid", 1, "foo", false, false) // test tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } b := client.SyncFragBook{ @@ -1547,7 +1525,7 @@ func TestMergeBook(t *testing.T) { if err := mergeBook(tx, b, modeInsert); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -1579,8 +1557,7 @@ func TestMergeBook(t *testing.T) { t.Run("insert, 3 duplicates", func(t *testing.T) { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b1-uuid", 1, "foo", false, false) database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b2-uuid", 2, "foo_2", true, false) @@ -1589,7 +1566,7 @@ func TestMergeBook(t *testing.T) { // test tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } b := client.SyncFragBook{ @@ -1602,7 +1579,7 @@ func TestMergeBook(t *testing.T) { if err := mergeBook(tx, b, modeInsert); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -1648,13 +1625,12 @@ func TestMergeBook(t *testing.T) { t.Run("update, no duplicates", func(t *testing.T) { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) // test tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } b1UUID := testutils.MustGenerateUUID(t) @@ -1670,7 +1646,7 @@ func TestMergeBook(t *testing.T) { if err := mergeBook(tx, b1, modeUpdate); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -1695,8 +1671,7 @@ func TestMergeBook(t *testing.T) { t.Run("update, 1 duplicate", func(t *testing.T) { // set up - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b1-uuid", 1, "foo", false, false) database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b2-uuid", 2, "bar", false, false) @@ -1704,7 +1679,7 @@ func TestMergeBook(t *testing.T) { // test tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } b := client.SyncFragBook{ @@ -1717,7 +1692,7 @@ func TestMergeBook(t *testing.T) { if err := mergeBook(tx, b, modeUpdate); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -1749,8 +1724,7 @@ func TestMergeBook(t *testing.T) { t.Run("update, 3 duplicate", func(t *testing.T) { // set uj - db := database.InitTestDB(t, dbPath, nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b1-uuid", 1, "foo", false, false) database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b2-uuid", 2, "bar", false, false) @@ -1760,7 +1734,7 @@ func TestMergeBook(t *testing.T) { // test tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } b := client.SyncFragBook{ @@ -1773,7 +1747,7 @@ func TestMergeBook(t *testing.T) { if err := mergeBook(tx, b, modeUpdate); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -1819,44 +1793,132 @@ func TestMergeBook(t *testing.T) { } func TestSaveServerState(t *testing.T) { - // set up - ctx := context.InitTestCtx(t, paths, nil) - defer context.TeardownTestCtx(t, ctx) - testutils.Login(t, &ctx) + t.Run("with data received", func(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + testutils.LoginDB(t, db) - db := ctx.DB + database.MustExec(t, "inserting last synced at", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastSyncAt, int64(1231108742)) + database.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 8) - database.MustExec(t, "inserting last synced at", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastSyncAt, int64(1231108742)) - database.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 8) + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } + serverTime := int64(1541108743) + serverMaxUSN := 100 + userMaxUSN := 100 - serverTime := int64(1541108743) - serverMaxUSN := 100 + err = saveSyncState(tx, serverTime, serverMaxUSN, userMaxUSN) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } - err = saveSyncState(tx, serverTime, serverMaxUSN) - if err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } + tx.Commit() - tx.Commit() + // test + var lastSyncedAt int64 + var lastMaxUSN int - // test - var lastSyncedAt int64 - var lastMaxUSN int + database.MustScan(t, "getting system value", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastSyncAt), &lastSyncedAt) + database.MustScan(t, "getting system value", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &lastMaxUSN) - database.MustScan(t, "getting system value", - db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastSyncAt), &lastSyncedAt) - database.MustScan(t, "getting system value", - db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &lastMaxUSN) + assert.Equal(t, lastSyncedAt, serverTime, "last synced at mismatch") + assert.Equal(t, lastMaxUSN, serverMaxUSN, "last max usn mismatch") + }) - assert.Equal(t, lastSyncedAt, serverTime, "last synced at mismatch") - assert.Equal(t, lastMaxUSN, serverMaxUSN, "last max usn mismatch") + t.Run("with empty fragment but server has data - preserves last_max_usn", func(t *testing.T) { + // This tests the fix for the infinite sync bug where empty fragments + // would reset last_max_usn to 0, causing the client to re-download all data. + // When serverMaxUSN=0 (no data in fragment) but userMaxUSN>0 (server has data), + // we're caught up and should preserve the existing last_max_usn. + + // set up + db := database.InitTestMemoryDB(t) + testutils.LoginDB(t, db) + + existingLastMaxUSN := 100 + database.MustExec(t, "inserting last synced at", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastSyncAt, int64(1231108742)) + database.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, existingLastMaxUSN) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + serverTime := int64(1541108743) + serverMaxUSN := 0 // Empty fragment (no data in this sync) + userMaxUSN := 150 // Server's actual max USN (higher than ours) + + err = saveSyncState(tx, serverTime, serverMaxUSN, userMaxUSN) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var lastSyncedAt int64 + var lastMaxUSN int + + database.MustScan(t, "getting system value", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastSyncAt), &lastSyncedAt) + database.MustScan(t, "getting system value", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &lastMaxUSN) + + assert.Equal(t, lastSyncedAt, serverTime, "last synced at should be updated") + // last_max_usn should NOT be updated to 0, it should preserve the existing value + assert.Equal(t, lastMaxUSN, existingLastMaxUSN, "last max usn should be preserved when fragment is empty but server has data") + }) + + t.Run("with empty server - resets last_max_usn to 0", func(t *testing.T) { + // When both serverMaxUSN=0 and userMaxUSN=0, the server is truly empty + // and we should reset last_max_usn to 0. + + // set up + db := database.InitTestMemoryDB(t) + testutils.LoginDB(t, db) + + database.MustExec(t, "inserting last synced at", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastSyncAt, int64(1231108742)) + database.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 50) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + serverTime := int64(1541108743) + serverMaxUSN := 0 // Empty fragment + userMaxUSN := 0 // Server is actually empty + + err = saveSyncState(tx, serverTime, serverMaxUSN, userMaxUSN) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var lastSyncedAt int64 + var lastMaxUSN int + + database.MustScan(t, "getting system value", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastSyncAt), &lastSyncedAt) + database.MustScan(t, "getting system value", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &lastMaxUSN) + + assert.Equal(t, lastSyncedAt, serverTime, "last synced at should be updated") + assert.Equal(t, lastMaxUSN, 0, "last max usn should be reset to 0 when server is empty") + }) } // TestSendBooks tests that books are put to correct 'buckets' by running a test server and recording the @@ -1864,8 +1926,7 @@ func TestSaveServerState(t *testing.T) { // are updated accordingly based on the server response. func TestSendBooks(t *testing.T) { // set up - ctx := context.InitTestCtx(t, paths, nil) - defer context.TeardownTestCtx(t, ctx) + ctx := context.InitTestCtx(t) testutils.Login(t, &ctx) db := ctx.DB @@ -1900,14 +1961,14 @@ func TestSendBooks(t *testing.T) { var updatesUUIDs []string var deletedUUIDs []string - // fire up a test server. It decrypts the payload for test purposes. + // fire up a test server ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.String() == "/v3/books" && r.Method == "POST" { var payload client.CreateBookPayload err := json.NewDecoder(r.Body).Decode(&payload) if err != nil { - t.Fatalf(errors.Wrap(err, "decoding payload in the test server").Error()) + t.Fatal(errors.Wrap(err, "decoding payload in the test server").Error()) return } @@ -1955,19 +2016,19 @@ func TestSendBooks(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if _, err := sendBooks(ctx, tx); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() // test - // First, decrypt data so that they can be asserted + // First, sort data so that they can be asserted sort.SliceStable(createdLabels, func(i, j int) bool { return strings.Compare(createdLabels[i], createdLabels[j]) < 0 }) @@ -2026,7 +2087,7 @@ func TestSendBooks_isBehind(t *testing.T) { err := json.NewDecoder(r.Body).Decode(&payload) if err != nil { - t.Fatalf(errors.Wrap(err, "decoding payload in the test server").Error()) + t.Fatal(errors.Wrap(err, "decoding payload in the test server").Error()) return } @@ -2097,9 +2158,8 @@ func TestSendBooks_isBehind(t *testing.T) { for idx, tc := range testCases { func() { // set up - ctx := context.InitTestCtx(t, paths, nil) + ctx := context.InitTestCtx(t) ctx.APIEndpoint = ts.URL - defer context.TeardownTestCtx(t, ctx) testutils.Login(t, &ctx) db := ctx.DB @@ -2110,13 +2170,13 @@ func TestSendBooks_isBehind(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } isBehind, err := sendBooks(ctx, tx) if err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -2145,9 +2205,8 @@ func TestSendBooks_isBehind(t *testing.T) { for idx, tc := range testCases { func() { // set up - ctx := context.InitTestCtx(t, paths, nil) + ctx := context.InitTestCtx(t) ctx.APIEndpoint = ts.URL - defer context.TeardownTestCtx(t, ctx) testutils.Login(t, &ctx) db := ctx.DB @@ -2158,13 +2217,13 @@ func TestSendBooks_isBehind(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } isBehind, err := sendBooks(ctx, tx) if err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -2193,9 +2252,8 @@ func TestSendBooks_isBehind(t *testing.T) { for idx, tc := range testCases { func() { // set up - ctx := context.InitTestCtx(t, paths, nil) + ctx := context.InitTestCtx(t) ctx.APIEndpoint = ts.URL - defer context.TeardownTestCtx(t, ctx) testutils.Login(t, &ctx) db := ctx.DB @@ -2206,13 +2264,13 @@ func TestSendBooks_isBehind(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } isBehind, err := sendBooks(ctx, tx) if err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -2228,8 +2286,7 @@ func TestSendBooks_isBehind(t *testing.T) { // uuid from the incoming data. func TestSendNotes(t *testing.T) { // set up - ctx := context.InitTestCtx(t, paths, nil) - defer context.TeardownTestCtx(t, ctx) + ctx := context.InitTestCtx(t) testutils.Login(t, &ctx) db := ctx.DB @@ -2264,14 +2321,14 @@ func TestSendNotes(t *testing.T) { var updatedUUIDs []string var deletedUUIDs []string - // fire up a test server. It decrypts the payload for test purposes. + // fire up a test server ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.String() == "/v3/notes" && r.Method == "POST" { var payload client.CreateNotePayload err := json.NewDecoder(r.Body).Decode(&payload) if err != nil { - t.Fatalf(errors.Wrap(err, "decoding payload in the test server").Error()) + t.Fatal(errors.Wrap(err, "decoding payload in the test server").Error()) return } @@ -2319,12 +2376,12 @@ func TestSendNotes(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if _, err := sendNotes(ctx, tx); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -2381,8 +2438,7 @@ func TestSendNotes(t *testing.T) { func TestSendNotes_addedOn(t *testing.T) { // set up - ctx := context.InitTestCtx(t, paths, nil) - defer context.TeardownTestCtx(t, ctx) + ctx := context.InitTestCtx(t) testutils.Login(t, &ctx) db := ctx.DB @@ -2393,7 +2449,7 @@ func TestSendNotes_addedOn(t *testing.T) { b1UUID := "b1-uuid" database.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 0, "n1-body", 1541108743, false, true) - // fire up a test server. It decrypts the payload for test purposes. + // fire up a test server ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.String() == "/v3/notes" && r.Method == "POST" { resp := client.CreateNoteResp{ @@ -2419,12 +2475,12 @@ func TestSendNotes_addedOn(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if _, err := sendNotes(ctx, tx); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -2442,7 +2498,7 @@ func TestSendNotes_isBehind(t *testing.T) { err := json.NewDecoder(r.Body).Decode(&payload) if err != nil { - t.Fatalf(errors.Wrap(err, "decoding payload in the test server").Error()) + t.Fatal(errors.Wrap(err, "decoding payload in the test server").Error()) return } @@ -2513,8 +2569,7 @@ func TestSendNotes_isBehind(t *testing.T) { for idx, tc := range testCases { func() { // set up - ctx := context.InitTestCtx(t, paths, nil) - defer context.TeardownTestCtx(t, ctx) + ctx := context.InitTestCtx(t) testutils.Login(t, &ctx) ctx.APIEndpoint = ts.URL @@ -2527,13 +2582,13 @@ func TestSendNotes_isBehind(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } isBehind, err := sendNotes(ctx, tx) if err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -2562,8 +2617,7 @@ func TestSendNotes_isBehind(t *testing.T) { for idx, tc := range testCases { func() { // set up - ctx := context.InitTestCtx(t, paths, nil) - defer context.TeardownTestCtx(t, ctx) + ctx := context.InitTestCtx(t) testutils.Login(t, &ctx) ctx.APIEndpoint = ts.URL @@ -2576,13 +2630,13 @@ func TestSendNotes_isBehind(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } isBehind, err := sendNotes(ctx, tx) if err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -2611,8 +2665,7 @@ func TestSendNotes_isBehind(t *testing.T) { for idx, tc := range testCases { func() { // set up - ctx := context.InitTestCtx(t, paths, nil) - defer context.TeardownTestCtx(t, ctx) + ctx := context.InitTestCtx(t) testutils.Login(t, &ctx) ctx.APIEndpoint = ts.URL @@ -2625,13 +2678,13 @@ func TestSendNotes_isBehind(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } isBehind, err := sendNotes(ctx, tx) if err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -2777,8 +2830,7 @@ n1 body edited for idx, tc := range testCases { func() { // set up - db := database.InitTestDB(t, "../../tmp/.dnote", nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", b1UUID, "b1-label", 5, false) database.MustExec(t, fmt.Sprintf("inserting b2 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", b2UUID, "b2-label", 6, false) @@ -2789,7 +2841,7 @@ n1 body edited // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } // update all fields but uuid and bump usn @@ -2809,7 +2861,7 @@ n1 body edited if err := mergeNote(tx, fragNote, localNote); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -2859,8 +2911,7 @@ n1 body edited func TestCheckBookPristine(t *testing.T) { // set up - db := database.InitTestDB(t, "../../tmp/.dnote", nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", "b1-uuid", "b1-label", 5, false) database.MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", "b2-uuid", "b2-label", 6, false) @@ -2874,12 +2925,12 @@ func TestCheckBookPristine(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } got, err := checkNotesPristine(tx, "b1-uuid") if err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -2892,12 +2943,12 @@ func TestCheckBookPristine(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } got, err := checkNotesPristine(tx, "b2-uuid") if err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -3033,8 +3084,7 @@ func TestCheckBookInList(t *testing.T) { func TestCleanLocalNotes(t *testing.T) { // set up - db := database.InitTestDB(t, "../../tmp/.dnote", nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) list := syncList{ Notes: map[string]client.SyncFragNote{ @@ -3082,12 +3132,12 @@ func TestCleanLocalNotes(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if err := cleanLocalNotes(tx, &list); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -3105,8 +3155,7 @@ func TestCleanLocalNotes(t *testing.T) { func TestCleanLocalBooks(t *testing.T) { // set up - db := database.InitTestDB(t, "../../tmp/.dnote", nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) list := syncList{ Notes: map[string]client.SyncFragNote{ @@ -3150,12 +3199,12 @@ func TestCleanLocalBooks(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if err := cleanLocalBooks(tx, &list); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -3170,3 +3219,69 @@ func TestCleanLocalBooks(t *testing.T) { database.MustScan(t, "getting b3", db.QueryRow("SELECT label FROM books WHERE uuid = ?", "b3-uuid"), &b3.Label) database.MustScan(t, "getting b5", db.QueryRow("SELECT label FROM books WHERE uuid = ?", "b5-uuid"), &b5.Label) } + +func TestPrepareEmptyServerSync(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + + // Setup: local has synced data (usn > 0, dirty = false) and some deleted items + database.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 5, false, false) + database.MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b2-uuid", "b2-label", 8, false, false) + database.MustExec(t, "inserting b3 deleted", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b3-uuid", "b3-label", 6, true, false) + database.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, usn, deleted, dirty, added_on) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", "b1-uuid", "note 1", 6, false, false, 1541108743) + database.MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, body, usn, deleted, dirty, added_on) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", "b2-uuid", "note 2", 9, false, false, 1541108743) + database.MustExec(t, "inserting n3 deleted", db, "INSERT INTO notes (uuid, book_uuid, body, usn, deleted, dirty, added_on) VALUES (?, ?, ?, ?, ?, ?, ?)", "n3-uuid", "b1-uuid", "note 3", 7, true, false, 1541108743) + database.MustExec(t, "setting last_max_usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 9) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning transaction")) + } + + if err := prepareEmptyServerSync(tx); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing prepareEmptyServerSync")) + } + + tx.Commit() + + // test - verify non-deleted items are marked dirty with usn=0, deleted items unchanged + var b1, b2, b3 database.Book + database.MustScan(t, "getting b1", db.QueryRow("SELECT usn, dirty, deleted FROM books WHERE uuid = ?", "b1-uuid"), &b1.USN, &b1.Dirty, &b1.Deleted) + database.MustScan(t, "getting b2", db.QueryRow("SELECT usn, dirty, deleted FROM books WHERE uuid = ?", "b2-uuid"), &b2.USN, &b2.Dirty, &b2.Deleted) + database.MustScan(t, "getting b3", db.QueryRow("SELECT usn, dirty, deleted FROM books WHERE uuid = ?", "b3-uuid"), &b3.USN, &b3.Dirty, &b3.Deleted) + + assert.Equal(t, b1.USN, 0, "b1 USN should be reset to 0") + assert.Equal(t, b1.Dirty, true, "b1 should be marked dirty") + assert.Equal(t, b1.Deleted, false, "b1 should not be deleted") + + assert.Equal(t, b2.USN, 0, "b2 USN should be reset to 0") + assert.Equal(t, b2.Dirty, true, "b2 should be marked dirty") + assert.Equal(t, b2.Deleted, false, "b2 should not be deleted") + + assert.Equal(t, b3.USN, 6, "b3 USN should remain unchanged (deleted item)") + assert.Equal(t, b3.Dirty, false, "b3 should not be marked dirty (deleted item)") + assert.Equal(t, b3.Deleted, true, "b3 should remain deleted") + + var n1, n2, n3 database.Note + database.MustScan(t, "getting n1", db.QueryRow("SELECT usn, dirty, deleted FROM notes WHERE uuid = ?", "n1-uuid"), &n1.USN, &n1.Dirty, &n1.Deleted) + database.MustScan(t, "getting n2", db.QueryRow("SELECT usn, dirty, deleted FROM notes WHERE uuid = ?", "n2-uuid"), &n2.USN, &n2.Dirty, &n2.Deleted) + database.MustScan(t, "getting n3", db.QueryRow("SELECT usn, dirty, deleted FROM notes WHERE uuid = ?", "n3-uuid"), &n3.USN, &n3.Dirty, &n3.Deleted) + + assert.Equal(t, n1.USN, 0, "n1 USN should be reset to 0") + assert.Equal(t, n1.Dirty, true, "n1 should be marked dirty") + assert.Equal(t, n1.Deleted, false, "n1 should not be deleted") + + assert.Equal(t, n2.USN, 0, "n2 USN should be reset to 0") + assert.Equal(t, n2.Dirty, true, "n2 should be marked dirty") + assert.Equal(t, n2.Deleted, false, "n2 should not be deleted") + + assert.Equal(t, n3.USN, 7, "n3 USN should remain unchanged (deleted item)") + assert.Equal(t, n3.Dirty, false, "n3 should not be marked dirty (deleted item)") + assert.Equal(t, n3.Deleted, true, "n3 should remain deleted") + + var lastMaxUSN int + database.MustScan(t, "getting last_max_usn", db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &lastMaxUSN) + assert.Equal(t, lastMaxUSN, 0, "last_max_usn should be reset to 0") +} diff --git a/pkg/cli/cmd/version/version.go b/pkg/cli/cmd/version/version.go index bb050fa3..79892204 100644 --- a/pkg/cli/cmd/version/version.go +++ b/pkg/cli/cmd/version/version.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package version diff --git a/pkg/cli/cmd/ls/ls.go b/pkg/cli/cmd/view/book.go similarity index 50% rename from pkg/cli/cmd/ls/ls.go rename to pkg/cli/cmd/view/book.go index 9f22ab4e..698a7de5 100644 --- a/pkg/cli/cmd/ls/ls.go +++ b/pkg/cli/cmd/view/book.go @@ -1,91 +1,31 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -package ls +package view import ( "database/sql" "fmt" + "io" "strings" "github.com/dnote/dnote/pkg/cli/context" - "github.com/dnote/dnote/pkg/cli/infra" "github.com/dnote/dnote/pkg/cli/log" "github.com/pkg/errors" - "github.com/spf13/cobra" ) -var example = ` - * List all books - dnote ls - - * List notes in a book - dnote ls javascript - ` - -var deprecationWarning = `and "view" will replace it in the future version. - -Run "dnote view --help" for more information. -` - -func preRun(cmd *cobra.Command, args []string) error { - if len(args) > 1 { - return errors.New("Incorrect number of argument") - } - - return nil -} - -// NewCmd returns a new ls command -func NewCmd(ctx context.DnoteCtx) *cobra.Command { - cmd := &cobra.Command{ - Use: "ls ", - Aliases: []string{"l", "notes"}, - Short: "List all notes", - Example: example, - RunE: NewRun(ctx, false), - PreRunE: preRun, - Deprecated: deprecationWarning, - } - - return cmd -} - -// NewRun returns a new run function for ls -func NewRun(ctx context.DnoteCtx, nameOnly bool) infra.RunEFunc { - return func(cmd *cobra.Command, args []string) error { - if len(args) == 0 { - if err := printBooks(ctx, nameOnly); err != nil { - return errors.Wrap(err, "viewing books") - } - - return nil - } - - bookName := args[0] - if err := printNotes(ctx, bookName); err != nil { - return errors.Wrapf(err, "viewing book '%s'", bookName) - } - - return nil - } -} - // bookInfo is an information about the book to be printed on screen type bookInfo struct { BookLabel string @@ -100,15 +40,13 @@ type noteInfo struct { // getNewlineIdx returns the index of newline character in a string func getNewlineIdx(str string) int { - var ret int - - ret = strings.Index(str, "\n") - - if ret == -1 { - ret = strings.Index(str, "\r\n") + // Check for \r\n first + if idx := strings.Index(str, "\r\n"); idx != -1 { + return idx } - return ret + // Then check for \n + return strings.Index(str, "\n") } // formatBody returns an excerpt of the given raw note content and a boolean @@ -126,15 +64,15 @@ func formatBody(noteBody string) (string, bool) { return strings.Trim(trimmed, " "), false } -func printBookLine(info bookInfo, nameOnly bool) { +func printBookLine(w io.Writer, info bookInfo, nameOnly bool) { if nameOnly { - fmt.Println(info.BookLabel) + fmt.Fprintln(w, info.BookLabel) } else { - log.Printf("%s %s\n", info.BookLabel, log.ColorYellow.Sprintf("(%d)", info.NoteCount)) + fmt.Fprintf(w, "%s %s\n", info.BookLabel, log.ColorYellow.Sprintf("(%d)", info.NoteCount)) } } -func printBooks(ctx context.DnoteCtx, nameOnly bool) error { +func listBooks(ctx context.DnoteCtx, w io.Writer, nameOnly bool) error { db := ctx.DB rows, err := db.Query(`SELECT books.label, count(notes.uuid) note_count @@ -160,13 +98,13 @@ func printBooks(ctx context.DnoteCtx, nameOnly bool) error { } for _, info := range infos { - printBookLine(info, nameOnly) + printBookLine(w, info, nameOnly) } return nil } -func printNotes(ctx context.DnoteCtx, bookName string) error { +func listNotes(ctx context.DnoteCtx, w io.Writer, bookName string) error { db := ctx.DB var bookUUID string @@ -194,7 +132,7 @@ func printNotes(ctx context.DnoteCtx, bookName string) error { infos = append(infos, info) } - log.Infof("on book %s\n", bookName) + fmt.Fprintf(w, "on book %s\n", bookName) for _, info := range infos { body, isExcerpt := formatBody(info.Body) @@ -204,7 +142,7 @@ func printNotes(ctx context.DnoteCtx, bookName string) error { body = fmt.Sprintf("%s %s", body, log.ColorYellow.Sprintf("[---More---]")) } - log.Plainf("%s %s\n", rowid, body) + fmt.Fprintf(w, "%s %s\n", rowid, body) } return nil diff --git a/pkg/cli/cmd/view/book_test.go b/pkg/cli/cmd/view/book_test.go new file mode 100644 index 00000000..226d5d04 --- /dev/null +++ b/pkg/cli/cmd/view/book_test.go @@ -0,0 +1,184 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package view + +import ( + "bytes" + "fmt" + "strings" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" +) + +func TestGetNewlineIdx(t *testing.T) { + testCases := []struct { + input string + expected int + }{ + { + input: "hello\nworld", + expected: 5, + }, + { + input: "hello\r\nworld", + expected: 5, + }, + { + input: "no newline here", + expected: -1, + }, + { + input: "", + expected: -1, + }, + { + input: "\n", + expected: 0, + }, + { + input: "\r\n", + expected: 0, + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("input: %q", tc.input), func(t *testing.T) { + got := getNewlineIdx(tc.input) + assert.Equal(t, got, tc.expected, "newline index mismatch") + }) + } +} + +func TestFormatBody(t *testing.T) { + testCases := []struct { + input string + expectedBody string + expectedExcerpt bool + }{ + { + input: "single line", + expectedBody: "single line", + expectedExcerpt: false, + }, + { + input: "first line\nsecond line", + expectedBody: "first line", + expectedExcerpt: true, + }, + { + input: "first line\r\nsecond line", + expectedBody: "first line", + expectedExcerpt: true, + }, + { + input: " spaced line ", + expectedBody: "spaced line", + expectedExcerpt: false, + }, + { + input: " first line \nsecond line", + expectedBody: "first line", + expectedExcerpt: true, + }, + { + input: "", + expectedBody: "", + expectedExcerpt: false, + }, + { + input: "line with trailing newline\n", + expectedBody: "line with trailing newline", + expectedExcerpt: false, + }, + { + input: "line with trailing newlines\n\n", + expectedBody: "line with trailing newlines", + expectedExcerpt: false, + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("input: %q", tc.input), func(t *testing.T) { + gotBody, gotExcerpt := formatBody(tc.input) + assert.Equal(t, gotBody, tc.expectedBody, "formatted body mismatch") + assert.Equal(t, gotExcerpt, tc.expectedExcerpt, "excerpt flag mismatch") + }) + } +} + +func TestListNotes(t *testing.T) { + // Setup + db := database.InitTestMemoryDB(t) + defer db.Close() + + bookUUID := "js-book-uuid" + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", bookUUID, "javascript") + database.MustExec(t, "inserting note 1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", "note-1", bookUUID, "first note", 1515199943) + database.MustExec(t, "inserting note 2", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", "note-2", bookUUID, "multiline note\nwith second line", 1515199945) + + ctx := context.DnoteCtx{DB: db} + var buf bytes.Buffer + + // Execute + err := listNotes(ctx, &buf, "javascript") + if err != nil { + t.Fatal(err) + } + + got := buf.String() + + // Verify output + assert.Equal(t, strings.Contains(got, "on book javascript"), true, "should show book name") + assert.Equal(t, strings.Contains(got, "first note"), true, "should contain first note") + assert.Equal(t, strings.Contains(got, "multiline note"), true, "should show first line of multiline note") + assert.Equal(t, strings.Contains(got, "[---More---]"), true, "should show more indicator for multiline note") + assert.Equal(t, strings.Contains(got, "with second line"), false, "should not show second line of multiline note") +} + +func TestListBooks(t *testing.T) { + // Setup + db := database.InitTestMemoryDB(t) + defer db.Close() + + b1UUID := "js-book-uuid" + b2UUID := "linux-book-uuid" + + database.MustExec(t, "inserting book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "javascript") + database.MustExec(t, "inserting book 2", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "linux") + + // Add notes to test count + database.MustExec(t, "inserting note 1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", "note-1", b1UUID, "note body 1", 1515199943) + database.MustExec(t, "inserting note 2", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", "note-2", b1UUID, "note body 2", 1515199944) + + ctx := context.DnoteCtx{DB: db} + var buf bytes.Buffer + + // Execute + err := listBooks(ctx, &buf, false) + if err != nil { + t.Fatal(err) + } + + got := buf.String() + + // Verify output + assert.Equal(t, strings.Contains(got, "javascript"), true, "should contain javascript book") + assert.Equal(t, strings.Contains(got, "linux"), true, "should contain linux book") + assert.Equal(t, strings.Contains(got, "(2)"), true, "should show 2 notes for javascript") +} diff --git a/pkg/cli/cmd/view/note.go b/pkg/cli/cmd/view/note.go new file mode 100644 index 00000000..f853dd9a --- /dev/null +++ b/pkg/cli/cmd/view/note.go @@ -0,0 +1,47 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package view + +import ( + "io" + "strconv" + + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/output" + "github.com/pkg/errors" +) + +func viewNote(ctx context.DnoteCtx, w io.Writer, noteRowIDArg string, contentOnly bool) error { + noteRowID, err := strconv.Atoi(noteRowIDArg) + if err != nil { + return errors.Wrap(err, "invalid rowid") + } + + db := ctx.DB + info, err := database.GetNoteInfo(db, noteRowID) + if err != nil { + return err + } + + if contentOnly { + output.NoteContent(w, info) + } else { + output.NoteInfo(w, info) + } + + return nil +} diff --git a/pkg/cli/cmd/view/note_test.go b/pkg/cli/cmd/view/note_test.go new file mode 100644 index 00000000..36e9aa84 --- /dev/null +++ b/pkg/cli/cmd/view/note_test.go @@ -0,0 +1,90 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package view + +import ( + "bytes" + "strings" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" +) + +func TestViewNote(t *testing.T) { + db := database.InitTestMemoryDB(t) + defer db.Close() + + bookUUID := "test-book-uuid" + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", bookUUID, "golang") + database.MustExec(t, "inserting note", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", + "note-uuid", bookUUID, "test note content", 1515199943000000000) + + ctx := context.DnoteCtx{DB: db} + var buf bytes.Buffer + + err := viewNote(ctx, &buf, "1", false) + if err != nil { + t.Fatal(err) + } + + got := buf.String() + assert.Equal(t, strings.Contains(got, "test note content"), true, "should contain note content") +} + +func TestViewNoteContentOnly(t *testing.T) { + db := database.InitTestMemoryDB(t) + defer db.Close() + + bookUUID := "test-book-uuid" + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", bookUUID, "golang") + database.MustExec(t, "inserting note", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", + "note-uuid", bookUUID, "test note content", 1515199943000000000) + + ctx := context.DnoteCtx{DB: db} + var buf bytes.Buffer + + err := viewNote(ctx, &buf, "1", true) + if err != nil { + t.Fatal(err) + } + + got := buf.String() + assert.Equal(t, got, "test note content", "should contain only note content") +} + +func TestViewNoteInvalidRowID(t *testing.T) { + db := database.InitTestMemoryDB(t) + defer db.Close() + + ctx := context.DnoteCtx{DB: db} + var buf bytes.Buffer + + err := viewNote(ctx, &buf, "not-a-number", false) + assert.NotEqual(t, err, nil, "should return error for invalid rowid") +} + +func TestViewNoteNotFound(t *testing.T) { + db := database.InitTestMemoryDB(t) + defer db.Close() + + ctx := context.DnoteCtx{DB: db} + var buf bytes.Buffer + + err := viewNote(ctx, &buf, "999", false) + assert.NotEqual(t, err, nil, "should return error for non-existent note") +} diff --git a/pkg/cli/cmd/view/view.go b/pkg/cli/cmd/view/view.go index cc39a451..57b17dbd 100644 --- a/pkg/cli/cmd/view/view.go +++ b/pkg/cli/cmd/view/view.go @@ -1,32 +1,28 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package view import ( + "os" + "github.com/dnote/dnote/pkg/cli/context" "github.com/dnote/dnote/pkg/cli/infra" + "github.com/dnote/dnote/pkg/cli/utils" "github.com/pkg/errors" "github.com/spf13/cobra" - - "github.com/dnote/dnote/pkg/cli/cmd/cat" - "github.com/dnote/dnote/pkg/cli/cmd/ls" - "github.com/dnote/dnote/pkg/cli/utils" ) var example = ` @@ -71,27 +67,26 @@ func NewCmd(ctx context.DnoteCtx) *cobra.Command { func newRun(ctx context.DnoteCtx) infra.RunEFunc { return func(cmd *cobra.Command, args []string) error { - var run infra.RunEFunc - if len(args) == 0 { - run = ls.NewRun(ctx, nameOnly) + // List all books + return listBooks(ctx, os.Stdout, nameOnly) } else if len(args) == 1 { if nameOnly { return errors.New("--name-only flag is only valid when viewing books") } if utils.IsNumber(args[0]) { - run = cat.NewRun(ctx, contentOnly) + // View a note by index + return viewNote(ctx, os.Stdout, args[0], contentOnly) } else { - run = ls.NewRun(ctx, false) + // List notes in a book + return listNotes(ctx, os.Stdout, args[0]) } } else if len(args) == 2 { - // DEPRECATED: passing book name to view command is deprecated - run = cat.NewRun(ctx, false) - } else { - return errors.New("Incorrect number of arguments") + // View a note in a book (book name + note index) + return viewNote(ctx, os.Stdout, args[1], contentOnly) } - return run(cmd, args) + return errors.New("Incorrect number of arguments") } } diff --git a/pkg/cli/config/config.go b/pkg/cli/config/config.go index e12c5c45..065100c0 100644 --- a/pkg/cli/config/config.go +++ b/pkg/cli/config/config.go @@ -1,26 +1,23 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package config import ( "fmt" - "io/ioutil" + "os" "github.com/dnote/dnote/pkg/cli/consts" "github.com/dnote/dnote/pkg/cli/context" @@ -32,8 +29,9 @@ import ( // Config holds dnote configuration type Config struct { - Editor string `yaml:"editor"` - APIEndpoint string `yaml:"apiEndpoint"` + Editor string `yaml:"editor"` + APIEndpoint string `yaml:"apiEndpoint"` + EnableUpgradeCheck bool `yaml:"enableUpgradeCheck"` } func checkLegacyPath(ctx context.DnoteCtx) (string, bool) { @@ -41,7 +39,7 @@ func checkLegacyPath(ctx context.DnoteCtx) (string, bool) { ok, err := utils.FileExists(legacyPath) if err != nil { - log.Errorf(errors.Wrapf(err, "checking legacy dnote directory at %s", legacyPath).Error()) + log.Error(errors.Wrapf(err, "checking legacy dnote directory at %s", legacyPath).Error()) } if ok { return legacyPath, true @@ -65,7 +63,7 @@ func Read(ctx context.DnoteCtx) (Config, error) { var ret Config configPath := GetPath(ctx) - b, err := ioutil.ReadFile(configPath) + b, err := os.ReadFile(configPath) if err != nil { return ret, errors.Wrap(err, "reading config file") } @@ -87,7 +85,7 @@ func Write(ctx context.DnoteCtx, cf Config) error { return errors.Wrap(err, "marshalling config into YAML") } - err = ioutil.WriteFile(path, b, 0644) + err = os.WriteFile(path, b, 0644) if err != nil { return errors.Wrap(err, "writing the config file") } diff --git a/pkg/cli/consts/consts.go b/pkg/cli/consts/consts.go index 17a043c1..0f49c152 100644 --- a/pkg/cli/consts/consts.go +++ b/pkg/cli/consts/consts.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package consts provides definitions of constants diff --git a/pkg/cli/context/ctx.go b/pkg/cli/context/ctx.go index cfef776b..971d9145 100644 --- a/pkg/cli/context/ctx.go +++ b/pkg/cli/context/ctx.go @@ -1,25 +1,24 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package context defines dnote context package context import ( + "net/http" + "github.com/dnote/dnote/pkg/cli/database" "github.com/dnote/dnote/pkg/clock" ) @@ -35,14 +34,16 @@ type Paths struct { // DnoteCtx is a context holding the information of the current runtime type DnoteCtx struct { - Paths Paths - APIEndpoint string - Version string - DB *database.DB - SessionKey string - SessionKeyExpiry int64 - Editor string - Clock clock.Clock + Paths Paths + APIEndpoint string + Version string + DB *database.DB + SessionKey string + SessionKeyExpiry int64 + Editor string + Clock clock.Clock + EnableUpgradeCheck bool + HTTPClient *http.Client } // Redact replaces private information from the context with a set of diff --git a/pkg/cli/context/files.go b/pkg/cli/context/files.go new file mode 100644 index 00000000..1abbcd47 --- /dev/null +++ b/pkg/cli/context/files.go @@ -0,0 +1,48 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package context + +import ( + "path/filepath" + + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/pkg/errors" +) + +// InitDnoteDirs creates the dnote directories if they don't already exist. +func InitDnoteDirs(paths Paths) error { + if paths.Config != "" { + configDir := filepath.Join(paths.Config, consts.DnoteDirName) + if err := utils.EnsureDir(configDir); err != nil { + return errors.Wrap(err, "initializing config dir") + } + } + if paths.Data != "" { + dataDir := filepath.Join(paths.Data, consts.DnoteDirName) + if err := utils.EnsureDir(dataDir); err != nil { + return errors.Wrap(err, "initializing data dir") + } + } + if paths.Cache != "" { + cacheDir := filepath.Join(paths.Cache, consts.DnoteDirName) + if err := utils.EnsureDir(cacheDir); err != nil { + return errors.Wrap(err, "initializing cache dir") + } + } + + return nil +} diff --git a/pkg/cli/context/files_test.go b/pkg/cli/context/files_test.go new file mode 100644 index 00000000..49d62dc9 --- /dev/null +++ b/pkg/cli/context/files_test.go @@ -0,0 +1,62 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package context + +import ( + "os" + "path/filepath" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/consts" +) + +func assertDirsExist(t *testing.T, paths Paths) { + configDir := filepath.Join(paths.Config, consts.DnoteDirName) + info, err := os.Stat(configDir) + assert.Equal(t, err, nil, "config dir should exist") + assert.Equal(t, info.IsDir(), true, "config should be a directory") + + dataDir := filepath.Join(paths.Data, consts.DnoteDirName) + info, err = os.Stat(dataDir) + assert.Equal(t, err, nil, "data dir should exist") + assert.Equal(t, info.IsDir(), true, "data should be a directory") + + cacheDir := filepath.Join(paths.Cache, consts.DnoteDirName) + info, err = os.Stat(cacheDir) + assert.Equal(t, err, nil, "cache dir should exist") + assert.Equal(t, info.IsDir(), true, "cache should be a directory") +} + +func TestInitDnoteDirs(t *testing.T) { + tmpDir := t.TempDir() + + paths := Paths{ + Config: filepath.Join(tmpDir, "config"), + Data: filepath.Join(tmpDir, "data"), + Cache: filepath.Join(tmpDir, "cache"), + } + + // Initialize directories + err := InitDnoteDirs(paths) + assert.Equal(t, err, nil, "InitDnoteDirs should succeed") + assertDirsExist(t, paths) + + // Call again - should be idempotent + err = InitDnoteDirs(paths) + assert.Equal(t, err, nil, "InitDnoteDirs should succeed when dirs already exist") + assertDirsExist(t, paths) +} diff --git a/pkg/cli/context/testutils.go b/pkg/cli/context/testutils.go index bd268bf3..cb477475 100644 --- a/pkg/cli/context/testutils.go +++ b/pkg/cli/context/testutils.go @@ -1,26 +1,22 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package context import ( - "fmt" - "os" + "path/filepath" "testing" "github.com/dnote/dnote/pkg/cli/consts" @@ -29,11 +25,27 @@ import ( "github.com/pkg/errors" ) -// InitTestCtx initializes a test context -func InitTestCtx(t *testing.T, paths Paths, dbOpts *database.TestDBOptions) DnoteCtx { - dbPath := fmt.Sprintf("%s/%s/%s", paths.Data, consts.DnoteDirName, consts.DnoteDBFileName) +// getDefaultTestPaths creates default test paths with all paths pointing to a temp directory +func getDefaultTestPaths(t *testing.T) Paths { + tmpDir := t.TempDir() + return Paths{ + Home: tmpDir, + Cache: tmpDir, + Config: tmpDir, + Data: tmpDir, + } +} - db := database.InitTestDB(t, dbPath, dbOpts) + +// InitTestCtx initializes a test context with an in-memory database +// and a temporary directory for all paths +func InitTestCtx(t *testing.T) DnoteCtx { + paths := getDefaultTestPaths(t) + db := database.InitTestMemoryDB(t) + + if err := InitDnoteDirs(paths); err != nil { + t.Fatal(errors.Wrap(err, "creating test directories")) + } return DnoteCtx{ DB: db, @@ -42,17 +54,47 @@ func InitTestCtx(t *testing.T, paths Paths, dbOpts *database.TestDBOptions) Dnot } } -// TeardownTestCtx cleans up the test context -func TeardownTestCtx(t *testing.T, ctx DnoteCtx) { - database.TeardownTestDB(t, ctx.DB) +// InitTestCtxWithDB initializes a test context with the provided database +// and a temporary directory for all paths. +// Used when you need full control over database initialization (e.g. migration tests). +func InitTestCtxWithDB(t *testing.T, db *database.DB) DnoteCtx { + paths := getDefaultTestPaths(t) - if err := os.RemoveAll(ctx.Paths.Data); err != nil { - t.Fatal(errors.Wrap(err, "removing test data directory")) + if err := InitDnoteDirs(paths); err != nil { + t.Fatal(errors.Wrap(err, "creating test directories")) } - if err := os.RemoveAll(ctx.Paths.Config); err != nil { - t.Fatal(errors.Wrap(err, "removing test config directory")) - } - if err := os.RemoveAll(ctx.Paths.Cache); err != nil { - t.Fatal(errors.Wrap(err, "removing test cache directory")) + + return DnoteCtx{ + DB: db, + Paths: paths, + Clock: clock.NewMock(), // Use a mock clock to test times + } +} + +// InitTestCtxWithFileDB initializes a test context with a file-based database +// at the expected path. +func InitTestCtxWithFileDB(t *testing.T) DnoteCtx { + paths := getDefaultTestPaths(t) + + if err := InitDnoteDirs(paths); err != nil { + t.Fatal(errors.Wrap(err, "creating test directories")) + } + + dbPath := filepath.Join(paths.Data, consts.DnoteDirName, consts.DnoteDBFileName) + db, err := database.Open(dbPath) + if err != nil { + t.Fatal(errors.Wrap(err, "opening database")) + } + + if _, err := db.Exec(database.GetDefaultSchemaSQL()); err != nil { + t.Fatal(errors.Wrap(err, "running schema sql")) + } + + t.Cleanup(func() { db.Close() }) + + return DnoteCtx{ + DB: db, + Paths: paths, + Clock: clock.NewMock(), // Use a mock clock to test times } } diff --git a/pkg/cli/crypt/crypto.go b/pkg/cli/crypt/crypto.go deleted file mode 100644 index c61efc1d..00000000 --- a/pkg/cli/crypt/crypto.go +++ /dev/null @@ -1,123 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . - */ - -// Package crypt provides cryptographic funcitonalities -package crypt - -import ( - "crypto/aes" - "crypto/cipher" - "crypto/rand" - "crypto/sha256" - "encoding/base64" - "io" - - "github.com/pkg/errors" - "golang.org/x/crypto/hkdf" - "golang.org/x/crypto/pbkdf2" -) - -var aesGcmNonceSize = 12 - -func runHkdf(secret, salt, info []byte) ([]byte, error) { - r := hkdf.New(sha256.New, secret, salt, info) - - ret := make([]byte, 32) - _, err := io.ReadFull(r, ret) - if err != nil { - return []byte{}, errors.Wrap(err, "reading key bytes") - } - - return ret, nil -} - -// MakeKeys derives, from the given credential, a key set comprising of an encryption key -// and an authentication key -func MakeKeys(password, email []byte, iteration int) ([]byte, []byte, error) { - masterKey := pbkdf2.Key([]byte(password), []byte(email), iteration, 32, sha256.New) - - authKey, err := runHkdf(masterKey, email, []byte("auth")) - if err != nil { - return nil, nil, errors.Wrap(err, "deriving auth key") - } - - return masterKey, authKey, nil -} - -// AesGcmEncrypt encrypts the plaintext using AES in a GCM mode. It returns -// a ciphertext prepended by a 12 byte pseudo-random nonce, encoded in base64. -func AesGcmEncrypt(key, plaintext []byte) (string, error) { - if key == nil { - return "", errors.New("no key provided") - } - - block, err := aes.NewCipher(key) - if err != nil { - return "", errors.Wrap(err, "initializing aes") - } - - aesgcm, err := cipher.NewGCM(block) - if err != nil { - return "", errors.Wrap(err, "initializing gcm") - } - - nonce := make([]byte, aesGcmNonceSize) - if _, err := io.ReadFull(rand.Reader, nonce); err != nil { - return "", errors.Wrap(err, "generating nonce") - } - - ciphertext := aesgcm.Seal(nonce, nonce, []byte(plaintext), nil) - cipherKeyB64 := base64.StdEncoding.EncodeToString(ciphertext) - - return cipherKeyB64, nil -} - -// AesGcmDecrypt decrypts the encrypted data using AES in a GCM mode. The data should be -// a base64 encoded string in the format of 12 byte nonce followed by a ciphertext. -func AesGcmDecrypt(key []byte, dataB64 string) ([]byte, error) { - if key == nil { - return nil, errors.New("no key provided") - } - - data, err := base64.StdEncoding.DecodeString(dataB64) - if err != nil { - return nil, errors.Wrap(err, "decoding base64 data") - } - - block, err := aes.NewCipher(key) - if err != nil { - return nil, errors.Wrap(err, "initializing aes") - } - - aesgcm, err := cipher.NewGCM(block) - if err != nil { - return nil, errors.Wrap(err, "initializing gcm") - } - - if len(data) < aesGcmNonceSize { - return nil, errors.Wrap(err, "malformed data") - } - - nonce, ciphertext := data[:aesGcmNonceSize], data[aesGcmNonceSize:] - plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil) - if err != nil { - return nil, errors.Wrap(err, "decrypting") - } - - return plaintext, nil -} diff --git a/pkg/cli/crypt/crypto_test.go b/pkg/cli/crypt/crypto_test.go deleted file mode 100644 index f5864d3f..00000000 --- a/pkg/cli/crypt/crypto_test.go +++ /dev/null @@ -1,118 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . - */ - -package crypt - -import ( - "crypto/aes" - "crypto/cipher" - "encoding/base64" - "fmt" - "testing" - - "github.com/dnote/dnote/pkg/assert" - "github.com/pkg/errors" -) - -func TestAesGcmEncrypt(t *testing.T) { - testCases := []struct { - key []byte - plaintext []byte - }{ - { - key: []byte("AES256Key-32Characters1234567890"), - plaintext: []byte("foo bar baz quz"), - }, - { - key: []byte("AES256Key-32Charactersabcdefghij"), - plaintext: []byte("1234 foo 5678 bar 7890 baz"), - }, - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("key %s plaintext %s", tc.key, tc.plaintext), func(t *testing.T) { - // encrypt - dataB64, err := AesGcmEncrypt(tc.key, tc.plaintext) - if err != nil { - t.Fatal(errors.Wrap(err, "performing encryption")) - } - - // test that data can be decrypted - data, err := base64.StdEncoding.DecodeString(dataB64) - if err != nil { - t.Fatal(errors.Wrap(err, "decoding data from base64")) - } - - nonce, ciphertext := data[:12], data[12:] - - fmt.Println(string(data)) - - block, err := aes.NewCipher([]byte(tc.key)) - if err != nil { - t.Fatal(errors.Wrap(err, "initializing aes")) - } - - aesgcm, err := cipher.NewGCM(block) - if err != nil { - t.Fatal(errors.Wrap(err, "initializing gcm")) - } - - plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil) - if err != nil { - t.Fatal(errors.Wrap(err, "decode")) - } - - assert.DeepEqual(t, plaintext, tc.plaintext, "plaintext mismatch") - }) - } -} - -func TestAesGcmDecrypt(t *testing.T) { - testCases := []struct { - key []byte - ciphertextB64 string - expectedPlaintext string - }{ - { - key: []byte("AES256Key-32Characters1234567890"), - ciphertextB64: "M2ov9hWMQ52v1S/zigwX3bJt4cVCV02uiRm/grKqN/rZxNkJrD7vK4Ii0g==", - expectedPlaintext: "foo bar baz quz", - }, - { - key: []byte("AES256Key-32Characters1234567890"), - ciphertextB64: "M4csFKUIUbD1FBEzLgHjscoKgN0lhMGJ0n2nKWiCkE/qSKlRP7kS", - expectedPlaintext: "foo\n1\nbar\n2", - }, - { - key: []byte("AES256Key-32Characters1234567890"), - ciphertextB64: "pe/fnw73MR1clmVIlRSJ5gDwBdnPly/DF7DsR5dJVz4dHZlv0b10WzvJEGOCHZEr+Q==", - expectedPlaintext: "föo\nbār\nbåz & qūz", - }, - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("key %s ciphertext %s", tc.key, tc.ciphertextB64), func(t *testing.T) { - plaintext, err := AesGcmDecrypt(tc.key, tc.ciphertextB64) - if err != nil { - t.Fatal(errors.Wrap(err, "performing decryption")) - } - - assert.DeepEqual(t, plaintext, []byte(tc.expectedPlaintext), "plaintext mismatch") - }) - } -} diff --git a/pkg/cli/database/models.go b/pkg/cli/database/models.go index 8330ccf2..e8e463bb 100644 --- a/pkg/cli/database/models.go +++ b/pkg/cli/database/models.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package database @@ -41,13 +38,12 @@ type Note struct { AddedOn int64 `json:"added_on"` EditedOn int64 `json:"edited_on"` USN int `json:"usn"` - Public bool `json:"public"` Deleted bool `json:"deleted"` Dirty bool `json:"dirty"` } // NewNote constructs a note with the given data -func NewNote(uuid, bookUUID, body string, addedOn, editedOn int64, usn int, public, deleted, dirty bool) Note { +func NewNote(uuid, bookUUID, body string, addedOn, editedOn int64, usn int, deleted, dirty bool) Note { return Note{ UUID: uuid, BookUUID: bookUUID, @@ -55,7 +51,6 @@ func NewNote(uuid, bookUUID, body string, addedOn, editedOn int64, usn int, publ AddedOn: addedOn, EditedOn: editedOn, USN: usn, - Public: public, Deleted: deleted, Dirty: dirty, } @@ -63,8 +58,8 @@ func NewNote(uuid, bookUUID, body string, addedOn, editedOn int64, usn int, publ // Insert inserts a new note func (n Note) Insert(db *DB) error { - _, err := db.Exec("INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", - n.UUID, n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Public, n.Deleted, n.Dirty) + _, err := db.Exec("INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + n.UUID, n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Deleted, n.Dirty) if err != nil { return errors.Wrapf(err, "inserting note with uuid %s", n.UUID) @@ -75,8 +70,8 @@ func (n Note) Insert(db *DB) error { // Update updates the note with the given data func (n Note) Update(db *DB) error { - _, err := db.Exec("UPDATE notes SET book_uuid = ?, body = ?, added_on = ?, edited_on = ?, usn = ?, public = ?, deleted = ?, dirty = ? WHERE uuid = ?", - n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Public, n.Deleted, n.Dirty, n.UUID) + _, err := db.Exec("UPDATE notes SET book_uuid = ?, body = ?, added_on = ?, edited_on = ?, usn = ?, deleted = ?, dirty = ? WHERE uuid = ?", + n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Deleted, n.Dirty, n.UUID) if err != nil { return errors.Wrapf(err, "updating the note with uuid %s", n.UUID) diff --git a/pkg/cli/database/models_test.go b/pkg/cli/database/models_test.go index 4e5b6557..6d0a45f0 100644 --- a/pkg/cli/database/models_test.go +++ b/pkg/cli/database/models_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package database @@ -34,7 +31,6 @@ func TestNewNote(t *testing.T) { addedOn int64 editedOn int64 usn int - public bool deleted bool dirty bool }{ @@ -45,7 +41,6 @@ func TestNewNote(t *testing.T) { addedOn: 1542058875, editedOn: 0, usn: 0, - public: false, deleted: false, dirty: false, }, @@ -56,14 +51,13 @@ func TestNewNote(t *testing.T) { addedOn: 1542058875, editedOn: 1542058876, usn: 1008, - public: true, deleted: true, dirty: true, }, } for idx, tc := range testCases { - got := NewNote(tc.uuid, tc.bookUUID, tc.body, tc.addedOn, tc.editedOn, tc.usn, tc.public, tc.deleted, tc.dirty) + got := NewNote(tc.uuid, tc.bookUUID, tc.body, tc.addedOn, tc.editedOn, tc.usn, tc.deleted, tc.dirty) assert.Equal(t, got.UUID, tc.uuid, fmt.Sprintf("UUID mismatch for test case %d", idx)) assert.Equal(t, got.BookUUID, tc.bookUUID, fmt.Sprintf("BookUUID mismatch for test case %d", idx)) @@ -71,7 +65,6 @@ func TestNewNote(t *testing.T) { assert.Equal(t, got.AddedOn, tc.addedOn, fmt.Sprintf("AddedOn mismatch for test case %d", idx)) assert.Equal(t, got.EditedOn, tc.editedOn, fmt.Sprintf("EditedOn mismatch for test case %d", idx)) assert.Equal(t, got.USN, tc.usn, fmt.Sprintf("USN mismatch for test case %d", idx)) - assert.Equal(t, got.Public, tc.public, fmt.Sprintf("Public mismatch for test case %d", idx)) assert.Equal(t, got.Deleted, tc.deleted, fmt.Sprintf("Deleted mismatch for test case %d", idx)) assert.Equal(t, got.Dirty, tc.dirty, fmt.Sprintf("Dirty mismatch for test case %d", idx)) } @@ -85,7 +78,6 @@ func TestNoteInsert(t *testing.T) { addedOn int64 editedOn int64 usn int - public bool deleted bool dirty bool }{ @@ -96,7 +88,6 @@ func TestNoteInsert(t *testing.T) { addedOn: 1542058875, editedOn: 0, usn: 0, - public: false, deleted: false, dirty: false, }, @@ -107,7 +98,6 @@ func TestNoteInsert(t *testing.T) { addedOn: 1542058875, editedOn: 1542058876, usn: 1008, - public: true, deleted: true, dirty: true, }, @@ -116,8 +106,7 @@ func TestNoteInsert(t *testing.T) { for idx, tc := range testCases { func() { // Setup - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) n := Note{ UUID: tc.uuid, @@ -126,7 +115,6 @@ func TestNoteInsert(t *testing.T) { AddedOn: tc.addedOn, EditedOn: tc.editedOn, USN: tc.usn, - Public: tc.public, Deleted: tc.deleted, Dirty: tc.dirty, } @@ -134,12 +122,12 @@ func TestNoteInsert(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } if err := n.Insert(tx); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -148,10 +136,10 @@ func TestNoteInsert(t *testing.T) { var uuid, bookUUID, body string var addedOn, editedOn int64 var usn int - var public, deleted, dirty bool + var deleted, dirty bool MustScan(t, "getting n1", - db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty FROM notes WHERE uuid = ?", tc.uuid), - &uuid, &bookUUID, &body, &addedOn, &editedOn, &usn, &public, &deleted, &dirty) + db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty FROM notes WHERE uuid = ?", tc.uuid), + &uuid, &bookUUID, &body, &addedOn, &editedOn, &usn, &deleted, &dirty) assert.Equal(t, uuid, tc.uuid, fmt.Sprintf("uuid mismatch for test case %d", idx)) assert.Equal(t, bookUUID, tc.bookUUID, fmt.Sprintf("bookUUID mismatch for test case %d", idx)) @@ -159,7 +147,6 @@ func TestNoteInsert(t *testing.T) { assert.Equal(t, addedOn, tc.addedOn, fmt.Sprintf("addedOn mismatch for test case %d", idx)) assert.Equal(t, editedOn, tc.editedOn, fmt.Sprintf("editedOn mismatch for test case %d", idx)) assert.Equal(t, usn, tc.usn, fmt.Sprintf("usn mismatch for test case %d", idx)) - assert.Equal(t, public, tc.public, fmt.Sprintf("public mismatch for test case %d", idx)) assert.Equal(t, deleted, tc.deleted, fmt.Sprintf("deleted mismatch for test case %d", idx)) assert.Equal(t, dirty, tc.dirty, fmt.Sprintf("dirty mismatch for test case %d", idx)) }() @@ -174,14 +161,12 @@ func TestNoteUpdate(t *testing.T) { addedOn int64 editedOn int64 usn int - public bool deleted bool dirty bool newBookUUID string newBody string newEditedOn int64 newUSN int - newPublic bool newDeleted bool newDirty bool }{ @@ -192,14 +177,12 @@ func TestNoteUpdate(t *testing.T) { addedOn: 1542058875, editedOn: 0, usn: 0, - public: false, deleted: false, dirty: false, newBookUUID: "b1-uuid", newBody: "n1-body edited", newEditedOn: 1542058879, newUSN: 0, - newPublic: false, newDeleted: false, newDirty: false, }, @@ -210,14 +193,12 @@ func TestNoteUpdate(t *testing.T) { addedOn: 1542058875, editedOn: 0, usn: 0, - public: false, deleted: false, dirty: true, newBookUUID: "b2-uuid", newBody: "n1-body", newEditedOn: 1542058879, newUSN: 0, - newPublic: true, newDeleted: false, newDirty: false, }, @@ -228,14 +209,12 @@ func TestNoteUpdate(t *testing.T) { addedOn: 1542058875, editedOn: 0, usn: 10, - public: false, deleted: false, dirty: false, newBookUUID: "", newBody: "", newEditedOn: 1542058879, newUSN: 151, - newPublic: false, newDeleted: true, newDirty: false, }, @@ -246,14 +225,12 @@ func TestNoteUpdate(t *testing.T) { addedOn: 1542058875, editedOn: 0, usn: 0, - public: false, deleted: false, dirty: false, newBookUUID: "", newBody: "", newEditedOn: 1542058879, newUSN: 15, - newPublic: false, newDeleted: true, newDirty: false, }, @@ -262,8 +239,7 @@ func TestNoteUpdate(t *testing.T) { for idx, tc := range testCases { func() { // Setup - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) n1 := Note{ UUID: tc.uuid, @@ -272,7 +248,6 @@ func TestNoteUpdate(t *testing.T) { AddedOn: tc.addedOn, EditedOn: tc.editedOn, USN: tc.usn, - Public: tc.public, Deleted: tc.deleted, Dirty: tc.dirty, } @@ -283,31 +258,29 @@ func TestNoteUpdate(t *testing.T) { AddedOn: 1542058875, EditedOn: 0, USN: 39, - Public: false, Deleted: false, Dirty: false, } - MustExec(t, fmt.Sprintf("inserting n1 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.USN, n1.AddedOn, n1.EditedOn, n1.Body, n1.Public, n1.Deleted, n1.Dirty) - MustExec(t, fmt.Sprintf("inserting n2 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.USN, n2.AddedOn, n2.EditedOn, n2.Body, n2.Public, n2.Deleted, n2.Dirty) + MustExec(t, fmt.Sprintf("inserting n1 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.USN, n1.AddedOn, n1.EditedOn, n1.Body, n1.Deleted, n1.Dirty) + MustExec(t, fmt.Sprintf("inserting n2 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.USN, n2.AddedOn, n2.EditedOn, n2.Body, n2.Deleted, n2.Dirty) // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } n1.BookUUID = tc.newBookUUID n1.Body = tc.newBody n1.EditedOn = tc.newEditedOn n1.USN = tc.newUSN - n1.Public = tc.newPublic n1.Deleted = tc.newDeleted n1.Dirty = tc.newDirty if err := n1.Update(tx); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -315,11 +288,11 @@ func TestNoteUpdate(t *testing.T) { // test var n1Record, n2Record Note MustScan(t, "getting n1", - db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty FROM notes WHERE uuid = ?", tc.uuid), - &n1Record.UUID, &n1Record.BookUUID, &n1Record.Body, &n1Record.AddedOn, &n1Record.EditedOn, &n1Record.USN, &n1Record.Public, &n1Record.Deleted, &n1Record.Dirty) + db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty FROM notes WHERE uuid = ?", tc.uuid), + &n1Record.UUID, &n1Record.BookUUID, &n1Record.Body, &n1Record.AddedOn, &n1Record.EditedOn, &n1Record.USN, &n1Record.Deleted, &n1Record.Dirty) MustScan(t, "getting n2", - db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty FROM notes WHERE uuid = ?", n2.UUID), - &n2Record.UUID, &n2Record.BookUUID, &n2Record.Body, &n2Record.AddedOn, &n2Record.EditedOn, &n2Record.USN, &n2Record.Public, &n2Record.Deleted, &n2Record.Dirty) + db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty FROM notes WHERE uuid = ?", n2.UUID), + &n2Record.UUID, &n2Record.BookUUID, &n2Record.Body, &n2Record.AddedOn, &n2Record.EditedOn, &n2Record.USN, &n2Record.Deleted, &n2Record.Dirty) assert.Equal(t, n1Record.UUID, n1.UUID, fmt.Sprintf("n1 uuid mismatch for test case %d", idx)) assert.Equal(t, n1Record.BookUUID, tc.newBookUUID, fmt.Sprintf("n1 bookUUID mismatch for test case %d", idx)) @@ -327,7 +300,6 @@ func TestNoteUpdate(t *testing.T) { assert.Equal(t, n1Record.AddedOn, n1.AddedOn, fmt.Sprintf("n1 addedOn mismatch for test case %d", idx)) assert.Equal(t, n1Record.EditedOn, tc.newEditedOn, fmt.Sprintf("n1 editedOn mismatch for test case %d", idx)) assert.Equal(t, n1Record.USN, tc.newUSN, fmt.Sprintf("n1 usn mismatch for test case %d", idx)) - assert.Equal(t, n1Record.Public, tc.newPublic, fmt.Sprintf("n1 public mismatch for test case %d", idx)) assert.Equal(t, n1Record.Deleted, tc.newDeleted, fmt.Sprintf("n1 deleted mismatch for test case %d", idx)) assert.Equal(t, n1Record.Dirty, tc.newDirty, fmt.Sprintf("n1 dirty mismatch for test case %d", idx)) @@ -337,7 +309,6 @@ func TestNoteUpdate(t *testing.T) { assert.Equal(t, n2Record.AddedOn, n2.AddedOn, fmt.Sprintf("n2 addedOn mismatch for test case %d", idx)) assert.Equal(t, n2Record.EditedOn, n2.EditedOn, fmt.Sprintf("n2 editedOn mismatch for test case %d", idx)) assert.Equal(t, n2Record.USN, n2.USN, fmt.Sprintf("n2 usn mismatch for test case %d", idx)) - assert.Equal(t, n2Record.Public, n2.Public, fmt.Sprintf("n2 public mismatch for test case %d", idx)) assert.Equal(t, n2Record.Deleted, n2.Deleted, fmt.Sprintf("n2 deleted mismatch for test case %d", idx)) assert.Equal(t, n2Record.Dirty, n2.Dirty, fmt.Sprintf("n2 dirty mismatch for test case %d", idx)) }() @@ -359,8 +330,7 @@ func TestNoteUpdateUUID(t *testing.T) { for idx, tc := range testCases { t.Run(fmt.Sprintf("testCase%d", idx), func(t *testing.T) { // Setup - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) n1 := Note{ UUID: "n1-uuid", @@ -387,11 +357,11 @@ func TestNoteUpdateUUID(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if err := n1.UpdateUUID(tx, tc.newUUID); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -414,8 +384,7 @@ func TestNoteUpdateUUID(t *testing.T) { func TestNoteExpunge(t *testing.T) { // Setup - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) n1 := Note{ UUID: "n1-uuid", @@ -424,7 +393,6 @@ func TestNoteExpunge(t *testing.T) { AddedOn: 1542058874, EditedOn: 0, USN: 22, - Public: false, Deleted: false, Dirty: false, } @@ -435,23 +403,22 @@ func TestNoteExpunge(t *testing.T) { AddedOn: 1542058875, EditedOn: 0, USN: 39, - Public: false, Deleted: false, Dirty: false, } - MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.USN, n1.AddedOn, n1.EditedOn, n1.Body, n1.Public, n1.Deleted, n1.Dirty) - MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.USN, n2.AddedOn, n2.EditedOn, n2.Body, n2.Public, n2.Deleted, n2.Dirty) + MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.USN, n1.AddedOn, n1.EditedOn, n1.Body, n1.Deleted, n1.Dirty) + MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.USN, n2.AddedOn, n2.EditedOn, n2.Body, n2.Deleted, n2.Dirty) // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if err := n1.Expunge(tx); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -464,8 +431,8 @@ func TestNoteExpunge(t *testing.T) { var n2Record Note MustScan(t, "getting n2", - db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty FROM notes WHERE uuid = ?", n2.UUID), - &n2Record.UUID, &n2Record.BookUUID, &n2Record.Body, &n2Record.AddedOn, &n2Record.EditedOn, &n2Record.USN, &n2Record.Public, &n2Record.Deleted, &n2Record.Dirty) + db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty FROM notes WHERE uuid = ?", n2.UUID), + &n2Record.UUID, &n2Record.BookUUID, &n2Record.Body, &n2Record.AddedOn, &n2Record.EditedOn, &n2Record.USN, &n2Record.Deleted, &n2Record.Dirty) assert.Equal(t, n2Record.UUID, n2.UUID, "n2 uuid mismatch") assert.Equal(t, n2Record.BookUUID, n2.BookUUID, "n2 bookUUID mismatch") @@ -473,7 +440,6 @@ func TestNoteExpunge(t *testing.T) { assert.Equal(t, n2Record.AddedOn, n2.AddedOn, "n2 addedOn mismatch") assert.Equal(t, n2Record.EditedOn, n2.EditedOn, "n2 editedOn mismatch") assert.Equal(t, n2Record.USN, n2.USN, "n2 usn mismatch") - assert.Equal(t, n2Record.Public, n2.Public, "n2 public mismatch") assert.Equal(t, n2Record.Deleted, n2.Deleted, "n2 deleted mismatch") assert.Equal(t, n2Record.Dirty, n2.Dirty, "n2 dirty mismatch") } @@ -540,8 +506,7 @@ func TestBookInsert(t *testing.T) { for idx, tc := range testCases { func() { // Setup - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) b := Book{ UUID: tc.uuid, @@ -555,12 +520,12 @@ func TestBookInsert(t *testing.T) { tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } if err := b.Insert(tx); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -621,8 +586,7 @@ func TestBookUpdate(t *testing.T) { for idx, tc := range testCases { func() { // Setup - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) b1 := Book{ UUID: "b1-uuid", @@ -645,7 +609,7 @@ func TestBookUpdate(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) } b1.Label = tc.newLabel @@ -655,7 +619,7 @@ func TestBookUpdate(t *testing.T) { if err := b1.Update(tx); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) } tx.Commit() @@ -700,8 +664,7 @@ func TestBookUpdateUUID(t *testing.T) { t.Run(fmt.Sprintf("testCase%d", idx), func(t *testing.T) { // Setup - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) b1 := Book{ UUID: "b1-uuid", @@ -724,11 +687,11 @@ func TestBookUpdateUUID(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if err := b1.UpdateUUID(tx, tc.newUUID); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -751,8 +714,7 @@ func TestBookUpdateUUID(t *testing.T) { func TestBookExpunge(t *testing.T) { // Setup - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) b1 := Book{ UUID: "b1-uuid", @@ -775,12 +737,12 @@ func TestBookExpunge(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if err := b1.Expunge(tx); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) + t.Fatal(errors.Wrap(err, "executing").Error()) } tx.Commit() @@ -806,8 +768,7 @@ func TestBookExpunge(t *testing.T) { // TestNoteFTS tests that note full text search indices stay in sync with the notes after insert, update and delete func TestNoteFTS(t *testing.T) { // set up - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) // execute - insert n := Note{ @@ -817,19 +778,18 @@ func TestNoteFTS(t *testing.T) { AddedOn: 1542058875, EditedOn: 0, USN: 0, - Public: false, Deleted: false, Dirty: false, } tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if err := n.Insert(tx); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "inserting").Error()) + t.Fatal(errors.Wrap(err, "inserting").Error()) } tx.Commit() @@ -847,13 +807,13 @@ func TestNoteFTS(t *testing.T) { // execute - update tx, err = db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } n.Body = "baz quz" if err := n.Update(tx); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "updating").Error()) + t.Fatal(errors.Wrap(err, "updating").Error()) } tx.Commit() @@ -872,12 +832,12 @@ func TestNoteFTS(t *testing.T) { // execute - delete tx, err = db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if err := n.Expunge(tx); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "expunging").Error()) + t.Fatal(errors.Wrap(err, "expunging").Error()) } tx.Commit() diff --git a/pkg/cli/database/queries.go b/pkg/cli/database/queries.go index 096ffed5..2209c8f3 100644 --- a/pkg/cli/database/queries.go +++ b/pkg/cli/database/queries.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package database @@ -170,7 +167,6 @@ func GetActiveNote(db *DB, rowid int) (Note, error) { added_on, edited_on, usn, - public, deleted, dirty FROM notes WHERE rowid = ? AND deleted = false;`, rowid).Scan( @@ -181,7 +177,6 @@ func GetActiveNote(db *DB, rowid int) (Note, error) { &ret.AddedOn, &ret.EditedOn, &ret.USN, - &ret.Public, &ret.Deleted, &ret.Dirty, ) diff --git a/pkg/cli/database/queries_test.go b/pkg/cli/database/queries_test.go index 5a460a8b..e394fe95 100644 --- a/pkg/cli/database/queries_test.go +++ b/pkg/cli/database/queries_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package database @@ -47,18 +44,17 @@ func TestInsertSystem(t *testing.T) { for _, tc := range testCases { t.Run(fmt.Sprintf("insert %s %s", tc.key, tc.val), func(t *testing.T) { // Setup - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if err := InsertSystem(tx, tc.key, tc.val); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing for test case").Error()) + t.Fatal(errors.Wrap(err, "executing for test case").Error()) } tx.Commit() @@ -95,8 +91,7 @@ func TestUpsertSystem(t *testing.T) { for _, tc := range testCases { t.Run(fmt.Sprintf("insert %s %s", tc.key, tc.val), func(t *testing.T) { // Setup - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "baz", "quz") @@ -106,12 +101,12 @@ func TestUpsertSystem(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if err := UpsertSystem(tx, tc.key, tc.val); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing for test case").Error()) + t.Fatal(errors.Wrap(err, "executing for test case").Error()) } tx.Commit() @@ -134,20 +129,19 @@ func TestUpsertSystem(t *testing.T) { func TestGetSystem(t *testing.T) { t.Run(fmt.Sprintf("get string value"), func(t *testing.T) { // Setup - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) // execute MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "foo", "bar") tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } var dest string if err := GetSystem(tx, "foo", &dest); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing for test case").Error()) + t.Fatal(errors.Wrap(err, "executing for test case").Error()) } tx.Commit() @@ -157,20 +151,19 @@ func TestGetSystem(t *testing.T) { t.Run(fmt.Sprintf("get int64 value"), func(t *testing.T) { // Setup - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) // execute MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "foo", 1234) tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } var dest int64 if err := GetSystem(tx, "foo", &dest); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing for test case").Error()) + t.Fatal(errors.Wrap(err, "executing for test case").Error()) } tx.Commit() @@ -198,8 +191,7 @@ func TestUpdateSystem(t *testing.T) { for _, tc := range testCases { t.Run(fmt.Sprintf("update %s %s", tc.key, tc.val), func(t *testing.T) { // Setup - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "foo", "fuz") MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "baz", "quz") @@ -210,12 +202,12 @@ func TestUpdateSystem(t *testing.T) { // execute tx, err := db.Begin() if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) } if err := UpdateSystem(tx, tc.key, tc.val); err != nil { tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing for test case").Error()) + t.Fatal(errors.Wrap(err, "executing for test case").Error()) } tx.Commit() @@ -238,11 +230,10 @@ func TestUpdateSystem(t *testing.T) { func TestGetActiveNote(t *testing.T) { t.Run("not deleted", func(t *testing.T) { // set up - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) n1UUID := "n1-uuid" - MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, "b1-uuid", "n1 content", 1542058875, 1542058876, 1, true, false, true) + MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, "b1-uuid", "n1 content", 1542058875, 1542058876, 1, false, true) var n1RowID int MustScan(t, "getting rowid", db.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", n1UUID), &n1RowID) @@ -261,18 +252,16 @@ func TestGetActiveNote(t *testing.T) { assert.Equal(t, got.AddedOn, int64(1542058875), "AddedOn mismatch") assert.Equal(t, got.EditedOn, int64(1542058876), "EditedOn mismatch") assert.Equal(t, got.USN, 1, "USN mismatch") - assert.Equal(t, got.Public, true, "Public mismatch") assert.Equal(t, got.Deleted, false, "Deleted mismatch") assert.Equal(t, got.Dirty, true, "Dirty mismatch") }) t.Run("deleted", func(t *testing.T) { // set up - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) n1UUID := "n1-uuid" - MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, "b1-uuid", "n1 content", 1542058875, 1542058876, 1, true, true, true) + MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, "b1-uuid", "n1 content", 1542058875, 1542058876, 1, true, true) var n1RowID int MustScan(t, "getting rowid", db.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", n1UUID), &n1RowID) @@ -292,11 +281,10 @@ func TestGetActiveNote(t *testing.T) { func TestUpdateNoteContent(t *testing.T) { // set up - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) uuid := "n1-uuid" - MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", uuid, "b1-uuid", "n1 content", 1542058875, 0, 1, false, false, false) + MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", uuid, "b1-uuid", "n1 content", 1542058875, 0, 1, false, false) var rowid int MustScan(t, "getting rowid", db.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", uuid), &rowid) @@ -324,8 +312,7 @@ func TestUpdateNoteContent(t *testing.T) { func TestUpdateNoteBook(t *testing.T) { // set up - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) b1UUID := "b1-uuid" b2UUID := "b2-uuid" @@ -333,7 +320,7 @@ func TestUpdateNoteBook(t *testing.T) { MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b2UUID, "b2-label", 9, false, false) uuid := "n1-uuid" - MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", uuid, b1UUID, "n1 content", 1542058875, 0, 1, false, false, false) + MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", uuid, b1UUID, "n1 content", 1542058875, 0, 1, false, false) var rowid int MustScan(t, "getting rowid", db.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", uuid), &rowid) @@ -361,8 +348,7 @@ func TestUpdateNoteBook(t *testing.T) { func TestUpdateBookName(t *testing.T) { // set up - db := InitTestDB(t, "../tmp/dnote-test.db", nil) - defer TeardownTestDB(t, db) + db := InitTestMemoryDB(t) b1UUID := "b1-uuid" MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b1UUID, "b1-label", 8, false, false) diff --git a/pkg/cli/database/schema.sql b/pkg/cli/database/schema.sql new file mode 100644 index 00000000..9a9de094 --- /dev/null +++ b/pkg/cli/database/schema.sql @@ -0,0 +1,40 @@ +-- This is the final state of the CLI database after all migrations. +-- Auto-generated by generate-schema.go. Do not edit manually. +CREATE TABLE books + ( + uuid text PRIMARY KEY, + label text NOT NULL + , dirty bool DEFAULT false, usn int DEFAULT 0 NOT NULL, deleted bool DEFAULT false); +CREATE TABLE system + ( + key string NOT NULL, + value text NOT NULL + ); +CREATE UNIQUE INDEX idx_books_label ON books(label); +CREATE UNIQUE INDEX idx_books_uuid ON books(uuid); +CREATE TABLE "notes" + ( + uuid text NOT NULL, + book_uuid text NOT NULL, + body text NOT NULL, + added_on integer NOT NULL, + edited_on integer DEFAULT 0, + dirty bool DEFAULT false, + usn int DEFAULT 0 NOT NULL, + deleted bool DEFAULT false + ); +CREATE VIRTUAL TABLE note_fts USING fts5(content=notes, body, tokenize="porter unicode61 categories 'L* N* Co Ps Pe'"); +CREATE TRIGGER notes_after_insert AFTER INSERT ON notes BEGIN + INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); + END; +CREATE TRIGGER notes_after_delete AFTER DELETE ON notes BEGIN + INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); + END; +CREATE TRIGGER notes_after_update AFTER UPDATE ON notes BEGIN + INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); + INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); + END; + +-- Migration version data. +INSERT INTO system (key, value) VALUES ('schema', 14); +INSERT INTO system (key, value) VALUES ('remote_schema', 1); diff --git a/pkg/cli/database/schema/main.go b/pkg/cli/database/schema/main.go new file mode 100644 index 00000000..6c75e4d0 --- /dev/null +++ b/pkg/cli/database/schema/main.go @@ -0,0 +1,163 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Command schema generates the CLI database schema.sql file. +package main + +import ( + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/dnote/dnote/pkg/cli/config" + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/infra" + "github.com/dnote/dnote/pkg/cli/migrate" +) + +func main() { + tmpDir, err := os.MkdirTemp("", "dnote-schema-gen-*") + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(1) + } + defer os.RemoveAll(tmpDir) + + schemaPath := filepath.Join("pkg", "cli", "database", "schema.sql") + + if err := run(tmpDir, schemaPath); err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(1) + } +} + +func run(tmpDir, outputPath string) error { + schema, err := generateSchema(tmpDir) + if err != nil { + return err + } + + if err := os.WriteFile(outputPath, []byte(schema), 0644); err != nil { + return fmt.Errorf("writing schema file: %w", err) + } + + fmt.Printf("Schema generated successfully at %s\n", outputPath) + return nil +} + +// generateSchema creates a fresh database, runs all migrations, and extracts the schema +func generateSchema(tmpDir string) (string, error) { + // Create dnote directory structure in temp dir + dnoteDir := filepath.Join(tmpDir, "dnote") + if err := os.MkdirAll(dnoteDir, 0755); err != nil { + return "", fmt.Errorf("creating dnote dir: %w", err) + } + + // Use a file-based database + dbPath := filepath.Join(tmpDir, "schema.db") + + // Create context + ctx := context.DnoteCtx{ + Paths: context.Paths{ + Home: tmpDir, + Config: tmpDir, + Data: tmpDir, + Cache: tmpDir, + }, + Version: "schema-gen", + } + + // Open database + db, err := database.Open(dbPath) + if err != nil { + return "", fmt.Errorf("opening database: %w", err) + } + defer db.Close() + ctx.DB = db + + // Initialize database with base tables + if err := infra.InitDB(ctx); err != nil { + return "", fmt.Errorf("initializing database: %w", err) + } + + // Initialize system data + if err := infra.InitSystem(ctx); err != nil { + return "", fmt.Errorf("initializing system: %w", err) + } + + // Create minimal config file + if err := config.Write(ctx, config.Config{}); err != nil { + return "", fmt.Errorf("writing initial config: %w", err) + } + + // Run all local migrations + if err := migrate.Run(ctx, migrate.LocalSequence, migrate.LocalMode); err != nil { + return "", fmt.Errorf("running migrations: %w", err) + } + + // Extract schema before closing database + schema, err := extractSchema(db) + if err != nil { + return "", fmt.Errorf("extracting schema: %w", err) + } + + // Add INSERT statements for migration versions. + systemData := "\n-- Migration version data.\n" + systemData += fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s', %d);\n", consts.SystemSchema, len(migrate.LocalSequence)) + systemData += fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s', %d);\n", consts.SystemRemoteSchema, len(migrate.RemoteSequence)) + + return schema + systemData, nil +} + +// extractSchema extracts the complete schema by querying sqlite_master +func extractSchema(db *database.DB) (string, error) { + // Query sqlite_master for all schema objects, excluding FTS shadow tables + // FTS shadow tables are internal tables automatically created by FTS virtual tables + rows, err := db.Conn.Query(`SELECT sql FROM sqlite_master + WHERE sql IS NOT NULL + AND name NOT LIKE 'sqlite_%' + AND (type != 'table' + OR (type = 'table' AND name NOT IN ( + SELECT m1.name FROM sqlite_master m1 + JOIN sqlite_master m2 ON m1.name LIKE m2.name || '_%' + WHERE m2.type = 'table' AND m2.sql LIKE '%VIRTUAL TABLE%' + )))`) + if err != nil { + return "", fmt.Errorf("querying sqlite_master: %w", err) + } + defer rows.Close() + + var schemas []string + for rows.Next() { + var sql string + if err := rows.Scan(&sql); err != nil { + return "", fmt.Errorf("scanning row: %w", err) + } + schemas = append(schemas, sql) + } + + if err := rows.Err(); err != nil { + return "", fmt.Errorf("iterating rows: %w", err) + } + + // Add autogenerated header comment + header := `-- This is the final state of the CLI database after all migrations. +-- Auto-generated by generate-schema.go. Do not edit manually. +` + return header + strings.Join(schemas, ";\n") + ";\n", nil +} diff --git a/pkg/cli/database/schema/main_test.go b/pkg/cli/database/schema/main_test.go new file mode 100644 index 00000000..26898ef3 --- /dev/null +++ b/pkg/cli/database/schema/main_test.go @@ -0,0 +1,81 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/consts" +) + +func TestRun(t *testing.T) { + tmpDir := t.TempDir() + outputPath := filepath.Join(tmpDir, "schema.sql") + + // Run the function + if err := run(tmpDir, outputPath); err != nil { + t.Fatalf("run() failed: %v", err) + } + + // Verify schema.sql was created + content, err := os.ReadFile(outputPath) + if err != nil { + t.Fatalf("reading schema.sql: %v", err) + } + + schema := string(content) + + // Verify it has the header + assert.Equal(t, strings.HasPrefix(schema, "-- This is the final state"), true, "schema.sql should have header comment") + + // Verify schema contains expected tables + expectedTables := []string{ + "CREATE TABLE books", + "CREATE TABLE system", + "CREATE TABLE \"notes\"", + "CREATE VIRTUAL TABLE note_fts", + } + + for _, expected := range expectedTables { + assert.Equal(t, strings.Contains(schema, expected), true, fmt.Sprintf("schema should contain %s", expected)) + } + + // Verify schema contains triggers + expectedTriggers := []string{ + "CREATE TRIGGER notes_after_insert", + "CREATE TRIGGER notes_after_delete", + "CREATE TRIGGER notes_after_update", + } + + for _, expected := range expectedTriggers { + assert.Equal(t, strings.Contains(schema, expected), true, fmt.Sprintf("schema should contain %s", expected)) + } + + // Verify schema does not contain sqlite internal tables + assert.Equal(t, strings.Contains(schema, "sqlite_sequence"), false, "schema should not contain sqlite_sequence") + + // Verify system key-value pairs for schema versions are present + expectedSchemaKey := fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s',", consts.SystemSchema) + assert.Equal(t, strings.Contains(schema, expectedSchemaKey), true, "schema should contain schema version INSERT statement") + + expectedRemoteSchemaKey := fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s',", consts.SystemRemoteSchema) + assert.Equal(t, strings.Contains(schema, expectedRemoteSchemaKey), true, "schema should contain remote_schema version INSERT statement") +} diff --git a/pkg/cli/database/sql.go b/pkg/cli/database/sql.go index ead73078..0af3c7fd 100644 --- a/pkg/cli/database/sql.go +++ b/pkg/cli/database/sql.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package database diff --git a/pkg/cli/database/testutils.go b/pkg/cli/database/testutils.go index 6c17f271..23633d53 100644 --- a/pkg/cli/database/testutils.go +++ b/pkg/cli/database/testutils.go @@ -1,27 +1,24 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package database import ( "database/sql" + _ "embed" "fmt" - "os" "path/filepath" "testing" @@ -30,56 +27,13 @@ import ( "github.com/pkg/errors" ) -var defaultSchemaSQL = `CREATE TABLE books - ( - uuid text PRIMARY KEY, - label text NOT NULL - , dirty bool DEFAULT false, usn int DEFAULT 0 NOT NULL, deleted bool DEFAULT false); -CREATE TABLE system - ( - key string NOT NULL, - value text NOT NULL - ); -CREATE UNIQUE INDEX idx_books_label ON books(label); -CREATE UNIQUE INDEX idx_books_uuid ON books(uuid); -CREATE TABLE IF NOT EXISTS "notes" - ( - uuid text NOT NULL, - book_uuid text NOT NULL, - body text NOT NULL, - added_on integer NOT NULL, - edited_on integer DEFAULT 0, - public bool DEFAULT false, - dirty bool DEFAULT false, - usn int DEFAULT 0 NOT NULL, - deleted bool DEFAULT false - ); -CREATE VIRTUAL TABLE note_fts USING fts5(content=notes, body, tokenize="porter unicode61 categories 'L* N* Co Ps Pe'") -/* note_fts(body) */; -CREATE TABLE IF NOT EXISTS 'note_fts_data'(id INTEGER PRIMARY KEY, block BLOB); -CREATE TABLE IF NOT EXISTS 'note_fts_idx'(segid, term, pgno, PRIMARY KEY(segid, term)) WITHOUT ROWID; -CREATE TABLE IF NOT EXISTS 'note_fts_docsize'(id INTEGER PRIMARY KEY, sz BLOB); -CREATE TABLE IF NOT EXISTS 'note_fts_config'(k PRIMARY KEY, v) WITHOUT ROWID; -CREATE TRIGGER notes_after_insert AFTER INSERT ON notes BEGIN - INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); - END; -CREATE TRIGGER notes_after_delete AFTER DELETE ON notes BEGIN - INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); - END; -CREATE TRIGGER notes_after_update AFTER UPDATE ON notes BEGIN - INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); - INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); - END; -CREATE TABLE actions - ( - uuid text PRIMARY KEY, - schema integer NOT NULL, - type text NOT NULL, - data text NOT NULL, - timestamp integer NOT NULL - ); -CREATE UNIQUE INDEX idx_notes_uuid ON notes(uuid); -CREATE INDEX idx_notes_book_uuid ON notes(book_uuid);` +//go:embed schema.sql +var defaultSchemaSQL string + +// GetDefaultSchemaSQL returns the default schema SQL for tests +func GetDefaultSchemaSQL() string { + return defaultSchemaSQL +} // MustScan scans the given row and fails a test in case of any errors func MustScan(t *testing.T, message string, row *sql.Row, args ...interface{}) { @@ -99,29 +53,48 @@ func MustExec(t *testing.T, message string, db *DB, query string, args ...interf return result } -// TestDBOptions contains options for test database -type TestDBOptions struct { - SchemaSQLPath string - SkipMigration bool +// InitTestMemoryDB initializes an in-memory test database with the default schema. +func InitTestMemoryDB(t *testing.T) *DB { + return InitTestMemoryDBRaw(t, "") } -// InitTestDB initializes a test database and opens connection to it -func InitTestDB(t *testing.T, dbPath string, options *TestDBOptions) *DB { +// InitTestFileDB initializes a file-based test database with the default schema. +func InitTestFileDB(t *testing.T) (*DB, string) { + uuid := mustGenerateTestUUID(t) + dbPath := filepath.Join(t.TempDir(), fmt.Sprintf("dnote-%s.db", uuid)) + db := InitTestFileDBRaw(t, dbPath) + return db, dbPath +} + +// InitTestFileDBRaw initializes a file-based test database at the specified path with the default schema. +func InitTestFileDBRaw(t *testing.T, dbPath string) *DB { db, err := Open(dbPath) if err != nil { - t.Fatal(errors.Wrap(err, "opening database connection")) + t.Fatal(errors.Wrap(err, "opening database")) } - dir, _ := filepath.Split(dbPath) - err = os.MkdirAll(dir, 0777) + if _, err := db.Exec(defaultSchemaSQL); err != nil { + t.Fatal(errors.Wrap(err, "running schema sql")) + } + + t.Cleanup(func() { db.Close() }) + return db +} + +// InitTestMemoryDBRaw initializes an in-memory test database without marking migrations complete. +// If schemaPath is empty, uses the default schema. Used for migration testing. +func InitTestMemoryDBRaw(t *testing.T, schemaPath string) *DB { + uuid := mustGenerateTestUUID(t) + dbName := fmt.Sprintf("file:%s?mode=memory&cache=shared", uuid) + + db, err := Open(dbName) if err != nil { - t.Fatal(errors.Wrap(err, "creating the directory for test database file")) + t.Fatal(errors.Wrap(err, "opening in-memory database")) } var schemaSQL string - if options != nil && options.SchemaSQLPath != "" { - b := utils.ReadFileAbs(options.SchemaSQLPath) - schemaSQL = string(b) + if schemaPath != "" { + schemaSQL = string(utils.ReadFileAbs(schemaPath)) } else { schemaSQL = defaultSchemaSQL } @@ -130,24 +103,10 @@ func InitTestDB(t *testing.T, dbPath string, options *TestDBOptions) *DB { t.Fatal(errors.Wrap(err, "running schema sql")) } - if options == nil || !options.SkipMigration { - MarkMigrationComplete(t, db) - } - + t.Cleanup(func() { db.Close() }) return db } -// TeardownTestDB closes the test database and removes the its file -func TeardownTestDB(t *testing.T, db *DB) { - if err := db.Close(); err != nil { - t.Fatal(errors.Wrap(err, "closing database")) - } - - if err := os.RemoveAll(db.Filepath); err != nil { - t.Fatal(errors.Wrap(err, "removing database file")) - } -} - // OpenTestDB opens the database connection to a test database // without initializing any schema func OpenTestDB(t *testing.T, dnoteDir string) *DB { @@ -160,12 +119,11 @@ func OpenTestDB(t *testing.T, dnoteDir string) *DB { return db } -// MarkMigrationComplete marks all migrations as complete in the database -func MarkMigrationComplete(t *testing.T, db *DB) { - if _, err := db.Exec("INSERT INTO system (key, value) VALUES (? , ?);", consts.SystemSchema, 12); err != nil { - t.Fatal(errors.Wrap(err, "inserting schema")) - } - if _, err := db.Exec("INSERT INTO system (key, value) VALUES (? , ?);", consts.SystemRemoteSchema, 1); err != nil { - t.Fatal(errors.Wrap(err, "inserting remote schema")) +// mustGenerateTestUUID generates a UUID for test databases and fails the test on error +func mustGenerateTestUUID(t *testing.T) string { + uuid, err := utils.GenerateUUID() + if err != nil { + t.Fatal(errors.Wrap(err, "generating UUID for test database")) } + return uuid } diff --git a/pkg/cli/dirs/dirs_test.go b/pkg/cli/dirs/dirs_test.go deleted file mode 100644 index 992af44a..00000000 --- a/pkg/cli/dirs/dirs_test.go +++ /dev/null @@ -1,43 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . - */ - -package dirs - -import ( - "os" - "testing" - - "github.com/dnote/dnote/pkg/assert" -) - -type envTestCase struct { - envKey string - envVal string - got *string - expected string -} - -func testCustomDirs(t *testing.T, testCases []envTestCase) { - for _, tc := range testCases { - os.Setenv(tc.envKey, tc.envVal) - - Reload() - - assert.Equal(t, *tc.got, tc.expected, "result mismatch") - } -} diff --git a/pkg/cli/dirs/dirs_unix.go b/pkg/cli/dirs/dirs_unix.go deleted file mode 100644 index fe9908e2..00000000 --- a/pkg/cli/dirs/dirs_unix.go +++ /dev/null @@ -1,52 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . - */ - -//go:build linux || darwin - - -package dirs - -import ( - "path/filepath" -) - -// The environment variable names for the XDG base directory specification -var ( - envConfigHome = "XDG_CONFIG_HOME" - envDataHome = "XDG_DATA_HOME" - envCacheHome = "XDG_CACHE_HOME" -) - -func initDirs() { - Home = getHomeDir() - ConfigHome = readPath(envConfigHome, getConfigHome(Home)) - DataHome = readPath(envDataHome, getDataHome(Home)) - CacheHome = readPath(envCacheHome, getCacheHome(Home)) -} - -func getConfigHome(homeDir string) string { - return filepath.Join(homeDir, ".config") -} - -func getDataHome(homeDir string) string { - return filepath.Join(homeDir, ".local/share") -} - -func getCacheHome(homeDir string) string { - return filepath.Join(homeDir, ".cache") -} diff --git a/pkg/cli/dirs/dirs_windows.go b/pkg/cli/dirs/dirs_windows.go deleted file mode 100644 index a70014e3..00000000 --- a/pkg/cli/dirs/dirs_windows.go +++ /dev/null @@ -1,33 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . - */ - -//go:build windows - - -package dirs - -import ( - "path/filepath" -) - -func initDirs() { - Home = getHomeDir() - ConfigHome = filepath.Join(Home, ".dnote") - DataHome = filepath.Join(Home, ".dnote") - CacheHome = filepath.Join(Home, ".dnote") -} diff --git a/pkg/cli/dirs/dirs_windows_test.go b/pkg/cli/dirs/dirs_windows_test.go deleted file mode 100644 index bdaa217d..00000000 --- a/pkg/cli/dirs/dirs_windows_test.go +++ /dev/null @@ -1,60 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . - */ - -//go:build windows - - -package dirs - -import ( - "path/filepath" - "testing" - - "github.com/dnote/dnote/pkg/assert" -) - -func TestDirs(t *testing.T) { - home := Home - assert.NotEqual(t, home, "", "home is empty") - - configHome := filepath.Join(home, ".dnote") - dataHome := filepath.Join(home, ".dnote") - cacheHome := filepath.Join(home, ".dnote") - - testCases := []struct { - got string - expected string - }{ - { - got: ConfigHome, - expected: configHome, - }, - { - got: DataHome, - expected: dataHome, - }, - { - got: CacheHome, - expected: cacheHome, - }, - } - - for _, tc := range testCases { - assert.Equal(t, tc.got, tc.expected, "result mismatch") - } -} diff --git a/pkg/cli/infra/init.go b/pkg/cli/infra/init.go index fc767e6b..a6cbd1aa 100644 --- a/pkg/cli/infra/init.go +++ b/pkg/cli/infra/init.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package infra provides operations and definitions for the @@ -24,23 +21,28 @@ import ( "database/sql" "fmt" "os" - "path/filepath" "strconv" "time" + "github.com/dnote/dnote/pkg/cli/client" "github.com/dnote/dnote/pkg/cli/config" "github.com/dnote/dnote/pkg/cli/consts" "github.com/dnote/dnote/pkg/cli/context" "github.com/dnote/dnote/pkg/cli/database" - "github.com/dnote/dnote/pkg/cli/dirs" "github.com/dnote/dnote/pkg/cli/log" "github.com/dnote/dnote/pkg/cli/migrate" "github.com/dnote/dnote/pkg/cli/utils" "github.com/dnote/dnote/pkg/clock" + "github.com/dnote/dnote/pkg/dirs" "github.com/pkg/errors" "github.com/spf13/cobra" ) +const ( + // DefaultAPIEndpoint is the default API endpoint used when none is configured + DefaultAPIEndpoint = "http://localhost:3001/api" +) + // RunEFunc is a function type of dnote commands type RunEFunc func(*cobra.Command, []string) error @@ -52,13 +54,18 @@ func checkLegacyDBPath() (string, bool) { } if err != nil { - log.Errorf(errors.Wrapf(err, "checking legacy dnote directory at %s", legacyDnoteDir).Error()) + log.Error(errors.Wrapf(err, "checking legacy dnote directory at %s", legacyDnoteDir).Error()) } return "", false } -func getDBPath(paths context.Paths) string { +func getDBPath(paths context.Paths, customPath string) string { + // If custom path is provided, use it + if customPath != "" { + return customPath + } + legacyDnoteDir, ok := checkLegacyDBPath() if ok { return fmt.Sprintf("%s/%s", legacyDnoteDir, consts.DnoteDBFileName) @@ -67,7 +74,10 @@ func getDBPath(paths context.Paths) string { return fmt.Sprintf("%s/%s/%s", paths.Data, consts.DnoteDirName, consts.DnoteDBFileName) } -func newCtx(versionTag string) (context.DnoteCtx, error) { +// newBaseCtx creates a minimal context with paths and database connection. +// This base context is used for file and database initialization before +// being enriched with config values by setupCtx. +func newBaseCtx(versionTag, customDBPath string) (context.DnoteCtx, error) { dnoteDir := getLegacyDnotePath(dirs.Home) paths := context.Paths{ Home: dirs.Home, @@ -77,7 +87,7 @@ func newCtx(versionTag string) (context.DnoteCtx, error) { LegacyDnote: dnoteDir, } - dbPath := getDBPath(paths) + dbPath := getDBPath(paths, customDBPath) db, err := database.Open(dbPath) if err != nil { @@ -94,13 +104,14 @@ func newCtx(versionTag string) (context.DnoteCtx, error) { } // Init initializes the Dnote environment and returns a new dnote context -func Init(apiEndpoint, versionTag string) (*context.DnoteCtx, error) { - ctx, err := newCtx(versionTag) +// apiEndpoint is used when creating a new config file (e.g., from ldflags during tests) +func Init(versionTag, apiEndpoint, dbPath string) (*context.DnoteCtx, error) { + ctx, err := newBaseCtx(versionTag, dbPath) if err != nil { return nil, errors.Wrap(err, "initializing a context") } - if err := InitFiles(ctx, apiEndpoint); err != nil { + if err := initFiles(ctx, apiEndpoint); err != nil { return nil, errors.Wrap(err, "initializing files") } @@ -118,18 +129,19 @@ func Init(apiEndpoint, versionTag string) (*context.DnoteCtx, error) { return nil, errors.Wrap(err, "running migration") } - ctx, err = SetupCtx(ctx) + ctx, err = setupCtx(ctx) if err != nil { return nil, errors.Wrap(err, "setting up the context") } - log.Debug("Running with Dnote context: %+v\n", context.Redact(ctx)) + log.Debug("context: %+v\n", context.Redact(ctx)) return &ctx, nil } -// SetupCtx populates the context and returns a new context -func SetupCtx(ctx context.DnoteCtx) (context.DnoteCtx, error) { +// setupCtx enriches the base context with values from config file and database. +// This is called after files and database have been initialized. +func setupCtx(ctx context.DnoteCtx) (context.DnoteCtx, error) { db := ctx.DB var sessionKey string @@ -150,14 +162,16 @@ func SetupCtx(ctx context.DnoteCtx) (context.DnoteCtx, error) { } ret := context.DnoteCtx{ - Paths: ctx.Paths, - Version: ctx.Version, - DB: ctx.DB, - SessionKey: sessionKey, - SessionKeyExpiry: sessionKeyExpiry, - APIEndpoint: cf.APIEndpoint, - Editor: cf.Editor, - Clock: clock.New(), + Paths: ctx.Paths, + Version: ctx.Version, + DB: ctx.DB, + SessionKey: sessionKey, + SessionKeyExpiry: sessionKeyExpiry, + APIEndpoint: cf.APIEndpoint, + Editor: cf.Editor, + Clock: clock.New(), + EnableUpgradeCheck: cf.EnableUpgradeCheck, + HTTPClient: client.NewRateLimitedHTTPClient(), } return ret, nil @@ -273,7 +287,9 @@ func InitSystem(ctx context.DnoteCtx) error { return errors.Wrapf(err, "initializing system config for %s", consts.SystemLastSyncAt) } - tx.Commit() + if err := tx.Commit(); err != nil { + return errors.Wrap(err, "committing transaction") + } return nil } @@ -309,36 +325,6 @@ func getEditorCommand() string { return ret } -func initDir(path string) error { - ok, err := utils.FileExists(path) - if err != nil { - return errors.Wrapf(err, "checking if dir exists at %s", path) - } - if ok { - return nil - } - - if err := os.MkdirAll(path, 0755); err != nil { - return errors.Wrapf(err, "creating a directory at %s", path) - } - - return nil -} - -// initDnoteDir initializes missing directories that Dnote uses -func initDnoteDir(ctx context.DnoteCtx) error { - if err := initDir(filepath.Join(ctx.Paths.Config, consts.DnoteDirName)); err != nil { - return errors.Wrap(err, "initializing config dir") - } - if err := initDir(filepath.Join(ctx.Paths.Data, consts.DnoteDirName)); err != nil { - return errors.Wrap(err, "initializing data dir") - } - if err := initDir(filepath.Join(ctx.Paths.Cache, consts.DnoteDirName)); err != nil { - return errors.Wrap(err, "initializing cache dir") - } - - return nil -} // initConfigFile populates a new config file if it does not exist yet func initConfigFile(ctx context.DnoteCtx, apiEndpoint string) error { @@ -353,9 +339,16 @@ func initConfigFile(ctx context.DnoteCtx, apiEndpoint string) error { editor := getEditorCommand() + // Use default API endpoint if none provided + endpoint := apiEndpoint + if endpoint == "" { + endpoint = DefaultAPIEndpoint + } + cf := config.Config{ - Editor: editor, - APIEndpoint: apiEndpoint, + Editor: editor, + APIEndpoint: endpoint, + EnableUpgradeCheck: true, } if err := config.Write(ctx, cf); err != nil { @@ -365,9 +358,9 @@ func initConfigFile(ctx context.DnoteCtx, apiEndpoint string) error { return nil } -// InitFiles creates, if necessary, the dnote directory and files inside -func InitFiles(ctx context.DnoteCtx, apiEndpoint string) error { - if err := initDnoteDir(ctx); err != nil { +// initFiles creates, if necessary, the dnote directory and files inside +func initFiles(ctx context.DnoteCtx, apiEndpoint string) error { + if err := context.InitDnoteDirs(ctx.Paths); err != nil { return errors.Wrap(err, "creating the dnote dir") } if err := initConfigFile(ctx, apiEndpoint); err != nil { diff --git a/pkg/cli/infra/init_test.go b/pkg/cli/infra/init_test.go index 6b548da4..8c698624 100644 --- a/pkg/cli/infra/init_test.go +++ b/pkg/cli/infra/init_test.go @@ -1,35 +1,35 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package infra import ( + "fmt" + "os" "testing" "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/config" "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/dirs" "github.com/pkg/errors" ) func TestInitSystemKV(t *testing.T) { // Setup - db := database.InitTestDB(t, "../tmp/dnote-test.db", nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) var originalCount int database.MustScan(t, "counting system configs", db.QueryRow("SELECT count(*) FROM system"), &originalCount) @@ -60,8 +60,7 @@ func TestInitSystemKV(t *testing.T) { func TestInitSystemKV_existing(t *testing.T) { // Setup - db := database.InitTestDB(t, "../tmp/dnote-test.db", nil) - defer database.TeardownTestDB(t, db) + db := database.InitTestMemoryDB(t) database.MustExec(t, "inserting a system config", db, "INSERT INTO system (key, value) VALUES (?, ?)", "testKey", "testVal") @@ -91,3 +90,37 @@ func TestInitSystemKV_existing(t *testing.T) { db.QueryRow("SELECT value FROM system WHERE key = ?", "testKey"), &val) assert.Equal(t, val, "testVal", "system value should not have been updated") } + +func TestInit_APIEndpoint(t *testing.T) { + // Create a temporary directory for test + tmpDir, err := os.MkdirTemp("", "dnote-init-test-*") + if err != nil { + t.Fatal(errors.Wrap(err, "creating temp dir")) + } + defer os.RemoveAll(tmpDir) + + // Set up environment to use our temp directory + t.Setenv("XDG_CONFIG_HOME", fmt.Sprintf("%s/config", tmpDir)) + t.Setenv("XDG_DATA_HOME", fmt.Sprintf("%s/data", tmpDir)) + t.Setenv("XDG_CACHE_HOME", fmt.Sprintf("%s/cache", tmpDir)) + + // Force dirs package to reload with new environment + dirs.Reload() + + // Initialize - should create config with default apiEndpoint + ctx, err := Init("test-version", "", "") + if err != nil { + t.Fatal(errors.Wrap(err, "initializing")) + } + defer ctx.DB.Close() + + // Read the config that was created + cf, err := config.Read(*ctx) + if err != nil { + t.Fatal(errors.Wrap(err, "reading config")) + } + + // Context should use the apiEndpoint from config + assert.Equal(t, ctx.APIEndpoint, DefaultAPIEndpoint, "context should use apiEndpoint from config") + assert.Equal(t, cf.APIEndpoint, DefaultAPIEndpoint, "context should use apiEndpoint from config") +} diff --git a/pkg/cli/log/log.go b/pkg/cli/log/log.go index d9313b43..0bc569b6 100644 --- a/pkg/cli/log/log.go +++ b/pkg/cli/log/log.go @@ -1,27 +1,30 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package log import ( "fmt" - "github.com/dnote/color" "os" + + "github.com/fatih/color" +) + +const ( + debugEnvName = "DNOTE_DEBUG" + debugEnvValue = "1" ) var ( @@ -81,7 +84,7 @@ func Error(msg string) { // Errorf prints an error message with optional format verbs func Errorf(msg string, v ...interface{}) { - fmt.Fprintf(color.Output, "%s%s %s", indent, ColorRed.Sprintf("⨯"), fmt.Sprintf(msg, v...)) + fmt.Fprintf(color.Output, "%s%s %s", indent, ColorRed.Sprintf("%s", "⨯"), fmt.Sprintf(msg, v...)) } // Printf prints an normal message @@ -96,17 +99,29 @@ func Askf(msg string, masked bool, v ...interface{}) { var symbol string if masked { - symbol = ColorGray.Sprintf(symbolChar) + symbol = ColorGray.Sprintf("%s", symbolChar) } else { - symbol = ColorGreen.Sprintf(symbolChar) + symbol = ColorGreen.Sprintf("%s", symbolChar) } fmt.Fprintf(color.Output, "%s%s %s: ", indent, symbol, fmt.Sprintf(msg, v...)) } +// isDebug returns true if debug mode is enabled +func isDebug() bool { + return os.Getenv(debugEnvName) == debugEnvValue +} + // Debug prints to the console if DNOTE_DEBUG is set func Debug(msg string, v ...interface{}) { - if os.Getenv("DNOTE_DEBUG") == "1" { + if isDebug() { fmt.Fprintf(color.Output, "%s %s", ColorGray.Sprint("DEBUG:"), fmt.Sprintf(msg, v...)) } } + +// DebugNewline prints a newline only in debug mode +func DebugNewline() { + if isDebug() { + fmt.Println() + } +} diff --git a/pkg/cli/main.go b/pkg/cli/main.go index a10d78e2..2fb1c564 100644 --- a/pkg/cli/main.go +++ b/pkg/cli/main.go @@ -1,25 +1,23 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package main import ( "os" + "strings" "github.com/dnote/dnote/pkg/cli/infra" "github.com/dnote/dnote/pkg/cli/log" @@ -28,12 +26,10 @@ import ( // commands "github.com/dnote/dnote/pkg/cli/cmd/add" - "github.com/dnote/dnote/pkg/cli/cmd/cat" "github.com/dnote/dnote/pkg/cli/cmd/edit" "github.com/dnote/dnote/pkg/cli/cmd/find" "github.com/dnote/dnote/pkg/cli/cmd/login" "github.com/dnote/dnote/pkg/cli/cmd/logout" - "github.com/dnote/dnote/pkg/cli/cmd/ls" "github.com/dnote/dnote/pkg/cli/cmd/remove" "github.com/dnote/dnote/pkg/cli/cmd/root" "github.com/dnote/dnote/pkg/cli/cmd/sync" @@ -45,8 +41,32 @@ import ( var apiEndpoint string var versionTag = "master" +// parseDBPath extracts --dbPath flag value from command line arguments +// regardless of where it appears (before or after subcommand). +// Returns empty string if not found. +func parseDBPath(args []string) string { + for i, arg := range args { + // Handle --dbPath=value + if strings.HasPrefix(arg, "--dbPath=") { + return strings.TrimPrefix(arg, "--dbPath=") + } + // Handle --dbPath value + if arg == "--dbPath" && i+1 < len(args) { + return args[i+1] + } + } + return "" +} + func main() { - ctx, err := infra.Init(apiEndpoint, versionTag) + // Parse flags early to get --dbPath before initializing database + // We need to manually parse --dbPath because it can appear after the subcommand + // (e.g., "dnote sync --full --dbPath=./custom.db") and root.ParseFlags only + // parses flags before the subcommand. + dbPath := parseDBPath(os.Args[1:]) + + // Initialize context - defaultAPIEndpoint is used when creating new config file + ctx, err := infra.Init(versionTag, apiEndpoint, dbPath) if err != nil { panic(errors.Wrap(err, "initializing context")) } @@ -57,10 +77,8 @@ func main() { root.Register(login.NewCmd(*ctx)) root.Register(logout.NewCmd(*ctx)) root.Register(add.NewCmd(*ctx)) - root.Register(ls.NewCmd(*ctx)) root.Register(sync.NewCmd(*ctx)) root.Register(version.NewCmd(*ctx)) - root.Register(cat.NewCmd(*ctx)) root.Register(view.NewCmd(*ctx)) root.Register(find.NewCmd(*ctx)) diff --git a/pkg/cli/main_test.go b/pkg/cli/main_test.go index 4f68e9b3..727a5c3e 100644 --- a/pkg/cli/main_test.go +++ b/pkg/cli/main_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package main @@ -23,6 +20,7 @@ import ( "log" "os" "os/exec" + "strings" "testing" "github.com/dnote/dnote/pkg/assert" @@ -35,14 +33,17 @@ import ( var binaryName = "test-dnote" -var testDir = "./tmp/.dnote" - -var opts = testutils.RunDnoteCmdOptions{ - Env: []string{ - fmt.Sprintf("XDG_CONFIG_HOME=%s", testDir), - fmt.Sprintf("XDG_DATA_HOME=%s", testDir), - fmt.Sprintf("XDG_CACHE_HOME=%s", testDir), - }, +// setupTestEnv creates a unique test directory for parallel test execution +func setupTestEnv(t *testing.T) (string, testutils.RunDnoteCmdOptions) { + testDir := t.TempDir() + opts := testutils.RunDnoteCmdOptions{ + Env: []string{ + fmt.Sprintf("XDG_CONFIG_HOME=%s", testDir), + fmt.Sprintf("XDG_DATA_HOME=%s", testDir), + fmt.Sprintf("XDG_CACHE_HOME=%s", testDir), + }, + } + return testDir, opts } func TestMain(m *testing.M) { @@ -55,10 +56,11 @@ func TestMain(m *testing.M) { } func TestInit(t *testing.T) { + testDir, opts := setupTestEnv(t) + // Execute // run an arbitrary command "view" due to https://github.com/spf13/cobra/issues/1056 testutils.RunDnoteCmd(t, opts, binaryName, "view") - defer testutils.RemoveDir(t, testDir) db := database.OpenTestDB(t, testDir) @@ -107,11 +109,11 @@ func TestInit(t *testing.T) { func TestAddNote(t *testing.T) { t.Run("new book", func(t *testing.T) { + testDir, opts := setupTestEnv(t) + // Set up and execute testutils.RunDnoteCmd(t, opts, binaryName, "add", "js", "-c", "foo") - testutils.WaitDnoteCmd(t, opts, testutils.UserContent, binaryName, "add", "js") - - defer testutils.RemoveDir(t, testDir) + testutils.MustWaitDnoteCmd(t, opts, testutils.UserContent, binaryName, "add", "js") db := database.OpenTestDB(t, testDir) @@ -138,13 +140,14 @@ func TestAddNote(t *testing.T) { }) t.Run("existing book", func(t *testing.T) { + _, opts := setupTestEnv(t) + // Setup - db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil) + db, dbPath := database.InitTestFileDB(t) testutils.Setup3(t, db) // Execute - testutils.RunDnoteCmd(t, opts, binaryName, "add", "js", "-c", "foo") - defer testutils.RemoveDir(t, testDir) + testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "add", "js", "-c", "foo") // Test @@ -179,13 +182,14 @@ func TestAddNote(t *testing.T) { func TestEditNote(t *testing.T) { t.Run("content flag", func(t *testing.T) { + _, opts := setupTestEnv(t) + // Setup - db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil) + db, dbPath := database.InitTestFileDB(t) testutils.Setup4(t, db) // Execute - testutils.RunDnoteCmd(t, opts, binaryName, "edit", "2", "-c", "foo bar") - defer testutils.RemoveDir(t, testDir) + testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "edit", "2", "-c", "foo bar") // Test var noteCount, bookCount int @@ -212,13 +216,14 @@ func TestEditNote(t *testing.T) { }) t.Run("book flag", func(t *testing.T) { + _, opts := setupTestEnv(t) + // Setup - db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil) + db, dbPath := database.InitTestFileDB(t) testutils.Setup5(t, db) // Execute - testutils.RunDnoteCmd(t, opts, binaryName, "edit", "2", "-b", "linux") - defer testutils.RemoveDir(t, testDir) + testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "edit", "2", "-b", "linux") // Test var noteCount, bookCount int @@ -246,13 +251,14 @@ func TestEditNote(t *testing.T) { }) t.Run("book flag and content flag", func(t *testing.T) { + _, opts := setupTestEnv(t) + // Setup - db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil) + db, dbPath := database.InitTestFileDB(t) testutils.Setup5(t, db) // Execute - testutils.RunDnoteCmd(t, opts, binaryName, "edit", "2", "-b", "linux", "-c", "n2 body updated") - defer testutils.RemoveDir(t, testDir) + testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "edit", "2", "-b", "linux", "-c", "n2 body updated") // Test var noteCount, bookCount int @@ -282,13 +288,14 @@ func TestEditNote(t *testing.T) { func TestEditBook(t *testing.T) { t.Run("name flag", func(t *testing.T) { + _, opts := setupTestEnv(t) + // Setup - db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil) + db, dbPath := database.InitTestFileDB(t) testutils.Setup1(t, db) // Execute - testutils.RunDnoteCmd(t, opts, binaryName, "edit", "js", "-n", "js-edited") - defer testutils.RemoveDir(t, testDir) + testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "edit", "js", "-n", "js-edited") // Test var noteCount, bookCount int @@ -341,17 +348,18 @@ func TestRemoveNote(t *testing.T) { for _, tc := range testCases { t.Run(fmt.Sprintf("--yes=%t", tc.yesFlag), func(t *testing.T) { + _, opts := setupTestEnv(t) + // Setup - db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil) + db, dbPath := database.InitTestFileDB(t) testutils.Setup2(t, db) // Execute if tc.yesFlag { - testutils.RunDnoteCmd(t, opts, binaryName, "remove", "-y", "1") + testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "remove", "-y", "1") } else { - testutils.WaitDnoteCmd(t, opts, testutils.UserConfirm, binaryName, "remove", "1") + testutils.MustWaitDnoteCmd(t, opts, testutils.ConfirmRemoveNote, binaryName, "--dbPath", dbPath, "remove", "1") } - defer testutils.RemoveDir(t, testDir) // Test var noteCount, bookCount, jsNoteCount, linuxNoteCount int @@ -428,19 +436,19 @@ func TestRemoveBook(t *testing.T) { for _, tc := range testCases { t.Run(fmt.Sprintf("--yes=%t", tc.yesFlag), func(t *testing.T) { + _, opts := setupTestEnv(t) + // Setup - db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil) + db, dbPath := database.InitTestFileDB(t) testutils.Setup2(t, db) // Execute if tc.yesFlag { - testutils.RunDnoteCmd(t, opts, binaryName, "remove", "-y", "js") + testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "remove", "-y", "js") } else { - testutils.WaitDnoteCmd(t, opts, testutils.UserConfirm, binaryName, "remove", "js") + testutils.MustWaitDnoteCmd(t, opts, testutils.ConfirmRemoveBook, binaryName, "--dbPath", dbPath, "remove", "js") } - defer testutils.RemoveDir(t, testDir) - // Test var noteCount, bookCount, jsNoteCount, linuxNoteCount int database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) @@ -501,3 +509,125 @@ func TestRemoveBook(t *testing.T) { }) } } + +func TestDBPathFlag(t *testing.T) { + // Helper function to verify database contents + verifyDatabase := func(t *testing.T, dbPath, expectedBook, expectedNote string) *database.DB { + ok, err := utils.FileExists(dbPath) + if err != nil { + t.Fatal(errors.Wrapf(err, "checking if custom db exists at %s", dbPath)) + } + if !ok { + t.Errorf("custom database was not created at %s", dbPath) + } + + db, err := database.Open(dbPath) + if err != nil { + t.Fatal(errors.Wrapf(err, "opening db at %s", dbPath)) + } + + var noteCount, bookCount int + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + + assert.Equalf(t, bookCount, 1, fmt.Sprintf("%s book count mismatch", dbPath)) + assert.Equalf(t, noteCount, 1, fmt.Sprintf("%s note count mismatch", dbPath)) + + var book database.Book + database.MustScan(t, "getting book", db.QueryRow("SELECT label FROM books"), &book.Label) + assert.Equalf(t, book.Label, expectedBook, fmt.Sprintf("%s book label mismatch", dbPath)) + + var note database.Note + database.MustScan(t, "getting note", db.QueryRow("SELECT body FROM notes"), ¬e.Body) + assert.Equalf(t, note.Body, expectedNote, fmt.Sprintf("%s note body mismatch", dbPath)) + + return db + } + + // Setup - use two different custom database paths + testDir, customOpts := setupTestEnv(t) + customDBPath1 := fmt.Sprintf("%s/custom-test1.db", testDir) + customDBPath2 := fmt.Sprintf("%s/custom-test2.db", testDir) + + // Execute - add different notes to each database + testutils.RunDnoteCmd(t, customOpts, binaryName, "--dbPath", customDBPath1, "add", "db1-book", "-c", "content in db1") + testutils.RunDnoteCmd(t, customOpts, binaryName, "--dbPath", customDBPath2, "add", "db2-book", "-c", "content in db2") + + // Test both databases + db1 := verifyDatabase(t, customDBPath1, "db1-book", "content in db1") + defer db1.Close() + + db2 := verifyDatabase(t, customDBPath2, "db2-book", "content in db2") + defer db2.Close() + + // Verify that the databases are independent + var db1HasDB2Book int + db1.QueryRow("SELECT count(*) FROM books WHERE label = ?", "db2-book").Scan(&db1HasDB2Book) + assert.Equal(t, db1HasDB2Book, 0, "db1 should not have db2's book") + + var db2HasDB1Book int + db2.QueryRow("SELECT count(*) FROM books WHERE label = ?", "db1-book").Scan(&db2HasDB1Book) + assert.Equal(t, db2HasDB1Book, 0, "db2 should not have db1's book") +} + +func TestView(t *testing.T) { + t.Run("view note by rowid", func(t *testing.T) { + _, opts := setupTestEnv(t) + + db, dbPath := database.InitTestFileDB(t) + testutils.Setup4(t, db) + + output := testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "view", "1") + + assert.Equal(t, strings.Contains(output, "Booleans have toString()"), true, "should contain note content") + assert.Equal(t, strings.Contains(output, "book name"), true, "should show metadata") + }) + + t.Run("view note content only", func(t *testing.T) { + _, opts := setupTestEnv(t) + + db, dbPath := database.InitTestFileDB(t) + testutils.Setup4(t, db) + + output := testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "view", "1", "--content-only") + + assert.Equal(t, strings.Contains(output, "Booleans have toString()"), true, "should contain note content") + assert.Equal(t, strings.Contains(output, "book name"), false, "should not show metadata") + }) + + t.Run("list books", func(t *testing.T) { + _, opts := setupTestEnv(t) + + db, dbPath := database.InitTestFileDB(t) + testutils.Setup1(t, db) + + output := testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "view") + + assert.Equal(t, strings.Contains(output, "js"), true, "should list js book") + assert.Equal(t, strings.Contains(output, "linux"), true, "should list linux book") + }) + + t.Run("list notes in book", func(t *testing.T) { + _, opts := setupTestEnv(t) + + db, dbPath := database.InitTestFileDB(t) + testutils.Setup2(t, db) + + output := testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "view", "js") + + assert.Equal(t, strings.Contains(output, "n1 body"), true, "should list note 1") + assert.Equal(t, strings.Contains(output, "n2 body"), true, "should list note 2") + }) + + t.Run("view note by book name and rowid", func(t *testing.T) { + _, opts := setupTestEnv(t) + + db, dbPath := database.InitTestFileDB(t) + testutils.Setup4(t, db) + + output := testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "view", "js", "2") + + assert.Equal(t, strings.Contains(output, "Date object implements mathematical comparisons"), true, "should contain note content") + assert.Equal(t, strings.Contains(output, "book name"), true, "should show metadata") + }) +} diff --git a/pkg/cli/migrate/fixtures/local-12-pre-schema.sql b/pkg/cli/migrate/fixtures/local-12-pre-schema.sql index c501a460..5560af3d 100644 --- a/pkg/cli/migrate/fixtures/local-12-pre-schema.sql +++ b/pkg/cli/migrate/fixtures/local-12-pre-schema.sql @@ -38,13 +38,5 @@ CREATE TRIGGER notes_after_update AFTER UPDATE ON notes BEGIN INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); END; -CREATE TABLE actions - ( - uuid text PRIMARY KEY, - schema integer NOT NULL, - type text NOT NULL, - data text NOT NULL, - timestamp integer NOT NULL - ); CREATE UNIQUE INDEX idx_notes_uuid ON notes(uuid); CREATE INDEX idx_notes_book_uuid ON notes(book_uuid); diff --git a/pkg/cli/migrate/fixtures/local-14-pre-schema.sql b/pkg/cli/migrate/fixtures/local-14-pre-schema.sql new file mode 100644 index 00000000..5560af3d --- /dev/null +++ b/pkg/cli/migrate/fixtures/local-14-pre-schema.sql @@ -0,0 +1,42 @@ +CREATE TABLE books + ( + uuid text PRIMARY KEY, + label text NOT NULL + , dirty bool DEFAULT false, usn int DEFAULT 0 NOT NULL, deleted bool DEFAULT false); +CREATE TABLE system + ( + key string NOT NULL, + value text NOT NULL + ); +CREATE UNIQUE INDEX idx_books_label ON books(label); +CREATE UNIQUE INDEX idx_books_uuid ON books(uuid); +CREATE TABLE IF NOT EXISTS "notes" + ( + uuid text NOT NULL, + book_uuid text NOT NULL, + body text NOT NULL, + added_on integer NOT NULL, + edited_on integer DEFAULT 0, + public bool DEFAULT false, + dirty bool DEFAULT false, + usn int DEFAULT 0 NOT NULL, + deleted bool DEFAULT false + ); +CREATE VIRTUAL TABLE note_fts USING fts5(content=notes, body, tokenize="porter unicode61 categories 'L* N* Co Ps Pe'") +/* note_fts(body) */; +CREATE TABLE IF NOT EXISTS 'note_fts_data'(id INTEGER PRIMARY KEY, block BLOB); +CREATE TABLE IF NOT EXISTS 'note_fts_idx'(segid, term, pgno, PRIMARY KEY(segid, term)) WITHOUT ROWID; +CREATE TABLE IF NOT EXISTS 'note_fts_docsize'(id INTEGER PRIMARY KEY, sz BLOB); +CREATE TABLE IF NOT EXISTS 'note_fts_config'(k PRIMARY KEY, v) WITHOUT ROWID; +CREATE TRIGGER notes_after_insert AFTER INSERT ON notes BEGIN + INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); + END; +CREATE TRIGGER notes_after_delete AFTER DELETE ON notes BEGIN + INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); + END; +CREATE TRIGGER notes_after_update AFTER UPDATE ON notes BEGIN + INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); + INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); + END; +CREATE UNIQUE INDEX idx_notes_uuid ON notes(uuid); +CREATE INDEX idx_notes_book_uuid ON notes(book_uuid); diff --git a/pkg/cli/migrate/legacy.go b/pkg/cli/migrate/legacy.go index 9ee01c68..4399f7fb 100644 --- a/pkg/cli/migrate/legacy.go +++ b/pkg/cli/migrate/legacy.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package migrate provides migration logic for both sqlite and @@ -23,7 +20,6 @@ package migrate import ( "encoding/json" "fmt" - "io/ioutil" "os" "time" @@ -232,7 +228,7 @@ func readSchema(ctx context.DnoteCtx) (schema, error) { path := getSchemaPath(ctx) - b, err := ioutil.ReadFile(path) + b, err := os.ReadFile(path) if err != nil { return ret, errors.Wrap(err, "Failed to read schema file") } @@ -252,7 +248,7 @@ func writeSchema(ctx context.DnoteCtx, s schema) error { return errors.Wrap(err, "Failed to marshal schema into yaml") } - if err := ioutil.WriteFile(path, d, 0644); err != nil { + if err := os.WriteFile(path, d, 0644); err != nil { return errors.Wrap(err, "Failed to write schema file") } @@ -504,7 +500,7 @@ func migrateToV1(ctx context.DnoteCtx) error { func migrateToV2(ctx context.DnoteCtx) error { notePath := fmt.Sprintf("%s/dnote", ctx.Paths.LegacyDnote) - b, err := ioutil.ReadFile(notePath) + b, err := os.ReadFile(notePath) if err != nil { return errors.Wrap(err, "Failed to read the note file") } @@ -548,7 +544,7 @@ func migrateToV2(ctx context.DnoteCtx) error { return errors.Wrap(err, "Failed to marshal new dnote into JSON") } - err = ioutil.WriteFile(notePath, d, 0644) + err = os.WriteFile(notePath, d, 0644) if err != nil { return errors.Wrap(err, "Failed to write the new dnote into the file") } @@ -561,7 +557,7 @@ func migrateToV3(ctx context.DnoteCtx) error { notePath := fmt.Sprintf("%s/dnote", ctx.Paths.LegacyDnote) actionsPath := fmt.Sprintf("%s/actions", ctx.Paths.LegacyDnote) - b, err := ioutil.ReadFile(notePath) + b, err := os.ReadFile(notePath) if err != nil { return errors.Wrap(err, "Failed to read the note file") } @@ -615,7 +611,7 @@ func migrateToV3(ctx context.DnoteCtx) error { return errors.Wrap(err, "Failed to marshal actions into JSON") } - err = ioutil.WriteFile(actionsPath, a, 0644) + err = os.WriteFile(actionsPath, a, 0644) if err != nil { return errors.Wrap(err, "Failed to write the actions into a file") } @@ -647,7 +643,7 @@ func getEditorCommand() string { func migrateToV4(ctx context.DnoteCtx) error { configPath := fmt.Sprintf("%s/dnoterc", ctx.Paths.LegacyDnote) - b, err := ioutil.ReadFile(configPath) + b, err := os.ReadFile(configPath) if err != nil { return errors.Wrap(err, "Failed to read the config file") } @@ -668,7 +664,7 @@ func migrateToV4(ctx context.DnoteCtx) error { return errors.Wrap(err, "Failed to marshal config into JSON") } - err = ioutil.WriteFile(configPath, data, 0644) + err = os.WriteFile(configPath, data, 0644) if err != nil { return errors.Wrap(err, "Failed to write the config into a file") } @@ -680,7 +676,7 @@ func migrateToV4(ctx context.DnoteCtx) error { func migrateToV5(ctx context.DnoteCtx) error { actionsPath := fmt.Sprintf("%s/actions", ctx.Paths.LegacyDnote) - b, err := ioutil.ReadFile(actionsPath) + b, err := os.ReadFile(actionsPath) if err != nil { return errors.Wrap(err, "reading the actions file") } @@ -738,7 +734,7 @@ func migrateToV5(ctx context.DnoteCtx) error { if err != nil { return errors.Wrap(err, "marshalling result into JSON") } - err = ioutil.WriteFile(actionsPath, a, 0644) + err = os.WriteFile(actionsPath, a, 0644) if err != nil { return errors.Wrap(err, "writing the result into a file") } @@ -750,7 +746,7 @@ func migrateToV5(ctx context.DnoteCtx) error { func migrateToV6(ctx context.DnoteCtx) error { notePath := fmt.Sprintf("%s/dnote", ctx.Paths.LegacyDnote) - b, err := ioutil.ReadFile(notePath) + b, err := os.ReadFile(notePath) if err != nil { return errors.Wrap(err, "Failed to read the note file") } @@ -791,7 +787,7 @@ func migrateToV6(ctx context.DnoteCtx) error { return errors.Wrap(err, "Failed to marshal new dnote into JSON") } - err = ioutil.WriteFile(notePath, d, 0644) + err = os.WriteFile(notePath, d, 0644) if err != nil { return errors.Wrap(err, "Failed to write the new dnote into the file") } @@ -805,7 +801,7 @@ func migrateToV6(ctx context.DnoteCtx) error { func migrateToV7(ctx context.DnoteCtx) error { actionPath := fmt.Sprintf("%s/actions", ctx.Paths.LegacyDnote) - b, err := ioutil.ReadFile(actionPath) + b, err := os.ReadFile(actionPath) if err != nil { return errors.Wrap(err, "reading actions file") } @@ -857,7 +853,7 @@ func migrateToV7(ctx context.DnoteCtx) error { return errors.Wrap(err, "marshalling new actions") } - err = ioutil.WriteFile(actionPath, d, 0644) + err = os.WriteFile(actionPath, d, 0644) if err != nil { return errors.Wrap(err, "writing new actions to a file") } @@ -874,7 +870,7 @@ func migrateToV8(ctx context.DnoteCtx) error { // 1. Migrate the the dnote file dnoteFilePath := fmt.Sprintf("%s/dnote", ctx.Paths.LegacyDnote) - b, err := ioutil.ReadFile(dnoteFilePath) + b, err := os.ReadFile(dnoteFilePath) if err != nil { return errors.Wrap(err, "reading the notes") } @@ -914,7 +910,7 @@ func migrateToV8(ctx context.DnoteCtx) error { // 2. Migrate the actions file actionsPath := fmt.Sprintf("%s/actions", ctx.Paths.LegacyDnote) - b, err = ioutil.ReadFile(actionsPath) + b, err = os.ReadFile(actionsPath) if err != nil { return errors.Wrap(err, "reading the actions") } @@ -939,7 +935,7 @@ func migrateToV8(ctx context.DnoteCtx) error { // 3. Migrate the timestamps file timestampsPath := fmt.Sprintf("%s/timestamps", ctx.Paths.LegacyDnote) - b, err = ioutil.ReadFile(timestampsPath) + b, err = os.ReadFile(timestampsPath) if err != nil { return errors.Wrap(err, "reading the timestamps") } diff --git a/pkg/cli/migrate/legacy_test.go b/pkg/cli/migrate/legacy_test.go index 554f47ce..73d8b063 100644 --- a/pkg/cli/migrate/legacy_test.go +++ b/pkg/cli/migrate/legacy_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package migrate @@ -21,7 +18,6 @@ package migrate import ( "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" "testing" @@ -65,7 +61,7 @@ func TestMigrateToV1(t *testing.T) { if err != nil { panic(errors.Wrap(err, "Failed to get absolute YAML path").Error()) } - ioutil.WriteFile(yamlPath, []byte{}, 0644) + os.WriteFile(yamlPath, []byte{}, 0644) // execute if err := migrateToV1(ctx); err != nil { @@ -354,14 +350,18 @@ func TestMigrateToV7(t *testing.T) { } func TestMigrateToV8(t *testing.T) { - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-1-pre-schema.sql", SkipMigration: true} - db := database.InitTestDB(t, "../tmp/.dnote/dnote-test.db", &opts) - defer database.TeardownTestDB(t, db) + tmpDir := t.TempDir() + dnoteDir := tmpDir + "/.dnote" + if err := os.MkdirAll(dnoteDir, 0755); err != nil { + t.Fatal(errors.Wrap(err, "creating legacy dnote directory")) + } + + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql") ctx := context.DnoteCtx{ Paths: context.Paths{ - Home: "../tmp", - LegacyDnote: "../tmp/.dnote", + Home: tmpDir, + LegacyDnote: dnoteDir, }, DB: db, } diff --git a/pkg/cli/migrate/migrate.go b/pkg/cli/migrate/migrate.go index d7a3936a..9b4b0f50 100644 --- a/pkg/cli/migrate/migrate.go +++ b/pkg/cli/migrate/migrate.go @@ -1,25 +1,23 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package migrate import ( "database/sql" + "github.com/dnote/dnote/pkg/cli/consts" "github.com/dnote/dnote/pkg/cli/context" "github.com/dnote/dnote/pkg/cli/log" @@ -47,6 +45,8 @@ var LocalSequence = []migration{ lm10, lm11, lm12, + lm13, + lm14, } // RemoteSequence is a list of remote migrations to be run @@ -141,7 +141,7 @@ func Run(ctx context.DnoteCtx, migrations []migration, mode int) error { return errors.Wrap(err, "getting the current schema") } - log.Debug("current schema: %s %d of %d\n", consts.SystemSchema, schema, len(migrations)) + log.Debug("%s: %d of %d\n", schemaKey, schema, len(migrations)) toRun := migrations[schema:] diff --git a/pkg/cli/migrate/migrate_test.go b/pkg/cli/migrate/migrate_test.go index c5e2a717..7b31a91e 100644 --- a/pkg/cli/migrate/migrate_test.go +++ b/pkg/cli/migrate/migrate_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package migrate @@ -21,13 +18,14 @@ package migrate import ( "encoding/json" "fmt" - "gopkg.in/yaml.v2" - "io/ioutil" "net/http" "net/http/httptest" + "os" "testing" "time" + "gopkg.in/yaml.v2" + "github.com/dnote/actions" "github.com/dnote/dnote/pkg/assert" "github.com/dnote/dnote/pkg/cli/consts" @@ -37,12 +35,13 @@ import ( "github.com/pkg/errors" ) -var paths context.Paths = context.Paths{ - Home: "../../tmp", - Cache: "../../tmp", - Config: "../../tmp", - Data: "../../tmp", - LegacyDnote: "../../tmp", +// initTestDBNoMigration initializes a test database with schema.sql but removes +// migration version data so tests can control the migration state themselves. +func initTestDBNoMigration(t *testing.T) *database.DB { + db := database.InitTestMemoryDBRaw(t, "") + // Remove migration versions from schema.sql so tests can set their own + database.MustExec(t, "clearing schema versions", db, "DELETE FROM system WHERE key IN (?, ?)", consts.SystemSchema, consts.SystemRemoteSchema) + return db } func TestExecute_bump_schema(t *testing.T) { @@ -60,11 +59,8 @@ func TestExecute_bump_schema(t *testing.T) { for _, tc := range testCases { func() { // set up - opts := database.TestDBOptions{SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := initTestDBNoMigration(t) + ctx := context.InitTestCtxWithDB(t, db) database.MustExec(t, "inserting a schema", db, "INSERT INTO system (key, value) VALUES (?, ?)", tc.schemaKey, 8) @@ -117,11 +113,8 @@ func TestRun_nonfresh(t *testing.T) { for _, tc := range testCases { func() { // set up - opts := database.TestDBOptions{SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := initTestDBNoMigration(t) + ctx := context.InitTestCtxWithDB(t, db) database.MustExec(t, "inserting a schema", db, "INSERT INTO system (key, value) VALUES (?, ?)", tc.schemaKey, 2) database.MustExec(t, "creating a temporary table for testing", db, "CREATE TABLE migrate_run_test ( name string )") @@ -197,11 +190,8 @@ func TestRun_fresh(t *testing.T) { for _, tc := range testCases { func() { // set up - opts := database.TestDBOptions{SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := initTestDBNoMigration(t) + ctx := context.InitTestCtxWithDB(t, db) database.MustExec(t, "creating a temporary table for testing", db, "CREATE TABLE migrate_run_test ( name string )") @@ -271,11 +261,8 @@ func TestRun_up_to_date(t *testing.T) { for _, tc := range testCases { func() { // set up - opts := database.TestDBOptions{SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := initTestDBNoMigration(t) + ctx := context.InitTestCtxWithDB(t, db) database.MustExec(t, "creating a temporary table for testing", db, "CREATE TABLE migrate_run_test ( name string )") @@ -326,11 +313,8 @@ func TestRun_up_to_date(t *testing.T) { func TestLocalMigration1(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-1-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) data := testutils.MustMarshalJSON(t, actions.AddBookDataV1{BookName: "js"}) a1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting action", db, @@ -404,11 +388,8 @@ func TestLocalMigration1(t *testing.T) { func TestLocalMigration2(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-1-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) c1 := "note 1 - v1" c2 := "note 1 - v2" css := "css" @@ -491,11 +472,8 @@ func TestLocalMigration2(t *testing.T) { func TestLocalMigration3(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-1-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) data := testutils.MustMarshalJSON(t, actions.AddNoteDataV2{NoteUUID: "note-1-uuid", BookName: "js", Content: "note 1", Public: false}) a1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting action", db, @@ -566,11 +544,8 @@ func TestLocalMigration3(t *testing.T) { func TestLocalMigration4(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-1-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "css") @@ -610,11 +585,8 @@ func TestLocalMigration4(t *testing.T) { func TestLocalMigration5(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-5-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-5-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "css") @@ -672,11 +644,8 @@ func TestLocalMigration5(t *testing.T) { func TestLocalMigration6(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-5-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-5-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) data := testutils.MustMarshalJSON(t, actions.AddBookDataV1{BookName: "js"}) a1UUID := testutils.MustGenerateUUID(t) @@ -705,11 +674,8 @@ func TestLocalMigration6(t *testing.T) { func TestLocalMigration7_trash(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-7-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-7-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting trash book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "trash") @@ -738,11 +704,8 @@ func TestLocalMigration7_trash(t *testing.T) { func TestLocalMigration7_conflicts(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-7-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-7-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "conflicts") @@ -771,11 +734,8 @@ func TestLocalMigration7_conflicts(t *testing.T) { func TestLocalMigration7_conflicts_dup(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-7-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-7-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "conflicts") @@ -809,11 +769,8 @@ func TestLocalMigration7_conflicts_dup(t *testing.T) { func TestLocalMigration8(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-8-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-8-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1") @@ -846,13 +803,13 @@ func TestLocalMigration8(t *testing.T) { var n1AddedOn, n1EditedOn int64 var n1USN int var n1Public, n1Dirty, n1Deleted bool - database.MustScan(t, "scanning n1", db.QueryRow("SELECT book_uuid, body, added_on, edited_on, usn, public, dirty, deleted FROM notes WHERE uuid = ?", n1UUID), &n1BookUUID, &n1Body, &n1AddedOn, &n1EditedOn, &n1USN, &n1Public, &n1Dirty, &n1Deleted) + database.MustScan(t, "scanning n1", db.QueryRow("SELECT book_uuid, body, added_on, edited_on, usn, public, dirty, deleted FROM notes WHERE uuid = ?", n1UUID), &n1BookUUID, &n1Body, &n1AddedOn, &n1EditedOn, &n1USN, &n1Public, &n1Dirty, &n1Deleted) var n2BookUUID, n2Body string var n2AddedOn, n2EditedOn int64 var n2USN int var n2Public, n2Dirty, n2Deleted bool - database.MustScan(t, "scanning n2", db.QueryRow("SELECT book_uuid, body, added_on, edited_on, usn, public, dirty, deleted FROM notes WHERE uuid = ?", n2UUID), &n2BookUUID, &n2Body, &n2AddedOn, &n2EditedOn, &n2USN, &n2Public, &n2Dirty, &n2Deleted) + database.MustScan(t, "scanning n2", db.QueryRow("SELECT book_uuid, body, added_on, edited_on, usn, public, dirty, deleted FROM notes WHERE uuid = ?", n2UUID), &n2BookUUID, &n2Body, &n2AddedOn, &n2EditedOn, &n2USN, &n2Public, &n2Dirty, &n2Deleted) assert.Equal(t, n1BookUUID, b1UUID, "n1 BookUUID mismatch") assert.Equal(t, n1Body, "n1 Body", "n1 Body mismatch") @@ -875,11 +832,8 @@ func TestLocalMigration8(t *testing.T) { func TestLocalMigration9(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-9-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-9-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1") @@ -921,11 +875,8 @@ func TestLocalMigration9(t *testing.T) { func TestLocalMigration10(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-10-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-10-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting book ", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "123") @@ -993,11 +944,8 @@ func TestLocalMigration10(t *testing.T) { func TestLocalMigration11(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-11-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) - - db := ctx.DB + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-11-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) b1UUID := testutils.MustGenerateUUID(t) database.MustExec(t, "inserting book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "foo") @@ -1073,13 +1021,12 @@ func TestLocalMigration11(t *testing.T) { func TestLocalMigration12(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-12-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-12-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) data := []byte("editor: vim") - path := fmt.Sprintf("%s/dnoterc", ctx.Paths.LegacyDnote) - if err := ioutil.WriteFile(path, data, 0644); err != nil { + path := fmt.Sprintf("%s/%s/dnoterc", ctx.Paths.Config, consts.DnoteDirName) + if err := os.WriteFile(path, data, 0644); err != nil { t.Fatal(errors.Wrap(err, "Failed to write schema file")) } @@ -1090,7 +1037,7 @@ func TestLocalMigration12(t *testing.T) { } // test - b, err := ioutil.ReadFile(path) + b, err := os.ReadFile(path) if err != nil { t.Fatal(errors.Wrap(err, "reading config")) } @@ -1108,11 +1055,98 @@ func TestLocalMigration12(t *testing.T) { assert.NotEqual(t, cf.APIEndpoint, "", "apiEndpoint was not populated") } +func TestLocalMigration13(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-12-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + data := []byte("editor: vim\napiEndpoint: https://test.com/api") + + path := fmt.Sprintf("%s/%s/dnoterc", ctx.Paths.Config, consts.DnoteDirName) + if err := os.WriteFile(path, data, 0644); err != nil { + t.Fatal(errors.Wrap(err, "Failed to write schema file")) + } + + // execute + err := lm13.run(ctx, nil) + if err != nil { + t.Fatal(errors.Wrap(err, "failed to run")) + } + + // test + b, err := os.ReadFile(path) + if err != nil { + t.Fatal(errors.Wrap(err, "reading config")) + } + + type config struct { + Editor string `yaml:"editor"` + ApiEndpoint string `yaml:"apiEndpoint"` + EnableUpgradeCheck bool `yaml:"enableUpgradeCheck"` + } + + var cf config + err = yaml.Unmarshal(b, &cf) + if err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling config")) + } + + assert.Equal(t, cf.Editor, "vim", "editor mismatch") + assert.Equal(t, cf.ApiEndpoint, "https://test.com/api", "apiEndpoint mismatch") + assert.Equal(t, cf.EnableUpgradeCheck, true, "enableUpgradeCheck mismatch") +} + +func TestLocalMigration14(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-14-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1") + + n1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting note", db, `INSERT INTO notes + (uuid, book_uuid, body, added_on, edited_on, public, dirty, usn, deleted) VALUES + (?, ?, ?, ?, ?, ?, ?, ?, ?)`, n1UUID, b1UUID, "test note", 1, 2, true, false, 0, false) + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm14.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test - verify public column was dropped by checking column names + rows, err := db.Query("SELECT name FROM pragma_table_info('notes')") + if err != nil { + t.Fatal(errors.Wrap(err, "getting table info")) + } + defer rows.Close() + + for rows.Next() { + var name string + err := rows.Scan(&name) + if err != nil { + t.Fatal(errors.Wrap(err, "scanning column name")) + } + + if name == "public" { + t.Fatal("public column still exists after migration") + } + } +} + func TestRemoteMigration1(t *testing.T) { // set up - opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/remote-1-pre-schema.sql", SkipMigration: true} - ctx := context.InitTestCtx(t, paths, &opts) - defer context.TeardownTestCtx(t, ctx) + db := database.InitTestMemoryDBRaw(t, "./fixtures/remote-1-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) testutils.Login(t, &ctx) JSBookUUID := "existing-js-book-uuid" @@ -1152,7 +1186,6 @@ func TestRemoteMigration1(t *testing.T) { ctx.APIEndpoint = server.URL - db := ctx.DB database.MustExec(t, "inserting js book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", JSBookUUID, "js") database.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", CSSBookUUID, "css") database.MustExec(t, "inserting linux book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", linuxBookUUID, "linux") diff --git a/pkg/cli/migrate/migrations.go b/pkg/cli/migrate/migrations.go index 254de773..197a2b03 100644 --- a/pkg/cli/migrate/migrations.go +++ b/pkg/cli/migrate/migrations.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package migrate @@ -539,7 +536,10 @@ var lm12 = migration{ return errors.Wrap(err, "reading config") } - cf.APIEndpoint = "https://api.getdnote.com" + // Only set if not already configured + if cf.APIEndpoint == "" { + cf.APIEndpoint = "https://api.getdnote.com" + } err = config.Write(ctx, cf) if err != nil { @@ -550,6 +550,37 @@ var lm12 = migration{ }, } +var lm13 = migration{ + name: "add enableUpgradeCheck to the configuration file", + run: func(ctx context.DnoteCtx, tx *database.DB) error { + cf, err := config.Read(ctx) + if err != nil { + return errors.Wrap(err, "reading config") + } + + cf.EnableUpgradeCheck = true + + err = config.Write(ctx, cf) + if err != nil { + return errors.Wrap(err, "writing config") + } + + return nil + }, +} + +var lm14 = migration{ + name: "drop-public-from-notes", + run: func(ctx context.DnoteCtx, tx *database.DB) error { + _, err := tx.Exec(`ALTER TABLE notes DROP COLUMN public;`) + if err != nil { + return errors.Wrap(err, "dropping public column from notes") + } + + return nil + }, +} + var rm1 = migration{ name: "sync-book-uuids-from-server", run: func(ctx context.DnoteCtx, tx *database.DB) error { diff --git a/pkg/cli/output/output.go b/pkg/cli/output/output.go index c5c7b1ec..d272ba88 100644 --- a/pkg/cli/output/output.go +++ b/pkg/cli/output/output.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package output provides functions to print informations on the terminal @@ -22,6 +19,7 @@ package output import ( "fmt" + "io" "time" "github.com/dnote/dnote/pkg/cli/database" @@ -29,7 +27,7 @@ import ( ) // NoteInfo prints a note information -func NoteInfo(info database.NoteInfo) { +func NoteInfo(w io.Writer, info database.NoteInfo) { log.Infof("book name: %s\n", info.BookLabel) log.Infof("created at: %s\n", time.Unix(0, info.AddedOn).Format("Jan 2, 2006 3:04pm (MST)")) if info.EditedOn != 0 { @@ -38,13 +36,13 @@ func NoteInfo(info database.NoteInfo) { log.Infof("note id: %d\n", info.RowID) log.Infof("note uuid: %s\n", info.UUID) - fmt.Printf("\n------------------------content------------------------\n") - fmt.Printf("%s", info.Content) - fmt.Printf("\n-------------------------------------------------------\n") + fmt.Fprintf(w, "\n------------------------content------------------------\n") + fmt.Fprintf(w, "%s", info.Content) + fmt.Fprintf(w, "\n-------------------------------------------------------\n") } -func NoteContent(info database.NoteInfo) { - fmt.Printf("%s", info.Content) +func NoteContent(w io.Writer, info database.NoteInfo) { + fmt.Fprintf(w, "%s", info.Content) } // BookInfo prints a note information diff --git a/pkg/cli/testutils/main.go b/pkg/cli/testutils/main.go index 592a2db0..db3282d7 100644 --- a/pkg/cli/testutils/main.go +++ b/pkg/cli/testutils/main.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package testutils provides utilities used in tests @@ -23,7 +20,6 @@ import ( "bytes" "encoding/json" "io" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -31,6 +27,7 @@ import ( "testing" "time" + "github.com/dnote/dnote/pkg/assert" "github.com/dnote/dnote/pkg/cli/consts" "github.com/dnote/dnote/pkg/cli/context" "github.com/dnote/dnote/pkg/cli/database" @@ -38,12 +35,25 @@ import ( "github.com/pkg/errors" ) -// Login simulates a logged in user by inserting credentials in the local database -func Login(t *testing.T, ctx *context.DnoteCtx) { - db := ctx.DB +// Prompts for user input +const ( + PromptRemoveNote = "remove this note?" + PromptDeleteBook = "delete book" + PromptEmptyServer = "The server is empty but you have local data" +) +// Timeout for waiting for prompts in tests +const promptTimeout = 10 * time.Second + +// LoginDB sets up login credentials in the database for tests +func LoginDB(t *testing.T, db *database.DB) { database.MustExec(t, "inserting sessionKey", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemSessionKey, "someSessionKey") database.MustExec(t, "inserting sessionKeyExpiry", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemSessionKeyExpiry, time.Now().Add(24*time.Hour).Unix()) +} + +// Login simulates a logged in user by inserting credentials in the local database +func Login(t *testing.T, ctx *context.DnoteCtx) { + LoginDB(t, ctx.DB) ctx.SessionKey = "someSessionKey" ctx.SessionKeyExpiry = time.Now().Add(24 * time.Hour).Unix() @@ -81,7 +91,7 @@ func WriteFile(ctx context.DnoteCtx, content []byte, filename string) { panic(err) } - if err := ioutil.WriteFile(dp, content, 0644); err != nil { + if err := os.WriteFile(dp, content, 0644); err != nil { panic(err) } } @@ -90,7 +100,7 @@ func WriteFile(ctx context.DnoteCtx, content []byte, filename string) { func ReadFile(ctx context.DnoteCtx, filename string) []byte { path := filepath.Join(ctx.Paths.LegacyDnote, filename) - b, err := ioutil.ReadFile(path) + b, err := os.ReadFile(path) if err != nil { panic(err) } @@ -101,7 +111,7 @@ func ReadFile(ctx context.DnoteCtx, filename string) []byte { // ReadJSON reads JSON fixture to the struct at the destination address func ReadJSON(path string, destination interface{}) { var dat []byte - dat, err := ioutil.ReadFile(path) + dat, err := os.ReadFile(path) if err != nil { panic(errors.Wrap(err, "Failed to load fixture payload")) } @@ -134,7 +144,7 @@ type RunDnoteCmdOptions struct { } // RunDnoteCmd runs a dnote command -func RunDnoteCmd(t *testing.T, opts RunDnoteCmdOptions, binaryName string, arg ...string) { +func RunDnoteCmd(t *testing.T, opts RunDnoteCmdOptions, binaryName string, arg ...string) string { t.Logf("running: %s %s", binaryName, strings.Join(arg, " ")) cmd, stderr, stdout, err := NewDnoteCmd(opts, binaryName, arg...) @@ -152,60 +162,100 @@ func RunDnoteCmd(t *testing.T, opts RunDnoteCmdOptions, binaryName string, arg . // Print stdout if and only if test fails later t.Logf("\n%s", stdout) + + return stdout.String() } -// WaitDnoteCmd runs a dnote command and waits until the command is exited -func WaitDnoteCmd(t *testing.T, opts RunDnoteCmdOptions, runFunc func(io.WriteCloser) error, binaryName string, arg ...string) { +// WaitDnoteCmd runs a dnote command and passes stdout to the callback. +func WaitDnoteCmd(t *testing.T, opts RunDnoteCmdOptions, runFunc func(io.Reader, io.WriteCloser) error, binaryName string, arg ...string) (string, error) { t.Logf("running: %s %s", binaryName, strings.Join(arg, " ")) - cmd, stderr, stdout, err := NewDnoteCmd(opts, binaryName, arg...) + binaryPath, err := filepath.Abs(binaryName) if err != nil { - t.Logf("\n%s", stdout) - t.Fatal(errors.Wrap(err, "getting command").Error()) + return "", errors.Wrap(err, "getting absolute path to test binary") + } + + cmd := exec.Command(binaryPath, arg...) + cmd.Env = opts.Env + + var stderr bytes.Buffer + cmd.Stderr = &stderr + + stdout, err := cmd.StdoutPipe() + if err != nil { + return "", errors.Wrap(err, "getting stdout pipe") } stdin, err := cmd.StdinPipe() if err != nil { - t.Logf("\n%s", stdout) - t.Fatal(errors.Wrap(err, "getting stdin %s")) + return "", errors.Wrap(err, "getting stdin") } defer stdin.Close() - // Start the program - err = cmd.Start() - if err != nil { - t.Logf("\n%s", stdout) - t.Fatal(errors.Wrap(err, "starting command")) + if err = cmd.Start(); err != nil { + return "", errors.Wrap(err, "starting command") } - err = runFunc(stdin) + var output bytes.Buffer + tee := io.TeeReader(stdout, &output) + + err = runFunc(tee, stdin) if err != nil { - t.Logf("\n%s", stdout) - t.Fatal(errors.Wrap(err, "running with stdin")) + t.Logf("\n%s", output.String()) + return output.String(), errors.Wrap(err, "running callback") } - err = cmd.Wait() - if err != nil { - t.Logf("\n%s", stdout) - t.Fatal(errors.Wrapf(err, "running command %s", stderr.String())) + io.Copy(&output, stdout) + + if err := cmd.Wait(); err != nil { + t.Logf("\n%s", output.String()) + return output.String(), errors.Wrapf(err, "command failed: %s", stderr.String()) } - // Print stdout if and only if test fails later - t.Logf("\n%s", stdout) + t.Logf("\n%s", output.String()) + return output.String(), nil } -// UserConfirm simulates confirmation from the user by writing to stdin -func UserConfirm(stdin io.WriteCloser) error { - // confirm - if _, err := io.WriteString(stdin, "y\n"); err != nil { - return errors.Wrap(err, "indicating confirmation in stdin") +func MustWaitDnoteCmd(t *testing.T, opts RunDnoteCmdOptions, runFunc func(io.Reader, io.WriteCloser) error, binaryName string, arg ...string) string { + output, err := WaitDnoteCmd(t, opts, runFunc, binaryName, arg...) + if err != nil { + t.Fatal(err) } - return nil + return output } -// UserContent simulates content from the user by writing to stdin -func UserContent(stdin io.WriteCloser) error { +// MustWaitForPrompt waits for an expected prompt with a default timeout. +// Fails the test if the prompt is not found or an error occurs. +func MustWaitForPrompt(t *testing.T, stdout io.Reader, expectedPrompt string) { + if err := assert.WaitForPrompt(stdout, expectedPrompt, promptTimeout); err != nil { + t.Fatal(err) + } +} + +// ConfirmRemoveNote waits for prompt for removing a note and confirms. +func ConfirmRemoveNote(stdout io.Reader, stdin io.WriteCloser) error { + return assert.RespondToPrompt(stdout, stdin, PromptRemoveNote, "y\n", promptTimeout) +} + +// ConfirmRemoveBook waits for prompt for deleting a book confirms. +func ConfirmRemoveBook(stdout io.Reader, stdin io.WriteCloser) error { + return assert.RespondToPrompt(stdout, stdin, PromptDeleteBook, "y\n", promptTimeout) +} + +// UserConfirmEmptyServerSync waits for an empty server prompt and confirms. +func UserConfirmEmptyServerSync(stdout io.Reader, stdin io.WriteCloser) error { + return assert.RespondToPrompt(stdout, stdin, PromptEmptyServer, "y\n", promptTimeout) +} + +// UserCancelEmptyServerSync waits for an empty server prompt and cancels. +func UserCancelEmptyServerSync(stdout io.Reader, stdin io.WriteCloser) error { + return assert.RespondToPrompt(stdout, stdin, PromptEmptyServer, "n\n", promptTimeout) +} + +// UserContent simulates content from the user by writing to stdin. +// This is used for piped input where no prompt is shown. +func UserContent(stdout io.Reader, stdin io.WriteCloser) error { longText := `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.` @@ -249,3 +299,12 @@ func MustGenerateUUID(t *testing.T) string { return ret } + +func MustOpenDatabase(t *testing.T, dbPath string) *database.DB { + db, err := database.Open(dbPath) + if err != nil { + t.Fatal(errors.Wrap(err, "opening database")) + } + + return db +} diff --git a/pkg/cli/testutils/setup.go b/pkg/cli/testutils/setup.go index d6fc6e8a..d88afd2d 100644 --- a/pkg/cli/testutils/setup.go +++ b/pkg/cli/testutils/setup.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package testutils diff --git a/pkg/cli/ui/editor.go b/pkg/cli/ui/editor.go index cfe8fc22..a6c165f4 100644 --- a/pkg/cli/ui/editor.go +++ b/pkg/cli/ui/editor.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package ui provides the user interface for the program @@ -21,7 +18,6 @@ package ui import ( "fmt" - "io/ioutil" "os" "os/exec" "strings" @@ -122,7 +118,7 @@ func GetEditorInput(ctx context.DnoteCtx, fpath string) (string, error) { return "", errors.Wrap(err, "waiting for the editor") } - b, err := ioutil.ReadFile(fpath) + b, err := os.ReadFile(fpath) if err != nil { return "", errors.Wrap(err, "reading the temporary content file") } diff --git a/pkg/cli/ui/editor_test.go b/pkg/cli/ui/editor_test.go index 60df617a..54538c49 100644 --- a/pkg/cli/ui/editor_test.go +++ b/pkg/cli/ui/editor_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package ui @@ -30,11 +27,7 @@ import ( func TestGetTmpContentPath(t *testing.T) { t.Run("no collision", func(t *testing.T) { - ctx := context.InitTestCtx(t, context.Paths{ - Data: "../tmp", - Cache: "../tmp", - }, nil) - defer context.TeardownTestCtx(t, ctx) + ctx := context.InitTestCtx(t) res, err := GetTmpContentPath(ctx) if err != nil { @@ -47,11 +40,7 @@ func TestGetTmpContentPath(t *testing.T) { t.Run("one existing session", func(t *testing.T) { // set up - ctx := context.InitTestCtx(t, context.Paths{ - Data: "../tmp2", - Cache: "../tmp2", - }, nil) - defer context.TeardownTestCtx(t, ctx) + ctx := context.InitTestCtx(t) p := fmt.Sprintf("%s/%s", ctx.Paths.Cache, "DNOTE_TMPCONTENT_0.md") if _, err := os.Create(p); err != nil { @@ -71,11 +60,7 @@ func TestGetTmpContentPath(t *testing.T) { t.Run("two existing sessions", func(t *testing.T) { // set up - ctx := context.InitTestCtx(t, context.Paths{ - Data: "../tmp3", - Cache: "../tmp3", - }, nil) - defer context.TeardownTestCtx(t, ctx) + ctx := context.InitTestCtx(t) p1 := fmt.Sprintf("%s/%s", ctx.Paths.Cache, "DNOTE_TMPCONTENT_0.md") if _, err := os.Create(p1); err != nil { diff --git a/pkg/cli/ui/terminal.go b/pkg/cli/ui/terminal.go index c58547f2..6a255766 100644 --- a/pkg/cli/ui/terminal.go +++ b/pkg/cli/ui/terminal.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package ui @@ -26,6 +23,7 @@ import ( "syscall" "github.com/dnote/dnote/pkg/cli/log" + "github.com/dnote/dnote/pkg/prompt" "github.com/pkg/errors" "golang.org/x/crypto/ssh/terminal" ) @@ -73,26 +71,16 @@ func PromptPassword(message string, dest *string) error { // Confirm prompts for user input to confirm a choice func Confirm(question string, optimistic bool) (bool, error) { - var choices string - if optimistic { - choices = "(Y/n)" - } else { - choices = "(y/N)" - } + message := prompt.FormatQuestion(question, optimistic) - message := fmt.Sprintf("%s %s", question, choices) + // Use log.Askf for colored prompt in CLI + log.Askf(message, false) - var input string - if err := PromptInput(message, &input); err != nil { + confirmed, err := prompt.ReadYesNo(os.Stdin, optimistic) + if err != nil { return false, errors.Wrap(err, "Failed to get user input") } - confirmed := input == "y" - - if optimistic { - confirmed = confirmed || input == "" - } - return confirmed, nil } @@ -110,4 +98,4 @@ func ReadStdInput() (string, error) { } return strings.Join(lines, "\n"), nil -} \ No newline at end of file +} diff --git a/pkg/cli/upgrade/upgrade.go b/pkg/cli/upgrade/upgrade.go index c7df1d5e..7c24a86d 100644 --- a/pkg/cli/upgrade/upgrade.go +++ b/pkg/cli/upgrade/upgrade.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package upgrade @@ -32,8 +29,8 @@ import ( "github.com/pkg/errors" ) -// upgradeInterval is 3 weeks -var upgradeInterval int64 = 86400 * 7 * 3 +// upgradeInterval is 8 weeks +var upgradeInterval int64 = 86400 * 7 * 8 // shouldCheckUpdate checks if update should be checked func shouldCheckUpdate(ctx context.DnoteCtx) (bool, error) { @@ -112,6 +109,11 @@ func checkVersion(ctx context.DnoteCtx) error { // Check triggers update if needed func Check(ctx context.DnoteCtx) error { + // If upgrade check is not enabled, do not proceed further + if !ctx.EnableUpgradeCheck { + return nil + } + shouldCheck, err := shouldCheckUpdate(ctx) if err != nil { return errors.Wrap(err, "checking if dnote should check update") diff --git a/pkg/cli/upgrade/upgrade_test.go b/pkg/cli/upgrade/upgrade_test.go index 81442121..917e6754 100644 --- a/pkg/cli/upgrade/upgrade_test.go +++ b/pkg/cli/upgrade/upgrade_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package upgrade diff --git a/pkg/cli/utils/diff/diff.go b/pkg/cli/utils/diff/diff.go index 05fd2520..ff7e3384 100644 --- a/pkg/cli/utils/diff/diff.go +++ b/pkg/cli/utils/diff/diff.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package diff provides line-by-line diff feature by wrapping diff --git a/pkg/cli/utils/diff/diff_test.go b/pkg/cli/utils/diff/diff_test.go index 06d4deb4..f777f26c 100644 --- a/pkg/cli/utils/diff/diff_test.go +++ b/pkg/cli/utils/diff/diff_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package diff diff --git a/pkg/cli/utils/files.go b/pkg/cli/utils/files.go index 4278a5ad..60f57de6 100644 --- a/pkg/cli/utils/files.go +++ b/pkg/cli/utils/files.go @@ -1,26 +1,22 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package utils import ( "io" - "io/ioutil" "os" "path/filepath" @@ -35,7 +31,7 @@ func ReadFileAbs(relpath string) []byte { panic(err) } - b, err := ioutil.ReadFile(fp) + b, err := os.ReadFile(fp) if err != nil { panic(err) } @@ -56,6 +52,24 @@ func FileExists(filepath string) (bool, error) { return false, errors.Wrap(err, "getting file info") } +// EnsureDir creates a directory if it doesn't exist. +// Returns nil if the directory already exists or was successfully created. +func EnsureDir(path string) error { + ok, err := FileExists(path) + if err != nil { + return errors.Wrapf(err, "checking if dir exists at %s", path) + } + if ok { + return nil + } + + if err := os.MkdirAll(path, 0755); err != nil { + return errors.Wrapf(err, "creating directory at %s", path) + } + + return nil +} + // CopyDir copies a directory from src to dest, recursively copying nested // directories func CopyDir(src, dest string) error { @@ -80,7 +94,7 @@ func CopyDir(src, dest string) error { return errors.Wrap(err, "creating destination") } - entries, err := ioutil.ReadDir(src) + entries, err := os.ReadDir(src) if err != nil { return errors.Wrap(err, "reading the directory listing for the input") } diff --git a/pkg/cli/utils/files_test.go b/pkg/cli/utils/files_test.go new file mode 100644 index 00000000..7f26e19c --- /dev/null +++ b/pkg/cli/utils/files_test.go @@ -0,0 +1,42 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package utils + +import ( + "os" + "path/filepath" + "testing" + + "github.com/dnote/dnote/pkg/assert" +) + +func TestEnsureDir(t *testing.T) { + tmpDir := t.TempDir() + testPath := filepath.Join(tmpDir, "test", "nested", "dir") + + // Create directory + err := EnsureDir(testPath) + assert.Equal(t, err, nil, "EnsureDir should succeed") + + // Verify it exists + info, err := os.Stat(testPath) + assert.Equal(t, err, nil, "directory should exist") + assert.Equal(t, info.IsDir(), true, "should be a directory") + + // Call again on existing directory - should not error + err = EnsureDir(testPath) + assert.Equal(t, err, nil, "EnsureDir should succeed on existing directory") +} diff --git a/pkg/cli/utils/utils.go b/pkg/cli/utils/utils.go index f6d24f9c..417963ce 100644 --- a/pkg/cli/utils/utils.go +++ b/pkg/cli/utils/utils.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package utils diff --git a/pkg/cli/validate/book_test.go b/pkg/cli/validate/book_test.go index 7a5c0661..97a384fc 100644 --- a/pkg/cli/validate/book_test.go +++ b/pkg/cli/validate/book_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package validate diff --git a/pkg/cli/validate/books.go b/pkg/cli/validate/books.go index 7a56c30d..b0e4c0ed 100644 --- a/pkg/cli/validate/books.go +++ b/pkg/cli/validate/books.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package validate diff --git a/pkg/clock/clock.go b/pkg/clock/clock.go index d4b04419..061bb1a3 100644 --- a/pkg/clock/clock.go +++ b/pkg/clock/clock.go @@ -1,30 +1,26 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package clock provides an abstract layer over the standard time package package clock import ( + "sync" "time" ) -//TODO: use mutex to avoid race - // Clock is an interface to the standard library time. // It is used to implement a real or a mock clock. The latter is used in tests. type Clock interface { @@ -39,16 +35,21 @@ func (c *clock) Now() time.Time { // Mock is a mock instance of clock type Mock struct { + mu sync.RWMutex currentTime time.Time } // SetNow sets the current time for the mock clock func (c *Mock) SetNow(t time.Time) { + c.mu.Lock() + defer c.mu.Unlock() c.currentTime = t } // Now returns the current time func (c *Mock) Now() time.Time { + c.mu.RLock() + defer c.mu.RUnlock() return c.currentTime } diff --git a/pkg/cli/dirs/dirs.go b/pkg/dirs/dirs.go similarity index 57% rename from pkg/cli/dirs/dirs.go rename to pkg/dirs/dirs.go index 50b85822..3eb57a7f 100644 --- a/pkg/cli/dirs/dirs.go +++ b/pkg/dirs/dirs.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package dirs provides base directory definitions for the system diff --git a/pkg/dirs/dirs_test.go b/pkg/dirs/dirs_test.go new file mode 100644 index 00000000..0c1ecb1f --- /dev/null +++ b/pkg/dirs/dirs_test.go @@ -0,0 +1,39 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dirs + +import ( + "testing" + + "github.com/dnote/dnote/pkg/assert" +) + +type envTestCase struct { + envKey string + envVal string + got *string + expected string +} + +func testCustomDirs(t *testing.T, testCases []envTestCase) { + for _, tc := range testCases { + t.Setenv(tc.envKey, tc.envVal) + + Reload() + + assert.Equal(t, *tc.got, tc.expected, "result mismatch") + } +} diff --git a/pkg/dirs/dirs_unix.go b/pkg/dirs/dirs_unix.go new file mode 100644 index 00000000..6c7696c6 --- /dev/null +++ b/pkg/dirs/dirs_unix.go @@ -0,0 +1,49 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//go:build linux || darwin || freebsd + + +package dirs + +import ( + "path/filepath" +) + +// The environment variable names for the XDG base directory specification +var ( + envConfigHome = "XDG_CONFIG_HOME" + envDataHome = "XDG_DATA_HOME" + envCacheHome = "XDG_CACHE_HOME" +) + +func initDirs() { + Home = getHomeDir() + ConfigHome = readPath(envConfigHome, getConfigHome(Home)) + DataHome = readPath(envDataHome, getDataHome(Home)) + CacheHome = readPath(envCacheHome, getCacheHome(Home)) +} + +func getConfigHome(homeDir string) string { + return filepath.Join(homeDir, ".config") +} + +func getDataHome(homeDir string) string { + return filepath.Join(homeDir, ".local/share") +} + +func getCacheHome(homeDir string) string { + return filepath.Join(homeDir, ".cache") +} diff --git a/pkg/cli/dirs/dirs_unix_test.go b/pkg/dirs/dirs_unix_test.go similarity index 60% rename from pkg/cli/dirs/dirs_unix_test.go rename to pkg/dirs/dirs_unix_test.go index 2b044ca0..9ea5805a 100644 --- a/pkg/cli/dirs/dirs_unix_test.go +++ b/pkg/dirs/dirs_unix_test.go @@ -1,22 +1,19 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -//go:build linux || darwin +//go:build linux || darwin || freebsd package dirs diff --git a/pkg/dirs/dirs_windows.go b/pkg/dirs/dirs_windows.go new file mode 100644 index 00000000..7159ae8d --- /dev/null +++ b/pkg/dirs/dirs_windows.go @@ -0,0 +1,30 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//go:build windows + + +package dirs + +import ( + "path/filepath" +) + +func initDirs() { + Home = getHomeDir() + ConfigHome = filepath.Join(Home, ".dnote") + DataHome = filepath.Join(Home, ".dnote") + CacheHome = filepath.Join(Home, ".dnote") +} diff --git a/pkg/dirs/dirs_windows_test.go b/pkg/dirs/dirs_windows_test.go new file mode 100644 index 00000000..101071f5 --- /dev/null +++ b/pkg/dirs/dirs_windows_test.go @@ -0,0 +1,57 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//go:build windows + + +package dirs + +import ( + "path/filepath" + "testing" + + "github.com/dnote/dnote/pkg/assert" +) + +func TestDirs(t *testing.T) { + home := Home + assert.NotEqual(t, home, "", "home is empty") + + configHome := filepath.Join(home, ".dnote") + dataHome := filepath.Join(home, ".dnote") + cacheHome := filepath.Join(home, ".dnote") + + testCases := []struct { + got string + expected string + }{ + { + got: ConfigHome, + expected: configHome, + }, + { + got: DataHome, + expected: dataHome, + }, + { + got: CacheHome, + expected: cacheHome, + }, + } + + for _, tc := range testCases { + assert.Equal(t, tc.got, tc.expected, "result mismatch") + } +} diff --git a/pkg/e2e/server_test.go b/pkg/e2e/server_test.go new file mode 100644 index 00000000..e8ca3da6 --- /dev/null +++ b/pkg/e2e/server_test.go @@ -0,0 +1,353 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "bytes" + "fmt" + "net/http" + "os" + "os/exec" + "strings" + "testing" + "time" + + "github.com/dnote/dnote/pkg/assert" + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +var testServerBinary string + +func init() { + // Build server binary in temp directory + tmpDir := os.TempDir() + testServerBinary = fmt.Sprintf("%s/dnote-test-server", tmpDir) + buildCmd := exec.Command("go", "build", "-tags", "fts5", "-o", testServerBinary, "../server") + if out, err := buildCmd.CombinedOutput(); err != nil { + panic(fmt.Sprintf("failed to build server: %v\n%s", err, out)) + } +} + +func TestServerStart(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + port := "13456" // Use different port to avoid conflicts with main test server + + // Start server in background + cmd := exec.Command(testServerBinary, "start", "--port", port) + cmd.Env = append(os.Environ(), + "DBPath="+tmpDB, + ) + + if err := cmd.Start(); err != nil { + t.Fatalf("failed to start server: %v", err) + } + + // Ensure cleanup + cleanup := func() { + if cmd.Process != nil { + cmd.Process.Kill() + cmd.Wait() // Wait for process to fully exit + } + } + defer cleanup() + + // Wait for server to start and migrations to run + time.Sleep(3 * time.Second) + + // Verify server responds to health check + resp, err := http.Get(fmt.Sprintf("http://localhost:%s/health", port)) + if err != nil { + t.Fatalf("failed to reach server health endpoint: %v", err) + } + defer resp.Body.Close() + + assert.Equal(t, resp.StatusCode, 200, "health endpoint should return 200") + + // Kill server before checking database to avoid locks + cleanup() + + // Verify database file was created + if _, err := os.Stat(tmpDB); os.IsNotExist(err) { + t.Fatalf("database file was not created at %s", tmpDB) + } + + // Verify migrations ran by checking database + db, err := gorm.Open(sqlite.Open(tmpDB), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open test database: %v", err) + } + + // Verify migrations ran + var count int64 + if err := db.Raw("SELECT COUNT(*) FROM schema_migrations").Scan(&count).Error; err != nil { + t.Fatalf("schema_migrations table not found: %v", err) + } + if count == 0 { + t.Fatal("no migrations were run") + } + + // Verify FTS table exists and is functional + if err := db.Exec("SELECT * FROM notes_fts LIMIT 1").Error; err != nil { + t.Fatalf("notes_fts table not found or not functional: %v", err) + } +} + +func TestServerVersion(t *testing.T) { + cmd := exec.Command("go", "run", "-tags", "fts5", "../server", "version") + output, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("version command failed: %v", err) + } + + outputStr := string(output) + if !strings.Contains(outputStr, "dnote-server-") { + t.Errorf("expected version output to contain 'dnote-server-', got: %s", outputStr) + } +} + +func TestServerRootCommand(t *testing.T) { + cmd := exec.Command(testServerBinary) + output, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("server command failed: %v", err) + } + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "Dnote server - a simple command line notebook"), true, "output should contain description") + assert.Equal(t, strings.Contains(outputStr, "start: Start the server"), true, "output should contain start command") + assert.Equal(t, strings.Contains(outputStr, "version: Print the version"), true, "output should contain version command") +} + +func TestServerStartHelp(t *testing.T) { + cmd := exec.Command(testServerBinary, "start", "--help") + output, _ := cmd.CombinedOutput() + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "dnote-server start [flags]"), true, "output should contain usage") + assert.Equal(t, strings.Contains(outputStr, "--port"), true, "output should contain port flag") + assert.Equal(t, strings.Contains(outputStr, "--baseUrl"), true, "output should contain baseUrl flag") + assert.Equal(t, strings.Contains(outputStr, "--dbPath"), true, "output should contain dbPath flag") + assert.Equal(t, strings.Contains(outputStr, "--disableRegistration"), true, "output should contain disableRegistration flag") +} + +func TestServerStartInvalidConfig(t *testing.T) { + cmd := exec.Command(testServerBinary, "start") + // Set invalid BaseURL to trigger validation failure + cmd.Env = []string{"BaseURL=not-a-valid-url"} + + output, err := cmd.CombinedOutput() + + // Should exit with non-zero status + if err == nil { + t.Fatal("expected command to fail with invalid config") + } + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "Error:"), true, "output should contain error message") + assert.Equal(t, strings.Contains(outputStr, "Invalid BaseURL"), true, "output should mention invalid BaseURL") + assert.Equal(t, strings.Contains(outputStr, "dnote-server start [flags]"), true, "output should show usage") + assert.Equal(t, strings.Contains(outputStr, "--baseUrl"), true, "output should show flags") +} + +func TestServerUnknownCommand(t *testing.T) { + cmd := exec.Command(testServerBinary, "unknown") + output, err := cmd.CombinedOutput() + + // Should exit with non-zero status + if err == nil { + t.Fatal("expected command to fail with unknown command") + } + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "Unknown command"), true, "output should contain unknown command message") + assert.Equal(t, strings.Contains(outputStr, "Dnote server - a simple command line notebook"), true, "output should show help") +} + +func TestServerUserCreate(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + cmd := exec.Command(testServerBinary, "user", "create", + "--dbPath", tmpDB, + "--email", "test@example.com", + "--password", "password123") + output, err := cmd.CombinedOutput() + + if err != nil { + t.Fatalf("user create failed: %v\nOutput: %s", err, output) + } + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "User created successfully"), true, "output should show success message") + assert.Equal(t, strings.Contains(outputStr, "test@example.com"), true, "output should show email") + + // Verify user exists in database + db, err := gorm.Open(sqlite.Open(tmpDB), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open database: %v", err) + } + defer func() { + sqlDB, _ := db.DB() + sqlDB.Close() + }() + + var count int64 + db.Table("users").Count(&count) + assert.Equal(t, count, int64(1), "should have created 1 user") +} + +func TestServerUserCreateShortPassword(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + cmd := exec.Command(testServerBinary, "user", "create", + "--dbPath", tmpDB, + "--email", "test@example.com", + "--password", "short") + output, err := cmd.CombinedOutput() + + // Should fail with short password + if err == nil { + t.Fatal("expected command to fail with short password") + } + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "password should be longer than 8 characters"), true, "output should show password error") +} + +func TestServerUserResetPassword(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + // Create user first + createCmd := exec.Command(testServerBinary, "user", "create", + "--dbPath", tmpDB, + "--email", "test@example.com", + "--password", "oldpassword123") + if output, err := createCmd.CombinedOutput(); err != nil { + t.Fatalf("failed to create user: %v\nOutput: %s", err, output) + } + + // Reset password + resetCmd := exec.Command(testServerBinary, "user", "reset-password", + "--dbPath", tmpDB, + "--email", "test@example.com", + "--password", "newpassword123") + output, err := resetCmd.CombinedOutput() + + if err != nil { + t.Fatalf("reset-password failed: %v\nOutput: %s", err, output) + } + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "Password reset successfully"), true, "output should show success message") +} + +func TestServerUserRemove(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + // Create user first + createCmd := exec.Command(testServerBinary, "user", "create", + "--dbPath", tmpDB, + "--email", "test@example.com", + "--password", "password123") + if output, err := createCmd.CombinedOutput(); err != nil { + t.Fatalf("failed to create user: %v\nOutput: %s", err, output) + } + + // Remove user with confirmation + removeCmd := exec.Command(testServerBinary, "user", "remove", + "--dbPath", tmpDB, + "--email", "test@example.com") + + // Pipe "y" to stdin to confirm removal + stdin, err := removeCmd.StdinPipe() + if err != nil { + t.Fatalf("failed to create stdin pipe: %v", err) + } + + // Capture output + stdout, err := removeCmd.StdoutPipe() + if err != nil { + t.Fatalf("failed to create stdout pipe: %v", err) + } + + var stderr bytes.Buffer + removeCmd.Stderr = &stderr + + // Start command + if err := removeCmd.Start(); err != nil { + t.Fatalf("failed to start remove command: %v", err) + } + + // Wait for prompt and send "y" to confirm + if err := assert.RespondToPrompt(stdout, stdin, "Remove user test@example.com?", "y\n", 10*time.Second); err != nil { + t.Fatalf("failed to confirm removal: %v", err) + } + + // Wait for command to finish + if err := removeCmd.Wait(); err != nil { + t.Fatalf("user remove failed: %v\nStderr: %s", err, stderr.String()) + } + + // Verify user was removed + db, err := gorm.Open(sqlite.Open(tmpDB), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open database: %v", err) + } + defer func() { + sqlDB, _ := db.DB() + sqlDB.Close() + }() + + var count int64 + db.Table("users").Count(&count) + assert.Equal(t, count, int64(0), "should have 0 users after removal") +} + +func TestServerUserCreateHelp(t *testing.T) { + cmd := exec.Command(testServerBinary, "user", "create", "--help") + output, err := cmd.CombinedOutput() + + if err != nil { + t.Fatalf("help command failed: %v\nOutput: %s", err, output) + } + + outputStr := string(output) + + // Verify help shows double-dash flags for consistency with CLI + assert.Equal(t, strings.Contains(outputStr, "--email"), true, "help should show --email (double dash)") + assert.Equal(t, strings.Contains(outputStr, "--password"), true, "help should show --password (double dash)") + assert.Equal(t, strings.Contains(outputStr, "--dbPath"), true, "help should show --dbPath (double dash)") +} + +func TestServerUserList(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + // Create two users + exec.Command(testServerBinary, "user", "create", "--dbPath", tmpDB, "--email", "alice@example.com", "--password", "password123").CombinedOutput() + exec.Command(testServerBinary, "user", "create", "--dbPath", tmpDB, "--email", "bob@example.com", "--password", "password123").CombinedOutput() + + // List users + listCmd := exec.Command(testServerBinary, "user", "list", "--dbPath", tmpDB) + output, err := listCmd.CombinedOutput() + + if err != nil { + t.Fatalf("user list failed: %v\nOutput: %s", err, output) + } + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "alice@example.com"), true, "output should have alice") + assert.Equal(t, strings.Contains(outputStr, "bob@example.com"), true, "output should have bob") +} diff --git a/pkg/e2e/sync/basic_test.go b/pkg/e2e/sync/basic_test.go new file mode 100644 index 00000000..d49b0cd9 --- /dev/null +++ b/pkg/e2e/sync/basic_test.go @@ -0,0 +1,3811 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sync + +import ( + "fmt" + "os" + "testing" + + "github.com/dnote/dnote/pkg/assert" + cliDatabase "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/testutils" + clitest "github.com/dnote/dnote/pkg/cli/testutils" + "github.com/dnote/dnote/pkg/server/database" + apitest "github.com/dnote/dnote/pkg/server/testutils" +) + +func TestSync_Empty(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + return map[string]string{} + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + // Test + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 0, + clientLastMaxUSN: 0, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 0, + serverBookCount: 0, + serverUserMaxUSN: 0, + }) + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) +} + +func TestSync_oneway(t *testing.T) { + t.Run("cli to api only", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) { + apitest.MustExec(t, env.ServerDB.Model(&user).Update("max_usn", 0), "updating user max_usn") + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js2") + } + + assert := func(t *testing.T, env testEnv, user database.User) { + cliDB := env.DB + + // test client + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 3, + clientBookCount: 2, + clientLastMaxUSN: 5, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 3, + serverBookCount: 2, + serverUserMaxUSN: 5, + }) + + var cliBookJS, cliBookCSS cliDatabase.Book + var cliNote1JS, cliNote2JS, cliNote1CSS cliDatabase.Note + cliDatabase.MustScan(t, "finding cli book js", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cli book css", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote2JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js2"), &cliNote2JS.UUID, &cliNote2JS.Body, &cliNote2JS.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliNote1CSS.UUID, &cliNote1CSS.Body, &cliNote1CSS.USN) + + // assert on usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + assert.NotEqual(t, cliNote2JS.USN, 0, "cliNote2JS USN mismatch") + assert.NotEqual(t, cliNote1CSS.USN, 0, "cliNote1CSS USN mismatch") + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote2JS.Body, "js2", "cliNote2JS Body mismatch") + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote2JS.Deleted, false, "cliNote2JS Deleted mismatch") + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + + // test server + var apiBookJS, apiBookCSS database.Book + var apiNote1JS, apiNote2JS, apiNote1CSS database.Note + apitest.MustExec(t, env.ServerDB.Model(&database.Note{}).Where("uuid = ?", cliNote1JS.UUID).First(&apiNote1JS), "getting js1 note") + apitest.MustExec(t, env.ServerDB.Model(&database.Note{}).Where("uuid = ?", cliNote2JS.UUID).First(&apiNote2JS), "getting js2 note") + apitest.MustExec(t, env.ServerDB.Model(&database.Note{}).Where("uuid = ?", cliNote1CSS.UUID).First(&apiNote1CSS), "getting css1 note") + apitest.MustExec(t, env.ServerDB.Model(&database.Book{}).Where("uuid = ?", cliBookJS.UUID).First(&apiBookJS), "getting js book") + apitest.MustExec(t, env.ServerDB.Model(&database.Book{}).Where("uuid = ?", cliBookCSS.UUID).First(&apiBookCSS), "getting css book") + + // assert usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote2JS.USN, 0, "apiNote2JS usn mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS usn mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS usn mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS usn mismatch") + // client must have generated uuids + assert.NotEqual(t, apiNote1JS.UUID, "", "apiNote1JS UUID mismatch") + assert.NotEqual(t, apiNote2JS.UUID, "", "apiNote2JS UUID mismatch") + assert.NotEqual(t, apiNote1CSS.UUID, "", "apiNote1CSS UUID mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiNote2JS.Deleted, false, "apiNote2JS Deleted mismatch") + assert.Equal(t, apiNote1CSS.Deleted, false, "apiNote1CSS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + // assert on body and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote2JS.Body, "js2", "apiNote2JS Body mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + } + + t.Run("stepSync", func(t *testing.T) { + + env := setupTestEnv(t) + user := setupUserAndLogin(t, env) + setup(t, env, user) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + assert(t, env, user) + }) + + t.Run("fullSync", func(t *testing.T) { + + env := setupTestEnv(t) + user := setupUserAndLogin(t, env) + setup(t, env, user) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "-f") + + assert(t, env, user) + }) + }) + + t.Run("cli to api with edit and delete", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) { + apiDB := env.ServerDB + apitest.MustExec(t, apiDB.Model(&user).Update("max_usn", 0), "updating user max_usn") + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js2") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js3") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css2") + + var nid, nid2 string + cliDB := env.DB + cliDatabase.MustScan(t, "getting id of note to edit", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "js3"), &nid) + cliDatabase.MustScan(t, "getting id of note to delete", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "css2"), &nid2) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", nid, "-c", "js3-edited") + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveNote, cliBinaryName, "remove", "css", nid2) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css3") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css4") + } + + assert := func(t *testing.T, env testEnv, user database.User) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 6, + clientBookCount: 2, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 6, + serverBookCount: 2, + serverUserMaxUSN: 8, + }) + + // test cli + var cliN1, cliN2, cliN3, cliN4, cliN5, cliN6 cliDatabase.Note + var cliB1, cliB2 cliDatabase.Book + cliDatabase.MustScan(t, "finding cliN1", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliN1.UUID, &cliN1.Body, &cliN1.USN) + cliDatabase.MustScan(t, "finding cliN2", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js2"), &cliN2.UUID, &cliN2.Body, &cliN2.USN) + cliDatabase.MustScan(t, "finding cliN3", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js3-edited"), &cliN3.UUID, &cliN3.Body, &cliN3.USN) + cliDatabase.MustScan(t, "finding cliN4", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliN4.UUID, &cliN4.Body, &cliN4.USN) + cliDatabase.MustScan(t, "finding cliN5", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css3"), &cliN5.UUID, &cliN5.Body, &cliN5.USN) + cliDatabase.MustScan(t, "finding cliN6", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css4"), &cliN6.UUID, &cliN6.Body, &cliN6.USN) + cliDatabase.MustScan(t, "finding cliB1", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliB1.UUID, &cliB1.Label, &cliB1.USN) + cliDatabase.MustScan(t, "finding cliB2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliB2.UUID, &cliB2.Label, &cliB2.USN) + + // assert on usn + assert.NotEqual(t, cliN1.USN, 0, "cliN1 USN mismatch") + assert.NotEqual(t, cliN2.USN, 0, "cliN2 USN mismatch") + assert.NotEqual(t, cliN3.USN, 0, "cliN3 USN mismatch") + assert.NotEqual(t, cliN4.USN, 0, "cliN4 USN mismatch") + assert.NotEqual(t, cliN5.USN, 0, "cliN5 USN mismatch") + assert.NotEqual(t, cliN6.USN, 0, "cliN6 USN mismatch") + assert.NotEqual(t, cliB1.USN, 0, "cliB1 USN mismatch") + assert.NotEqual(t, cliB2.USN, 0, "cliB2 USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliN1.Body, "js1", "cliN1 Body mismatch") + assert.Equal(t, cliN2.Body, "js2", "cliN2 Body mismatch") + assert.Equal(t, cliN3.Body, "js3-edited", "cliN3 Body mismatch") + assert.Equal(t, cliN4.Body, "css1", "cliN4 Body mismatch") + assert.Equal(t, cliN5.Body, "css3", "cliN5 Body mismatch") + assert.Equal(t, cliN6.Body, "css4", "cliN6 Body mismatch") + assert.Equal(t, cliB1.Label, "js", "cliB1 Label mismatch") + assert.Equal(t, cliB2.Label, "css", "cliB2 Label mismatch") + // assert on deleted + assert.Equal(t, cliN1.Deleted, false, "cliN1 Deleted mismatch") + assert.Equal(t, cliN2.Deleted, false, "cliN2 Deleted mismatch") + assert.Equal(t, cliN3.Deleted, false, "cliN3 Deleted mismatch") + assert.Equal(t, cliN4.Deleted, false, "cliN4 Deleted mismatch") + assert.Equal(t, cliN5.Deleted, false, "cliN5 Deleted mismatch") + assert.Equal(t, cliN6.Deleted, false, "cliN6 Deleted mismatch") + assert.Equal(t, cliB1.Deleted, false, "cliB1 Deleted mismatch") + assert.Equal(t, cliB2.Deleted, false, "cliB2 Deleted mismatch") + + // test api + var apiN1, apiN2, apiN3, apiN4, apiN5, apiN6 database.Note + var apiB1, apiB2 database.Book + apitest.MustExec(t, apiDB.Where("uuid = ?", cliN1.UUID).First(&apiN1), "finding apiN1") + apitest.MustExec(t, apiDB.Where("uuid = ?", cliN2.UUID).First(&apiN2), "finding apiN2") + apitest.MustExec(t, apiDB.Where("uuid = ?", cliN3.UUID).First(&apiN3), "finding apiN3") + apitest.MustExec(t, apiDB.Where("uuid = ?", cliN4.UUID).First(&apiN4), "finding apiN4") + apitest.MustExec(t, apiDB.Where("uuid = ?", cliN5.UUID).First(&apiN5), "finding apiN5") + apitest.MustExec(t, apiDB.Where("uuid = ?", cliN6.UUID).First(&apiN6), "finding apiN6") + apitest.MustExec(t, apiDB.Where("uuid = ?", cliB1.UUID).First(&apiB1), "finding apiB1") + apitest.MustExec(t, apiDB.Where("uuid = ?", cliB2.UUID).First(&apiB2), "finding apiB2") + + // assert on usn + assert.NotEqual(t, apiN1.USN, 0, "apiN1 usn mismatch") + assert.NotEqual(t, apiN2.USN, 0, "apiN2 usn mismatch") + assert.NotEqual(t, apiN3.USN, 0, "apiN3 usn mismatch") + assert.NotEqual(t, apiN4.USN, 0, "apiN4 usn mismatch") + assert.NotEqual(t, apiN5.USN, 0, "apiN5 usn mismatch") + assert.NotEqual(t, apiN6.USN, 0, "apiN6 usn mismatch") + assert.NotEqual(t, apiB1.USN, 0, "apiB1 usn mismatch") + assert.NotEqual(t, apiB2.USN, 0, "apiB2 usn mismatch") + // client must have generated uuids + assert.NotEqual(t, apiN1.UUID, "", "apiN1 UUID mismatch") + assert.NotEqual(t, apiN2.UUID, "", "apiN2 UUID mismatch") + assert.NotEqual(t, apiN3.UUID, "", "apiN3 UUID mismatch") + assert.NotEqual(t, apiN4.UUID, "", "apiN4 UUID mismatch") + assert.NotEqual(t, apiN5.UUID, "", "apiN5 UUID mismatch") + assert.NotEqual(t, apiN6.UUID, "", "apiN6 UUID mismatch") + assert.NotEqual(t, apiB1.UUID, "", "apiB1 UUID mismatch") + assert.NotEqual(t, apiB2.UUID, "", "apiB2 UUID mismatch") + // assert on deleted + assert.Equal(t, apiN1.Deleted, false, "apiN1 Deleted mismatch") + assert.Equal(t, apiN2.Deleted, false, "apiN2 Deleted mismatch") + assert.Equal(t, apiN3.Deleted, false, "apiN3 Deleted mismatch") + assert.Equal(t, apiN4.Deleted, false, "apiN4 Deleted mismatch") + assert.Equal(t, apiN5.Deleted, false, "apiN5 Deleted mismatch") + assert.Equal(t, apiN6.Deleted, false, "apiN6 Deleted mismatch") + assert.Equal(t, apiB1.Deleted, false, "apiB1 Deleted mismatch") + assert.Equal(t, apiB2.Deleted, false, "apiB2 Deleted mismatch") + // assert on body and labels + assert.Equal(t, apiN1.Body, "js1", "apiN1 Body mismatch") + assert.Equal(t, apiN2.Body, "js2", "apiN2 Body mismatch") + assert.Equal(t, apiN3.Body, "js3-edited", "apiN3 Body mismatch") + assert.Equal(t, apiN4.Body, "css1", "apiN4 Body mismatch") + assert.Equal(t, apiN5.Body, "css3", "apiN5 Body mismatch") + assert.Equal(t, apiN6.Body, "css4", "apiN6 Body mismatch") + assert.Equal(t, apiB1.Label, "js", "apiB1 Label mismatch") + assert.Equal(t, apiB2.Label, "css", "apiB2 Label mismatch") + } + + t.Run("stepSync", func(t *testing.T) { + + env := setupTestEnv(t) + user := setupUserAndLogin(t, env) + setup(t, env, user) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + assert(t, env, user) + }) + + t.Run("fullSync", func(t *testing.T) { + + env := setupTestEnv(t) + user := setupUserAndLogin(t, env) + setup(t, env, user) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "-f") + + assert(t, env, user) + }) + }) + + t.Run("api to cli", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + apiDB := env.ServerDB + + apitest.MustExec(t, apiDB.Model(&user).Update("max_usn", 0), "updating user max_usn") + + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding css book") + cssNote1UUID := apiCreateNote(t, env, user, cssBookUUID, "css1", "adding css note 1") + jsNote2UUID := apiCreateNote(t, env, user, jsBookUUID, "js2", "adding js note 2") + cssNote2UUID := apiCreateNote(t, env, user, cssBookUUID, "css2", "adding css note 2") + linuxBookUUID := apiCreateBook(t, env, user, "linux", "adding linux book") + linuxNote1UUID := apiCreateNote(t, env, user, linuxBookUUID, "linux1", "adding linux note 1") + apiPatchNote(t, env, user, jsNote2UUID, fmt.Sprintf(`{"book_uuid": "%s"}`, linuxBookUUID), "moving js note 2 to linux") + apiDeleteNote(t, env, user, jsNote1UUID, "deleting js note 1") + cssNote3UUID := apiCreateNote(t, env, user, cssBookUUID, "css3", "adding css note 3") + bashBookUUID := apiCreateBook(t, env, user, "bash", "adding bash book") + bashNote1UUID := apiCreateNote(t, env, user, bashBookUUID, "bash1", "adding bash note 1") + + // delete the linux book and its two notes + apiDeleteBook(t, env, user, linuxBookUUID, "deleting linux book") + + apiPatchNote(t, env, user, cssNote2UUID, fmt.Sprintf(`{"content": "%s"}`, "css2-edited"), "editing css 2 body") + bashNote2UUID := apiCreateNote(t, env, user, bashBookUUID, "bash2", "adding bash note 2") + linuxBook2UUID := apiCreateBook(t, env, user, "linux", "adding new linux book") + linux2Note1UUID := apiCreateNote(t, env, user, linuxBookUUID, "linux-new-1", "adding linux note 1") + apiDeleteBook(t, env, user, jsBookUUID, "deleting js book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + "jsNote2UUID": jsNote2UUID, + "cssBookUUID": cssBookUUID, + "cssNote1UUID": cssNote1UUID, + "cssNote2UUID": cssNote2UUID, + "cssNote3UUID": cssNote3UUID, + "linuxBookUUID": linuxBookUUID, + "linuxNote1UUID": linuxNote1UUID, + "bashBookUUID": bashBookUUID, + "bashNote1UUID": bashNote1UUID, + "bashNote2UUID": bashNote2UUID, + "linuxBook2UUID": linuxBook2UUID, + "linux2Note1UUID": linux2Note1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 6, + clientBookCount: 3, + clientLastMaxUSN: 21, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 9, + serverBookCount: 5, + serverUserMaxUSN: 21, + }) + + // test server + var apiNote1JS, apiNote2JS, apiNote1CSS, apiNote2CSS, apiNote3CSS, apiNote1Bash, apiNote2Bash, apiNote1Linux, apiNote2Linux, apiNote1LinuxDup database.Note + var apiBookJS, apiBookCSS, apiBookBash, apiBookLinux, apiBookLinuxDup database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding api js note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote2UUID"]).First(&apiNote2JS), "finding api js note 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote1UUID"]).First(&apiNote1CSS), "finding api css note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote2UUID"]).First(&apiNote2CSS), "finding api css note 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote3UUID"]).First(&apiNote3CSS), "finding api css note 3") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linuxNote1UUID"]).First(&apiNote1Linux), "finding api linux note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote2UUID"]).First(&apiNote2Linux), "finding api linux note 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["bashNote1UUID"]).First(&apiNote1Bash), "finding api bash note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["bashNote2UUID"]).First(&apiNote2Bash), "finding api bash note 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linux2Note1UUID"]).First(&apiNote1LinuxDup), "finding api linux 2 note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding api js book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding api css book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["bashBookUUID"]).First(&apiBookBash), "finding api bash book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linuxBookUUID"]).First(&apiBookLinux), "finding api linux book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linuxBook2UUID"]).First(&apiBookLinuxDup), "finding api linux book 2") + + // assert on server Label + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote2JS.USN, 0, "apiNote2JS USN mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS USN mismatch") + assert.NotEqual(t, apiNote2CSS.USN, 0, "apiNote2CSS USN mismatch") + assert.NotEqual(t, apiNote3CSS.USN, 0, "apiNote3CSS USN mismatch") + assert.NotEqual(t, apiNote1Linux.USN, 0, "apiNote1Linux USN mismatch") + assert.NotEqual(t, apiNote2Linux.USN, 0, "apiNote2Linux USN mismatch") + assert.NotEqual(t, apiNote1Bash.USN, 0, "apiNote1Bash USN mismatch") + assert.NotEqual(t, apiNote2Bash.USN, 0, "apiNote2Bash USN mismatch") + assert.NotEqual(t, apiNote1LinuxDup.USN, 0, "apiNote1LinuxDup USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apibookJS USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apibookCSS USN mismatch") + assert.NotEqual(t, apiBookBash.USN, 0, "apibookBash USN mismatch") + assert.NotEqual(t, apiBookLinux.USN, 0, "apibookLinux USN mismatch") + assert.NotEqual(t, apiBookLinuxDup.USN, 0, "apiBookLinuxDup USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote2JS.Body, "", "apiNote2JS Body mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS Body mismatch") + assert.Equal(t, apiNote2CSS.Body, "css2-edited", "apiNote2CSS Body mismatch") + assert.Equal(t, apiNote3CSS.Body, "css3", "apiNote3CSS Body mismatch") + assert.Equal(t, apiNote1Linux.Body, "", "apiNote1Linux Body mismatch") + assert.Equal(t, apiNote2Linux.Body, "", "apiNote2Linux Body mismatch") + assert.Equal(t, apiNote1Bash.Body, "bash1", "apiNote1Bash Body mismatch") + assert.Equal(t, apiNote2Bash.Body, "bash2", "apiNote2Bash Body mismatch") + assert.Equal(t, apiNote1LinuxDup.Body, "linux-new-1", "apiNote1LinuxDup Body mismatch") + assert.Equal(t, apiBookJS.Label, "", "apibookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apibookCSS Label mismatch") + assert.Equal(t, apiBookBash.Label, "bash", "apibookBash Label mismatch") + assert.Equal(t, apiBookLinux.Label, "", "apibookLinux Label mismatch") + assert.Equal(t, apiBookLinuxDup.Label, "linux", "apiBookLinuxDup Label mismatch") + // assert on uuids + assert.NotEqual(t, apiNote1JS.UUID, "", "apiNote1JS UUID mismatch") + assert.NotEqual(t, apiNote2JS.UUID, "", "apiNote2JS UUID mismatch") + assert.NotEqual(t, apiNote1CSS.UUID, "", "apiNote1CSS UUID mismatch") + assert.NotEqual(t, apiNote2CSS.UUID, "", "apiNote2CSS UUID mismatch") + assert.NotEqual(t, apiNote3CSS.UUID, "", "apiNote3CSS UUID mismatch") + assert.NotEqual(t, apiNote1Linux.UUID, "", "apiNote1Linux UUID mismatch") + assert.NotEqual(t, apiNote2Linux.UUID, "", "apiNote2Linux UUID mismatch") + assert.NotEqual(t, apiNote1Bash.UUID, "", "apiNote1Bash UUID mismatch") + assert.NotEqual(t, apiNote2Bash.UUID, "", "apiNote2Bash UUID mismatch") + assert.NotEqual(t, apiNote2Bash.UUID, "", "apiNote2Bash UUID mismatch") + assert.NotEqual(t, apiBookJS.UUID, "", "apibookJS UUID mismatch") + assert.NotEqual(t, apiBookCSS.UUID, "", "apibookCSS UUID mismatch") + assert.NotEqual(t, apiBookBash.UUID, "", "apibookBash UUID mismatch") + assert.NotEqual(t, apiBookLinux.UUID, "", "apibookLinux UUID mismatch") + assert.NotEqual(t, apiBookLinuxDup.UUID, "", "apiBookLinuxDup UUID mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiNote2JS.Deleted, true, "apiNote2JS Deleted mismatch") + assert.Equal(t, apiNote1CSS.Deleted, false, "apiNote1CSS Deleted mismatch") + assert.Equal(t, apiNote2CSS.Deleted, false, "apiNote2CSS Deleted mismatch") + assert.Equal(t, apiNote3CSS.Deleted, false, "apiNote3CSS Deleted mismatch") + assert.Equal(t, apiNote1Linux.Deleted, true, "apiNote1Linux Deleted mismatch") + assert.Equal(t, apiNote2Linux.Deleted, true, "apiNote2Linux Deleted mismatch") + assert.Equal(t, apiNote1Bash.Deleted, false, "apiNote1Bash Deleted mismatch") + assert.Equal(t, apiNote2Bash.Deleted, false, "apiNote2Bash Deleted mismatch") + assert.Equal(t, apiNote2Bash.Deleted, false, "apiNote2Bash Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, true, "apibookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apibookCSS Deleted mismatch") + assert.Equal(t, apiBookBash.Deleted, false, "apibookBash Deleted mismatch") + assert.Equal(t, apiBookLinux.Deleted, true, "apibookLinux Deleted mismatch") + assert.Equal(t, apiBookLinuxDup.Deleted, false, "apiBookLinuxDup Deleted mismatch") + + // test client + var cliBookCSS, cliBookBash, cliBookLinux cliDatabase.Book + var cliNote1CSS, cliNote2CSS, cliNote3CSS, cliNote1Bash, cliNote2Bash, cliNote1Linux cliDatabase.Note + cliDatabase.MustScan(t, "finding cli book css", cliDB.QueryRow("SELECT label FROM books WHERE uuid = ?", ids["cssBookUUID"]), &cliBookCSS.Label) + cliDatabase.MustScan(t, "finding cli book bash", cliDB.QueryRow("SELECT label FROM books WHERE uuid = ?", ids["bashBookUUID"]), &cliBookBash.Label) + cliDatabase.MustScan(t, "finding cli book linux2", cliDB.QueryRow("SELECT label FROM books WHERE uuid = ?", ids["linuxBook2UUID"]), &cliBookLinux.Label) + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", apiNote1CSS.UUID), &cliNote1CSS.Body, &cliNote1CSS.USN) + cliDatabase.MustScan(t, "finding cliNote2CSS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", apiNote2CSS.UUID), &cliNote2CSS.Body, &cliNote2CSS.USN) + cliDatabase.MustScan(t, "finding cliNote3CSS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", apiNote3CSS.UUID), &cliNote3CSS.Body, &cliNote3CSS.USN) + cliDatabase.MustScan(t, "finding cliNote1Bash", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", apiNote1Bash.UUID), &cliNote1Bash.Body, &cliNote1Bash.USN) + cliDatabase.MustScan(t, "finding cliNote2Bash", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", apiNote2Bash.UUID), &cliNote2Bash.Body, &cliNote2Bash.USN) + cliDatabase.MustScan(t, "finding cliNote2Bash", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", apiNote1LinuxDup.UUID), &cliNote1Linux.Body, &cliNote1Linux.USN) + + // assert on usn + assert.NotEqual(t, cliNote1CSS.USN, 0, "cliNote1CSS usn mismatch") + assert.NotEqual(t, cliNote2CSS.USN, 0, "cliNote2CSS usn mismatch") + assert.NotEqual(t, cliNote3CSS.USN, 0, "cliNote3CSS usn mismatch") + assert.NotEqual(t, cliNote1Bash.USN, 0, "cliNote1Bash usn mismatch") + assert.NotEqual(t, cliNote2Bash.USN, 0, "cliNote2Bash usn mismatch") + assert.NotEqual(t, cliNote1Linux.USN, 0, "cliNote1Linux usn mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliNote2CSS.Body, "css2-edited", "cliNote2CSS Body mismatch") + assert.Equal(t, cliNote3CSS.Body, "css3", "cliNote3CSS Body mismatch") + assert.Equal(t, cliNote1Bash.Body, "bash1", "cliNote1Bash Body mismatch") + assert.Equal(t, cliNote2Bash.Body, "bash2", "cliNote2Bash Body mismatch") + assert.Equal(t, cliNote1Linux.Body, "linux-new-1", "cliNote1Linux Body mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + assert.Equal(t, cliBookBash.Label, "bash", "cliBookBash Label mismatch") + assert.Equal(t, cliBookLinux.Label, "linux", "cliBookLinux Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliNote2CSS.Deleted, false, "cliNote2CSS Deleted mismatch") + assert.Equal(t, cliNote3CSS.Deleted, false, "cliNote3CSS Deleted mismatch") + assert.Equal(t, cliNote1Bash.Deleted, false, "cliNote1Bash Deleted mismatch") + assert.Equal(t, cliNote2Bash.Deleted, false, "cliNote2Bash Deleted mismatch") + assert.Equal(t, cliNote1Linux.Deleted, false, "cliNote1Linux Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + assert.Equal(t, cliBookBash.Deleted, false, "cliBookBash Deleted mismatch") + assert.Equal(t, cliBookLinux.Deleted, false, "cliBookLinux Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) +} + +func TestSync_twoway(t *testing.T) { + t.Run("once", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding css book") + cssNote1UUID := apiCreateNote(t, env, user, cssBookUUID, "css1", "adding css note 1") + jsNote2UUID := apiCreateNote(t, env, user, jsBookUUID, "js2", "adding js note 2") + cssNote2UUID := apiCreateNote(t, env, user, cssBookUUID, "css2", "adding css note 2") + linuxBookUUID := apiCreateBook(t, env, user, "linux", "adding linux book") + linuxNote1UUID := apiCreateNote(t, env, user, linuxBookUUID, "linux1", "adding linux note 1") + apiPatchNote(t, env, user, jsNote2UUID, fmt.Sprintf(`{"book_uuid": "%s"}`, linuxBookUUID), "moving js note 2 to linux") + apiDeleteNote(t, env, user, jsNote1UUID, "deleting js note 1") + cssNote3UUID := apiCreateNote(t, env, user, cssBookUUID, "css3", "adding css note 3") + bashBookUUID := apiCreateBook(t, env, user, "bash", "adding bash book") + bashNote1UUID := apiCreateNote(t, env, user, bashBookUUID, "bash1", "adding bash note 1") + apiDeleteBook(t, env, user, linuxBookUUID, "deleting linux book") + apiPatchNote(t, env, user, cssNote2UUID, fmt.Sprintf(`{"content": "%s"}`, "css2-edited"), "editing css 2 body") + bashNote2UUID := apiCreateNote(t, env, user, bashBookUUID, "bash2", "adding bash note 2") + linuxBook2UUID := apiCreateBook(t, env, user, "linux", "adding new linux book") + linux2Note1UUID := apiCreateNote(t, env, user, linuxBookUUID, "linux-new-1", "adding linux note 1") + apiDeleteBook(t, env, user, jsBookUUID, "deleting js book") + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js3") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "algorithms", "-c", "algorithms1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js4") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "algorithms", "-c", "algorithms2") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "math", "-c", "math1") + + var nid string + cliDatabase.MustScan(t, "getting id of note to remove", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "js3"), &nid) + + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveBook, cliBinaryName, "remove", "algorithms") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css4") + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveNote, cliBinaryName, "remove", "js", nid) + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + "jsNote2UUID": jsNote2UUID, + "cssBookUUID": cssBookUUID, + "cssNote1UUID": cssNote1UUID, + "cssNote2UUID": cssNote2UUID, + "cssNote3UUID": cssNote3UUID, + "linuxBookUUID": linuxBookUUID, + "linuxNote1UUID": linuxNote1UUID, + "bashBookUUID": bashBookUUID, + "bashNote1UUID": bashNote1UUID, + "bashNote2UUID": bashNote2UUID, + "linuxBook2UUID": linuxBook2UUID, + "linux2Note1UUID": linux2Note1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 9, + clientBookCount: 6, + clientLastMaxUSN: 27, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 12, + serverBookCount: 8, + serverUserMaxUSN: 27, + }) + + // test client + var cliNote1CSS, cliNote2CSS, cliNote3CSS, cliNote1CSS2, cliNote1Bash, cliNote2Bash, cliNote1Linux, cliNote1Math, cliNote1JS cliDatabase.Note + var cliBookCSS, cliBookCSS2, cliBookBash, cliBookLinux, cliBookMath, cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliNote1CSS.UUID, &cliNote1CSS.Body, &cliNote1CSS.USN) + cliDatabase.MustScan(t, "finding cliNote2CSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css2-edited"), &cliNote2CSS.UUID, &cliNote2CSS.Body, &cliNote2CSS.USN) + cliDatabase.MustScan(t, "finding cliNote3CSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css3"), &cliNote3CSS.UUID, &cliNote3CSS.Body, &cliNote3CSS.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS2", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css4"), &cliNote1CSS2.UUID, &cliNote1CSS2.Body, &cliNote1CSS2.USN) + cliDatabase.MustScan(t, "finding cliNote1Bash", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "bash1"), &cliNote1Bash.UUID, &cliNote1Bash.Body, &cliNote1Bash.USN) + cliDatabase.MustScan(t, "finding cliNote2Bash", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "bash2"), &cliNote2Bash.UUID, &cliNote2Bash.Body, &cliNote2Bash.USN) + cliDatabase.MustScan(t, "finding cliNote1Linux", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "linux-new-1"), &cliNote1Linux.UUID, &cliNote1Linux.Body, &cliNote1Linux.USN) + cliDatabase.MustScan(t, "finding cliNote1Math", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "math1"), &cliNote1Math.UUID, &cliNote1Math.Body, &cliNote1Math.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js4"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css_2"), &cliBookCSS2.UUID, &cliBookCSS2.Label, &cliBookCSS2.USN) + cliDatabase.MustScan(t, "finding cliBookBash", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "bash"), &cliBookBash.UUID, &cliBookBash.Label, &cliBookBash.USN) + cliDatabase.MustScan(t, "finding cliBookLinux", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "linux"), &cliBookLinux.UUID, &cliBookLinux.Label, &cliBookLinux.USN) + cliDatabase.MustScan(t, "finding cliBookMath", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "math"), &cliBookMath.UUID, &cliBookMath.Label, &cliBookMath.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1CSS.USN, 0, "cliNote1CSS USN mismatch") + assert.NotEqual(t, cliNote2CSS.USN, 0, "cliNote2CSS USN mismatch") + assert.NotEqual(t, cliNote3CSS.USN, 0, "cliNote3CSS USN mismatch") + assert.NotEqual(t, cliNote1CSS2.USN, 0, "cliNote1CSS2 USN mismatch") + assert.NotEqual(t, cliNote1Bash.USN, 0, "cliNote1Bash USN mismatch") + assert.NotEqual(t, cliNote2Bash.USN, 0, "cliNote2Bash USN mismatch") + assert.NotEqual(t, cliNote1Linux.USN, 0, "cliNote1Linux USN mismatch") + assert.NotEqual(t, cliNote1Math.USN, 0, "cliNote1Math USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + assert.NotEqual(t, cliBookCSS2.USN, 0, "cliBookCSS2 USN mismatch") + assert.NotEqual(t, cliBookBash.USN, 0, "cliBookBash USN mismatch") + assert.NotEqual(t, cliBookMath.USN, 0, "cliBookMath USN mismatch") + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliNote2CSS.Body, "css2-edited", "cliNote2CSS Body mismatch") + assert.Equal(t, cliNote3CSS.Body, "css3", "cliNote3CSS Body mismatch") + assert.Equal(t, cliNote1CSS2.Body, "css4", "cliNote1CSS2 Body mismatch") + assert.Equal(t, cliNote1Bash.Body, "bash1", "cliNote1Bash Body mismatch") + assert.Equal(t, cliNote2Bash.Body, "bash2", "cliNote2Bash Body mismatch") + assert.Equal(t, cliNote1Linux.Body, "linux-new-1", "cliNote1Linux Body mismatch") + assert.Equal(t, cliNote1Math.Body, "math1", "cliNote1Math Body mismatch") + assert.Equal(t, cliNote1JS.Body, "js4", "cliNote1JS Body mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + assert.Equal(t, cliBookCSS2.Label, "css_2", "cliBookCSS2 Label mismatch") + assert.Equal(t, cliBookBash.Label, "bash", "cliBookBash Label mismatch") + assert.Equal(t, cliBookMath.Label, "math", "cliBookMath Label mismatch") + assert.Equal(t, cliBookLinux.Label, "linux", "cliBookLinux Label mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliNote2CSS.Deleted, false, "cliNote2CSS Deleted mismatch") + assert.Equal(t, cliNote3CSS.Deleted, false, "cliNote3CSS Deleted mismatch") + assert.Equal(t, cliNote1CSS2.Deleted, false, "cliNote1CSS2 Deleted mismatch") + assert.Equal(t, cliNote1Bash.Deleted, false, "cliNote1Bash Deleted mismatch") + assert.Equal(t, cliNote2Bash.Deleted, false, "cliNote2Bash Deleted mismatch") + assert.Equal(t, cliNote1Linux.Deleted, false, "cliNote1Linux Deleted mismatch") + assert.Equal(t, cliNote1Math.Deleted, false, "cliNote1Math Deleted mismatch") + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + assert.Equal(t, cliBookCSS2.Deleted, false, "cliBookCSS2 Deleted mismatch") + assert.Equal(t, cliBookBash.Deleted, false, "cliBookBash Deleted mismatch") + assert.Equal(t, cliBookMath.Deleted, false, "cliBookMath Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + + // test server + var apiNote1JS, apiNote1CSS, apiNote2CSS, apiNote3CSS, apiNote1Linux, apiNote2Linux, apiNote1Bash, apiNote2Bash, apiNote1LinuxDup, apiNote1CSS2, apiNote1Math, apiNote1JS2 database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding api js note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote1UUID"]).First(&apiNote1CSS), "finding api css note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote2UUID"]).First(&apiNote2CSS), "finding api css note 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote3UUID"]).First(&apiNote3CSS), "finding api css note 3") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linuxNote1UUID"]).First(&apiNote1Linux), "finding api linux note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote2UUID"]).First(&apiNote2Linux), "finding api linux note 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["bashNote1UUID"]).First(&apiNote1Bash), "finding api bash note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["bashNote2UUID"]).First(&apiNote2Bash), "finding api bash note 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linux2Note1UUID"]).First(&apiNote1LinuxDup), "finding api linux 2 note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1CSS2.UUID).First(&apiNote1CSS2), "finding apiNote1CSS2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1Math.UUID).First(&apiNote1Math), "finding apiNote1Math") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1JS.UUID).First(&apiNote1JS2), "finding apiNote1JS2") + var apiBookJS, apiBookCSS, apiBookLinux, apiBookBash, apiBookLinuxDup, apiBookCSS2, apiBookMath, apiBookJS2 database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding api js book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding api css book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["bashBookUUID"]).First(&apiBookBash), "finding api bash book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linuxBookUUID"]).First(&apiBookLinux), "finding api linux book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linuxBook2UUID"]).First(&apiBookLinuxDup), "finding api linux book 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookCSS2.UUID).First(&apiBookCSS2), "finding apiBookCSS2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookMath.UUID).First(&apiBookMath), "finding apiBookMath") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookJS.UUID).First(&apiBookJS2), "finding apiBookJS2") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS usn mismatch") + assert.NotEqual(t, apiNote2CSS.USN, 0, "apiNote2CSS usn mismatch") + assert.NotEqual(t, apiNote3CSS.USN, 0, "apiNote3CSS usn mismatch") + assert.NotEqual(t, apiNote1Linux.USN, 0, "apiNote1Linux usn mismatch") + assert.NotEqual(t, apiNote2Linux.USN, 0, "apiNote2Linux usn mismatch") + assert.NotEqual(t, apiNote1Bash.USN, 0, "apiNote1Bash usn mismatch") + assert.NotEqual(t, apiNote2Bash.USN, 0, "apiNote2Bash usn mismatch") + assert.NotEqual(t, apiNote1LinuxDup.USN, 0, "apiNote1LinuxDup usn mismatch") + assert.NotEqual(t, apiNote1CSS2.USN, 0, "apiNoteCSS2 usn mismatch") + assert.NotEqual(t, apiNote1Math.USN, 0, "apiNote1Math usn mismatch") + assert.NotEqual(t, apiNote1JS2.USN, 0, "apiNote1JS2 usn mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS usn mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS usn mismatch") + assert.NotEqual(t, apiBookLinux.USN, 0, "apiBookLinux usn mismatch") + assert.NotEqual(t, apiBookBash.USN, 0, "apiBookBash usn mismatch") + assert.NotEqual(t, apiBookLinuxDup.USN, 0, "apiBookLinuxDup usn mismatch") + assert.NotEqual(t, apiBookCSS2.USN, 0, "apiBookCSS2 usn mismatch") + assert.NotEqual(t, apiBookMath.USN, 0, "apiBookMath usn mismatch") + assert.NotEqual(t, apiBookJS2.USN, 0, "apiBookJS2 usn mismatch") + // assert on note bodys + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS Body mismatch") + assert.Equal(t, apiNote2CSS.Body, "css2-edited", "apiNote2CSS Body mismatch") + assert.Equal(t, apiNote3CSS.Body, "css3", "apiNote3CSS Body mismatch") + assert.Equal(t, apiNote1Linux.Body, "", "apiNote1Linux Body mismatch") + assert.Equal(t, apiNote2Linux.Body, "", "apiNote2Linux Body mismatch") + assert.Equal(t, apiNote1Bash.Body, "bash1", "apiNote1Bash Body mismatch") + assert.Equal(t, apiNote2Bash.Body, "bash2", "apiNote2Bash Body mismatch") + assert.Equal(t, apiNote1LinuxDup.Body, "linux-new-1", "apiNote1LinuxDup Body mismatch") + assert.Equal(t, apiNote1CSS2.Body, "css4", "apiNote1CSS2 Body mismatch") + assert.Equal(t, apiNote1Math.Body, "math1", "apiNote1Math Body mismatch") + assert.Equal(t, apiNote1JS2.Body, "js4", "apiNote1JS2 Body mismatch") + // client must have generated uuids + assert.NotEqual(t, apiNote1CSS2.UUID, "", "apiNote1CSS2 uuid mismatch") + assert.NotEqual(t, apiNote1Math.UUID, "", "apiNote1Math uuid mismatch") + assert.NotEqual(t, apiNote1JS2.UUID, "", "apiNote1JS2 uuid mismatch") + // assert on labels + assert.Equal(t, apiBookJS.Label, "", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + assert.Equal(t, apiBookLinux.Label, "", "apiBookLinux Label mismatch") + assert.Equal(t, apiBookBash.Label, "bash", "apiBookBash Label mismatch") + assert.Equal(t, apiBookLinuxDup.Label, "linux", "apiBookLinuxDup Label mismatch") + assert.Equal(t, apiBookCSS2.Label, "css_2", "apiBookCSS2 Label mismatch") + assert.Equal(t, apiBookMath.Label, "math", "apiBookMath Label mismatch") + assert.Equal(t, apiBookJS2.Label, "js", "apiBookJS2 Label mismatch") + // assert on note deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiNote1CSS.Deleted, false, "apiNote1CSS Deleted mismatch") + assert.Equal(t, apiNote2CSS.Deleted, false, "apiNote2CSS Deleted mismatch") + assert.Equal(t, apiNote3CSS.Deleted, false, "apiNote3CSS Deleted mismatch") + assert.Equal(t, apiNote1Linux.Deleted, true, "apiNote1Linux Deleted mismatch") + assert.Equal(t, apiNote2Linux.Deleted, true, "apiNote2Linux Deleted mismatch") + assert.Equal(t, apiNote1Bash.Deleted, false, "apiNote1Bash Deleted mismatch") + assert.Equal(t, apiNote2Bash.Deleted, false, "apiNote2Bash Deleted mismatch") + assert.Equal(t, apiNote1LinuxDup.Deleted, false, "apiNote1LinuxDup Deleted mismatch") + assert.Equal(t, apiNote1CSS2.Deleted, false, "apiNote1CSS2 Deleted mismatch") + assert.Equal(t, apiNote1Math.Deleted, false, "apiNote1Math Deleted mismatch") + assert.Equal(t, apiNote1JS2.Deleted, false, "apiNote1JS2 Deleted mismatch") + // assert on book deleted + assert.Equal(t, apiBookJS.Deleted, true, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + assert.Equal(t, apiBookLinux.Deleted, true, "apiBookLinux Deleted mismatch") + assert.Equal(t, apiBookBash.Deleted, false, "apiBookBash Deleted mismatch") + assert.Equal(t, apiBookLinuxDup.Deleted, false, "apiBookLinuxDup Deleted mismatch") + assert.Equal(t, apiBookCSS2.Deleted, false, "apiBookCSS2 Deleted mismatch") + assert.Equal(t, apiBookMath.Deleted, false, "apiBookMath Deleted mismatch") + assert.Equal(t, apiBookJS2.Deleted, false, "apiBookJS2 Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("twice", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding css book") + cssNote1UUID := apiCreateNote(t, env, user, cssBookUUID, "css1", "adding css note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js2") + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveBook, cliBinaryName, "remove", "js") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "math", "-c", "math1") + + var nid string + cliDB := env.DB + cliDatabase.MustScan(t, "getting id of note to edit", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "math1"), &nid) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "math", nid, "-c", "math1-edited") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + scssBookUUID := apiCreateBook(t, env, user, "scss", "adding a scss book") + apiPatchNote(t, env, user, cssNote1UUID, fmt.Sprintf(`{"book_uuid": "%s"}`, scssBookUUID), "moving css note 1 to scss") + + var n1UUID string + cliDatabase.MustScan(t, "getting math1-edited note UUID", cliDB.QueryRow("SELECT uuid FROM notes WHERE body = ?", "math1-edited"), &n1UUID) + apiPatchNote(t, env, user, n1UUID, fmt.Sprintf(`{"content": "%s", "public": true}`, "math1-edited"), "editing math1 note") + + cssNote2UUID := apiCreateNote(t, env, user, cssBookUUID, "css2", "adding css note 2") + apiDeleteBook(t, env, user, cssBookUUID, "deleting css book") + + bashBookUUID := apiCreateBook(t, env, user, "bash", "adding a bash book") + algorithmsBookUUID := apiCreateBook(t, env, user, "algorithms", "adding a algorithms book") + + // 4. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js3") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "algorithms", "-c", "algorithms1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + "cssBookUUID": cssBookUUID, + "scssBookUUID": scssBookUUID, + "cssNote1UUID": cssNote1UUID, + "cssNote2UUID": cssNote2UUID, + "bashBookUUID": bashBookUUID, + "algorithmsBookUUID": algorithmsBookUUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + apiDB := env.ServerDB + cliDB := env.DB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 5, + clientBookCount: 6, + clientLastMaxUSN: 17, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 6, + serverBookCount: 7, + serverUserMaxUSN: 17, + }) + + // test client + var cliNote1JS, cliNote2JS, cliNote1SCSS, cliNote1Math, cliNote1Alg2 cliDatabase.Note + var cliBookJS, cliBookSCSS, cliBookMath, cliBookBash, cliBookAlg, cliBookAlg2 cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote2JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js3"), &cliNote2JS.UUID, &cliNote2JS.Body, &cliNote2JS.USN) + cliDatabase.MustScan(t, "finding cliNote1SCSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliNote1SCSS.UUID, &cliNote1SCSS.Body, &cliNote1SCSS.USN) + cliDatabase.MustScan(t, "finding cliNote1Math", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "math1-edited"), &cliNote1Math.UUID, &cliNote1Math.Body, &cliNote1Math.USN) + cliDatabase.MustScan(t, "finding cliNote1Alg2", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "algorithms1"), &cliNote1Alg2.UUID, &cliNote1Alg2.Body, &cliNote1Alg2.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookSCSS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "scss"), &cliBookSCSS.UUID, &cliBookSCSS.Label, &cliBookSCSS.USN) + cliDatabase.MustScan(t, "finding cliBookMath", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "math"), &cliBookMath.UUID, &cliBookMath.Label, &cliBookMath.USN) + cliDatabase.MustScan(t, "finding cliBookBash", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "bash"), &cliBookBash.UUID, &cliBookBash.Label, &cliBookBash.USN) + cliDatabase.MustScan(t, "finding cliBookAlg", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "algorithms"), &cliBookAlg.UUID, &cliBookAlg.Label, &cliBookAlg.USN) + cliDatabase.MustScan(t, "finding cliBookAlg2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "algorithms_2"), &cliBookAlg2.UUID, &cliBookAlg2.Label, &cliBookAlg2.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + assert.NotEqual(t, cliNote2JS.USN, 0, "cliNote2JS USN mismatch") + assert.NotEqual(t, cliNote1SCSS.USN, 0, "cliNote1SCSS USN mismatch") + assert.NotEqual(t, cliNote1Math.USN, 0, "cliNote1Math USN mismatch") + assert.NotEqual(t, cliNote1Alg2.USN, 0, "cliNote1Alg2 USN mismatch") + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookSCSS.USN, 0, "cliBookSCSS USN mismatch") + assert.NotEqual(t, cliBookMath.USN, 0, "cliBookMath USN mismatch") + assert.NotEqual(t, cliBookBash.USN, 0, "cliBookBash USN mismatch") + assert.NotEqual(t, cliBookAlg.USN, 0, "cliBookAlg USN mismatch") + assert.NotEqual(t, cliBookAlg2.USN, 0, "cliBookAlg2 USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote2JS.Body, "js3", "cliNote2JS Body mismatch") + assert.Equal(t, cliNote1SCSS.Body, "css1", "cliNote1SCSS Body mismatch") + assert.Equal(t, cliNote1Math.Body, "math1-edited", "cliNote1Math Body mismatch") + assert.Equal(t, cliNote1Alg2.Body, "algorithms1", "cliNote1Alg2 Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookSCSS.Label, "scss", "cliBookSCSS Label mismatch") + assert.Equal(t, cliBookMath.Label, "math", "cliBookMath Label mismatch") + assert.Equal(t, cliBookBash.Label, "bash", "cliBookBash Label mismatch") + assert.Equal(t, cliBookAlg.Label, "algorithms", "cliBookAlg Label mismatch") + assert.Equal(t, cliBookAlg2.Label, "algorithms_2", "cliBookAlg2 Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote2JS.Deleted, false, "cliNote2JS Deleted mismatch") + assert.Equal(t, cliNote1SCSS.Deleted, false, "cliNote1SCSS Deleted mismatch") + assert.Equal(t, cliNote1Math.Deleted, false, "cliNote1Math Deleted mismatch") + assert.Equal(t, cliNote1Alg2.Deleted, false, "cliNote1Alg2 Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookSCSS.Deleted, false, "cliBookSCSS Deleted mismatch") + assert.Equal(t, cliBookMath.Deleted, false, "cliBookMath Deleted mismatch") + assert.Equal(t, cliBookBash.Deleted, false, "cliBookBash Deleted mismatch") + assert.Equal(t, cliBookAlg.Deleted, false, "cliBookAlg Deleted mismatch") + assert.Equal(t, cliBookAlg2.Deleted, false, "cliBookAlg2 Deleted mismatch") + + // test server + var apiNote1JS, apiNote2JS, apiNote1SCSS, apiNote2CSS, apiNote1Math, apiNote1Alg database.Note + var apiBookJS, apiBookCSS, apiBookSCSS, apiBookMath, apiBookBash, apiBookAlg, apiBookAlg2 database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote2UUID"]).First(&apiNote2CSS), "finding apiNote2CSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote2JS.UUID).First(&apiNote2JS), "finding apiNote2JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote1UUID"]).First(&apiNote1SCSS), "finding apiNote1SCSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1Math.UUID).First(&apiNote1Math), "finding apiNote1Math") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1Alg2.UUID).First(&apiNote1Alg), "finding apiNote1Alg") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["bashBookUUID"]).First(&apiBookBash), "finding apiBookBash") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["scssBookUUID"]).First(&apiBookSCSS), "finding apiBookSCSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["algorithmsBookUUID"]).First(&apiBookAlg), "finding apiBookAlg") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookAlg2.UUID).First(&apiBookAlg2), "finding apiBookAlg2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookMath.UUID).First(&apiBookMath), "finding apiBookMath") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote2JS.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote1SCSS.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote2CSS.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote1Math.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote1Alg.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBook1Alg usn mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS usn mismatch") + assert.NotEqual(t, apiBookSCSS.USN, 0, "apibookSCSS usn mismatch") + assert.NotEqual(t, apiBookMath.USN, 0, "apiBookMath usn mismatch") + assert.NotEqual(t, apiBookBash.USN, 0, "apiBookBash usn mismatch") + assert.NotEqual(t, apiBookAlg.USN, 0, "apiBookAlg usn mismatch") + assert.NotEqual(t, apiBookAlg2.USN, 0, "apiBookAlg2 usn mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote2JS.Body, "js3", "apiNote2JS Body mismatch") + assert.Equal(t, apiNote1SCSS.Body, "css1", "apiNote1SCSS Body mismatch") + assert.Equal(t, apiNote2CSS.Body, "", "apiNote2CSS Body mismatch") + assert.Equal(t, apiNote1Math.Body, "math1-edited", "apiNote1Math Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "", "apiBookCSS Label mismatch") + assert.Equal(t, apiBookSCSS.Label, "scss", "apiBookSCSS Label mismatch") + assert.Equal(t, apiBookMath.Label, "math", "apiBookMath Label mismatch") + assert.Equal(t, apiBookBash.Label, "bash", "apiBookBash Label mismatch") + assert.Equal(t, apiBookAlg.Label, "algorithms", "apiBookAlg Label mismatch") + assert.Equal(t, apiBookAlg2.Label, "algorithms_2", "apiBookAlg2 Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiNote2JS.Deleted, false, "apiNote2JS Deleted mismatch") + assert.Equal(t, apiNote1SCSS.Deleted, false, "apiNote1SCSS Deleted mismatch") + assert.Equal(t, apiNote2CSS.Deleted, true, "apiNote2CSS Deleted mismatch") + assert.Equal(t, apiNote1Math.Deleted, false, "apiNote1Math Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, true, "apiBookCSS Deleted mismatch") + assert.Equal(t, apiBookSCSS.Deleted, false, "apiBookSCSS Deleted mismatch") + assert.Equal(t, apiBookMath.Deleted, false, "apiBookMath Deleted mismatch") + assert.Equal(t, apiBookBash.Deleted, false, "apiBookBash Deleted mismatch") + assert.Equal(t, apiBookAlg.Deleted, false, "apiBookAlg Deleted mismatch") + assert.Equal(t, apiBookAlg2.Deleted, false, "apiBookAlg2 Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("three times", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + goBookUUID := apiCreateBook(t, env, user, "go", "adding a go book") + goNote1UUID := apiCreateNote(t, env, user, goBookUUID, "go1", "adding go note 1") + + // 4. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "html", "-c", "html1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + "goBookUUID": goBookUUID, + "goNote1UUID": goNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 4, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 4, + serverUserMaxUSN: 8, + }) + + // test client + var cliNote1JS, cliNote1CSS, cliNote1Go, cliNote1HTML cliDatabase.Note + var cliBookJS, cliBookCSS, cliBookGo, cliBookHTML cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliNote1CSS.UUID, &cliNote1CSS.Body, &cliNote1CSS.USN) + cliDatabase.MustScan(t, "finding cliNote1Go", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "go1"), &cliNote1Go.UUID, &cliNote1Go.Body, &cliNote1Go.USN) + cliDatabase.MustScan(t, "finding cliNote1HTML", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "html1"), &cliNote1HTML.UUID, &cliNote1HTML.Body, &cliNote1HTML.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliBookGo", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "go"), &cliBookGo.UUID, &cliBookGo.Label, &cliBookGo.USN) + cliDatabase.MustScan(t, "finding cliBookHTML", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "html"), &cliBookHTML.UUID, &cliBookHTML.Label, &cliBookHTML.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + assert.NotEqual(t, cliNote1CSS.USN, 0, "cliNote1CSS USN mismatch") + assert.NotEqual(t, cliNote1Go.USN, 0, "cliNote1Go USN mismatch") + assert.NotEqual(t, cliNote1HTML.USN, 0, "cliNote1HTML USN mismatch") + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + assert.NotEqual(t, cliBookGo.USN, 0, "cliBookGo USN mismatch") + assert.NotEqual(t, cliBookHTML.USN, 0, "cliBookHTML USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliNote1Go.Body, "go1", "cliNote1Go Body mismatch") + assert.Equal(t, cliNote1HTML.Body, "html1", "cliNote1HTML Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + assert.Equal(t, cliBookGo.Label, "go", "cliBookGo Label mismatch") + assert.Equal(t, cliBookHTML.Label, "html", "cliBookHTML Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliNote1Go.Deleted, false, "cliNote1Go Deleted mismatch") + assert.Equal(t, cliNote1HTML.Deleted, false, "cliNote1HTML Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + assert.Equal(t, cliBookGo.Deleted, false, "cliBookGo Deleted mismatch") + assert.Equal(t, cliBookHTML.Deleted, false, "cliBookHTML Deleted mismatch") + + // test server + var apiNote1JS, apiNote1CSS, apiNote1Go, apiNote1HTML database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["goNote1UUID"]).First(&apiNote1Go), "finding apiNote1Go") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1CSS.UUID).First(&apiNote1CSS), "finding apiNote1CSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1HTML.UUID).First(&apiNote1HTML), "finding apiNote1HTML") + var apiBookJS, apiBookCSS, apiBookGo, apiBookHTML database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["goBookUUID"]).First(&apiBookGo), "finding apiBookGo") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookCSS.UUID).First(&apiBookCSS), "finding apiBookCSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookHTML.UUID).First(&apiBookHTML), "finding apiBookHTML") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS USN mismatch") + assert.NotEqual(t, apiNote1Go.USN, 0, "apiNote1Go USN mismatch") + assert.NotEqual(t, apiNote1HTML.USN, 0, "apiNote1HTM USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookGo.USN, 0, "apiBookGo USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS USN mismatch") + assert.NotEqual(t, apiBookHTML.USN, 0, "apiBookHTML USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS Body mismatch") + assert.Equal(t, apiNote1Go.Body, "go1", "apiNote1Go Body mismatch") + assert.Equal(t, apiNote1HTML.Body, "html1", "apiNote1HTM Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookGo.Label, "go", "apiBookGo Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + assert.Equal(t, apiBookHTML.Label, "html", "apiBookHTML Label mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) +} + +func TestSync(t *testing.T) { + t.Run("client adds a book and a note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + + return map[string]string{} + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 2, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 2, + }) + + // test client + // assert on bodys and labels + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1JS.UUID).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookJS.UUID).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client deletes a book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveBook, cliBinaryName, "remove", "js") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 0, + clientLastMaxUSN: 5, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 5, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, true, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client deletes a note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + var nid string + cliDatabase.MustScan(t, "getting id of note to remove", cliDB.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", jsNote1UUID), &nid) + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveNote, cliBinaryName, "remove", "js", nid) + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE label = ?", "js"), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client edits a note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + var nid string + cliDatabase.MustScan(t, "getting id of note to edit", cliDB.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", jsNote1UUID), &nid) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", nid, "-c", "js1-edited") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1-edited", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1-edited", "cliNote1JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client edits a book by renaming it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", "-n", "js-edited") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test client + var cliBookJS cliDatabase.Book + var cliNote1JS cliDatabase.Note + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, book_uuid, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.BookUUID, &cliNote1JS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookJS.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js-edited", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js-edited", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server adds a book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 1, + clientLastMaxUSN: 1, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 0, + serverBookCount: 1, + serverUserMaxUSN: 1, + }) + + // test server + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + // assert on bodys and labels + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE label = ?", "js"), &cliBookJS.Label, &cliBookJS.USN) + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server edits a book by renaming it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiPatchBook(t, env, user, jsBookUUID, fmt.Sprintf(`{"name": "%s"}`, "js-new-label"), "editing js book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 1, + clientLastMaxUSN: 2, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 0, + serverBookCount: 1, + serverUserMaxUSN: 2, + }) + + // test server + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiBookJS.Label, "js-new-label", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + // assert on bodys and labels + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + assert.Equal(t, cliBookJS.Label, "js-new-label", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server deletes a book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiDeleteBook(t, env, user, jsBookUUID, "deleting js book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 0, + clientLastMaxUSN: 2, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 0, + serverBookCount: 1, + serverUserMaxUSN: 2, + }) + + // test server + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiBookJS.Label, "", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiBookJS.Deleted, true, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server adds a note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 2, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 2, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server edits a note body", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"content": "%s"}`, "js1-edited"), "editing js note 1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1-edited", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1-edited", "cliNote1JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server moves a note to another book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding css book") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"book_uuid": "%s"}`, cssBookUUID), "moving js note 1 to css book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + "cssBookUUID": cssBookUUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS, apiBookCSS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["cssBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS, cliBookCSS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["cssBookUUID"]), &cliBookCSS.Label, &cliBookCSS.USN) + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server deletes a note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiDeleteNote(t, env, user, jsNote1UUID, "deleting js note 1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + // assert on bodys and labels + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client and server deletes the same book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiDeleteBook(t, env, user, jsBookUUID, "deleting js book") + + // 4. on cli + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveBook, cliBinaryName, "remove", "js") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 0, + clientLastMaxUSN: 6, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 6, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, true, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client and server deletes the same note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiDeleteNote(t, env, user, jsNote1UUID, "deleting js note 1") + + // 4. on cli + var nid string + cliDatabase.MustScan(t, "getting id of note to remove", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "js1"), &nid) + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveNote, cliBinaryName, "remove", "js", nid) + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + apiDB := env.ServerDB + cliDB := env.DB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 1, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 4, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server and client adds a note with same body", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 4. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test client + var cliNote1JS, cliNote2JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote2JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ? and uuid != ?", "js1", ids["jsNote1UUID"]), &cliNote2JS.UUID, &cliNote2JS.Body, &cliNote2JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote2JS.Body, "js1", "cliNote2JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote2JS.Deleted, false, "cliNote2JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + + // test server + var apiNote1JS, apiNote2JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote2JS.UUID).First(&apiNote2JS), "finding apiNote2JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote2JS.USN, 0, "apiNote2JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote2JS.Body, "js1", "apiNote2JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server and client adds a book with same label", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // test client + var cliNote1JS, cliNote1JS2 cliDatabase.Note + var cliBookJS, cliBookJS2 cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS2", + cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ? AND uuid !=?", "js1", ids["jsNote1UUID"]), &cliNote1JS2.UUID, &cliNote1JS2.Body, &cliNote1JS2.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookJS2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js_2"), &cliBookJS2.UUID, &cliBookJS2.Label, &cliBookJS2.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS2.Body, "js1", "cliNote1JS2 Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookJS2.Label, "js_2", "cliBookJS2 Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote1JS2.Deleted, false, "cliNote1JS2 Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookJS2.Deleted, false, "cliBookJS2 Deleted mismatch") + + // test server + var apiNote1JS, apiNote1JS2 database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1JS2.UUID).First(&apiNote1JS2), "finding apiNote1JS2") + var apiBookJS, apiBookJS2 database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookJS2.UUID).First(&apiBookJS2), "finding apiBookJS2") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote1JS2.USN, 0, "apiNote1JS2 USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookJS2.USN, 0, "apiBookJS2 USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS2.Body, "js1", "apiNote1JS2 Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookJS2.Label, "js_2", "apiBookJS2 Label mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server and client adds two sets of books with same labels", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding css book") + cssNote1UUID := apiCreateNote(t, env, user, cssBookUUID, "css1", "adding css note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + "cssBookUUID": cssBookUUID, + "cssNote1UUID": cssNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 4, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 4, + serverUserMaxUSN: 8, + }) + + // test client + var cliNote1JS, cliNote1JS2, cliNote1CSS, cliNote1CSS2 cliDatabase.Note + var cliBookJS, cliBookJS2, cliBookCSS, cliBookCSS2 cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS2", + cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ? AND uuid != ?", "js1", ids["jsNote1UUID"]), &cliNote1JS2.UUID, &cliNote1JS2.Body, &cliNote1JS2.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE uuid = ?", ids["cssNote1UUID"]), &cliNote1CSS.UUID, &cliNote1CSS.Body, &cliNote1CSS.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS2", + cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ? AND uuid != ?", "css1", ids["cssNote1UUID"]), &cliNote1CSS2.UUID, &cliNote1CSS2.Body, &cliNote1CSS2.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookJS2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js_2"), &cliBookJS2.UUID, &cliBookJS2.Label, &cliBookJS2.USN) + cliDatabase.MustScan(t, "finding cliBookCSS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css_2"), &cliBookCSS2.UUID, &cliBookCSS2.Label, &cliBookCSS2.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS2.Body, "js1", "cliNote1JS2 Body mismatch") + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliNote1CSS2.Body, "css1", "cliNote1CSS2 Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookJS2.Label, "js_2", "cliBookJS2 Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + assert.Equal(t, cliBookCSS2.Label, "css_2", "cliBookCSS2 Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote1JS2.Deleted, false, "cliNote1JS2 Deleted mismatch") + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliNote1CSS2.Deleted, false, "cliNote1CSS2 Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookJS2.Deleted, false, "cliBookJS2 Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + assert.Equal(t, cliBookCSS2.Deleted, false, "cliBookCSS2 Deleted mismatch") + + // test server + var apiNote1JS, apiNote1JS2, apiNote1CSS, apiNote1CSS2 database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1JS2.UUID).First(&apiNote1JS2), "finding apiNote1JS2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote1UUID"]).First(&apiNote1CSS), "finding apiNote1CSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1CSS2.UUID).First(&apiNote1CSS2), "finding apiNote1CSS2") + var apiBookJS, apiBookJS2, apiBookCSS, apiBookCSS2 database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookJS2.UUID).First(&apiBookJS2), "finding apiBookJS2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookCSS2.UUID).First(&apiBookCSS2), "finding apiBookCSS2") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote1JS2.USN, 0, "apiNote1JS2 USN mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS USN mismatch") + assert.NotEqual(t, apiNote1CSS2.USN, 0, "apiNote1CSS2 USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookJS2.USN, 0, "apiBookJS2 USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS USN mismatch") + assert.NotEqual(t, apiBookCSS2.USN, 0, "apiBookCSS2 USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS2.Body, "js1", "apiNote1JS2 Body mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS2 Body mismatch") + assert.Equal(t, apiNote1CSS2.Body, "css1", "apiNote1CSS2 Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookJS2.Label, "js_2", "apiBookJS2 Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + assert.Equal(t, apiBookCSS2.Label, "css_2", "apiBookCSS2 Label mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server and client adds notes to the same book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 4. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js2") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test client + var cliNote1JS, cliNote2JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote2JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js2"), &cliNote2JS.UUID, &cliNote2JS.Body, &cliNote2JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote2JS.Body, "js2", "cliNote2JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote2JS.Deleted, false, "cliNote2JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + + // test server + var apiNote1JS, apiNote2JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote2JS.UUID).First(&apiNote2JS), "finding apiNote2JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote2JS.USN, 0, "apiNote2JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote2JS.Body, "js2", "apiNote2JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server and client adds a book with the same label and notes in it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js2") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // test client + var cliNote1JS, cliNote2JS cliDatabase.Note + var cliBookJS, cliBookJS2 cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote2JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js2"), &cliNote2JS.UUID, &cliNote2JS.Body, &cliNote2JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookJS2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js_2"), &cliBookJS2.UUID, &cliBookJS2.Label, &cliBookJS2.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote2JS.Body, "js2", "cliNote2JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookJS2.Label, "js_2", "cliBookJS2 Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote2JS.Deleted, false, "cliNote2JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookJS2.Deleted, false, "cliBookJS2 Deleted mismatch") + + // test server + var apiNote1JS, apiNote2JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote2JS.UUID).First(&apiNote2JS), "finding apiNote2JS") + var apiBookJS, apiBookJS2 database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookJS2.UUID).First(&apiBookJS2), "finding apiBookJS2") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote2JS.USN, 0, "apiNote2JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookJS2.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote2JS.Body, "js2", "apiNote2JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookJS2.Label, "js_2", "apiBookJS2 USN mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client and server edits bodys of the same note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + var nid string + cliDatabase.MustScan(t, "getting id of note to edit", cliDB.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", jsNote1UUID), &nid) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", nid, "-c", "js1-edited-from-client") + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"content": "%s"}`, "js1-edited-from-server"), "editing js note 1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + resolvedBody := "<<<<<<< Local\njs1-edited-from-client\n=======\njs1-edited-from-server\n>>>>>>> Server\n" + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 4, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, resolvedBody, "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, resolvedBody, "cliNote1JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("clients deletes a note and server edits its body", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + var nid string + cliDatabase.MustScan(t, "getting id of note to remove", cliDB.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", jsNote1UUID), &nid) + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveNote, cliBinaryName, "remove", "js", nid) + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"content": "%s"}`, "js1-edited"), "editing js note 1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1-edited", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1-edited", "cliNote1JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("clients deletes a note and server moves it to another book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding css book") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + var nid string + cliDatabase.MustScan(t, "getting id of note to remove", cliDB.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", jsNote1UUID), &nid) + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveNote, cliBinaryName, "remove", "js", nid) + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"book_uuid": "%s"}`, cssBookUUID), "moving js note 1 to css book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + "cssBookUUID": cssBookUUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS, apiBookCSS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["cssBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS, cliBookCSS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["cssBookUUID"]), &cliBookCSS.Label, &cliBookCSS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, ids["cssNote1UUID"], "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server deletes a note and client edits it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiDeleteNote(t, env, user, jsNote1UUID, "deleting js note 1") + + // 4. on cli + var nid string + cliDatabase.MustScan(t, "getting id of note to edit", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "js1"), &nid) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", nid, "-c", "js1-edited") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 4, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1-edited", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, book_uuid, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.BookUUID, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1-edited", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, ids["jsBookUUID"], "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server deletes a book and client edits it by renaming it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiDeleteNote(t, env, user, jsNote1UUID, "deleting js note 1") + + // 4. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", "-n", "js-edited") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 1, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 4, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js-edited", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliBookJS.Label, "js-edited", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server deletes a book and client edits a note in it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiDeleteBook(t, env, user, jsBookUUID, "deleting js book") + + // 4. on cli + var nid string + cliDatabase.MustScan(t, "getting id of note to edit", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "js1"), &nid) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", nid, "-c", "js1-edited") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 6, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 6, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1-edited", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, book_uuid, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.BookUUID, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1-edited", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, ids["jsBookUUID"], "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client deletes a book and server edits it by renaming it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveBook, cliBinaryName, "remove", "js") + + // 3. on server + apiPatchBook(t, env, user, jsBookUUID, fmt.Sprintf(`{"name": "%s"}`, "js-edited"), "editing js book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 1, + clientLastMaxUSN: 5, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 5, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js-edited", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliBookJS.Label, "js-edited", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client deletes a book and server edits a note in it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"content": "%s"}`, "js1-edited"), "editing js1 note") + + // 4. on cli + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveBook, cliBinaryName, "remove", "js") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 0, + clientLastMaxUSN: 6, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 6, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, true, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client and server edit a book by renaming it to a same name", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", "-n", "js-edited") + + // 3. on server + apiPatchBook(t, env, user, jsBookUUID, fmt.Sprintf(`{"name": "%s"}`, "js-edited"), "editing js book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 4, + }) + + // test client + var cliBookJS cliDatabase.Book + var cliNote1JS cliDatabase.Note + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, book_uuid, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.BookUUID, &cliNote1JS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookJS.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js-edited", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js-edited", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client and server edit a book by renaming it to different names", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", "-n", "js-edited-client") + + // 3. on server + apiPatchBook(t, env, user, jsBookUUID, fmt.Sprintf(`{"name": "%s"}`, "js-edited-server"), "editing js book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + // In this case, server's change wins and overwrites that of client's + + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 4, + }) + + // test client + var cliBookJS cliDatabase.Book + var cliNote1JS cliDatabase.Note + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, book_uuid, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.BookUUID, &cliNote1JS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookJS.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js-edited-server", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js-edited-server", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client moves a note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding a css book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "1", "-b", "css") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "cssBookUUID": cssBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS, apiBookCSS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["cssBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + + // test client + var cliBookJS, cliBookCSS cliDatabase.Book + var cliNote1JS cliDatabase.Note + cliDatabase.MustScan(t, "finding cli book js", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cli book css", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, book_uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.BookUUID, &cliNote1JS.Body, &cliNote1JS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookCSS.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + assert.Equal(t, cliBookCSS.Dirty, false, "cliBookCSS Dirty mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client and server each moves a note to a same book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding a css book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"book_uuid": "%s"}`, cssBookUUID), "moving js note 1 to css book") + + // 3. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "1", "-b", "css") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "cssBookUUID": cssBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 2, + clientLastMaxUSN: 5, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 2, + serverUserMaxUSN: 5, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS, apiBookCSS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["cssBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + + // test client + var cliBookJS, cliBookCSS cliDatabase.Book + var cliNote1JS cliDatabase.Note + cliDatabase.MustScan(t, "finding cli book js", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cli book css", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, book_uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.BookUUID, &cliNote1JS.Body, &cliNote1JS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookCSS.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + assert.Equal(t, cliBookCSS.Dirty, false, "cliBookCSS Dirty mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client and server each moves a note to different books", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding a css book") + linuxBookUUID := apiCreateBook(t, env, user, "linux", "adding a linux book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"book_uuid": "%s"}`, cssBookUUID), "moving js note 1 to css book") + + // 3. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "1", "-b", "linux") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "cssBookUUID": cssBookUUID, + "jsNote1UUID": jsNote1UUID, + "linuxBookUUID": linuxBookUUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + expectedNote1JSBody := `<<<<<<< Local +Moved to the book linux +======= +Moved to the book css +>>>>>>> Server + +js1` + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 4, + clientLastMaxUSN: 7, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 4, + serverUserMaxUSN: 7, + }) + + // test client + var cliBookJS, cliBookCSS, cliBookLinux, cliBookConflicts cliDatabase.Book + var cliNote1JS cliDatabase.Note + cliDatabase.MustScan(t, "finding cli book js", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cli book css", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliBookLinux", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "linux"), &cliBookLinux.UUID, &cliBookLinux.Label, &cliBookLinux.USN) + cliDatabase.MustScan(t, "finding cliBookConflicts", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "conflicts"), &cliBookConflicts.UUID, &cliBookConflicts.Label, &cliBookConflicts.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, book_uuid, body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.UUID, &cliNote1JS.BookUUID, &cliNote1JS.Body, &cliNote1JS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + assert.NotEqual(t, cliBookLinux.USN, 0, "cliBookLinux USN mismatch") + assert.NotEqual(t, cliBookConflicts.USN, 0, "cliBookConflicts USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, expectedNote1JSBody, "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookConflicts.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + assert.Equal(t, cliBookLinux.Label, "linux", "cliBookLinux Label mismatch") + assert.Equal(t, cliBookConflicts.Label, "conflicts", "cliBookConflicts Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + assert.Equal(t, cliBookLinux.Deleted, false, "cliBookLinux Deleted mismatch") + assert.Equal(t, cliBookConflicts.Deleted, false, "cliBookConflicts Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + assert.Equal(t, cliBookCSS.Dirty, false, "cliBookCSS Dirty mismatch") + assert.Equal(t, cliBookLinux.Dirty, false, "cliBookLinux Dirty mismatch") + assert.Equal(t, cliBookConflicts.Dirty, false, "cliBookConflicts Dirty mismatch") + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS, apiBookCSS, apiBookLinux, apiBookConflicts database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linuxBookUUID"]).First(&apiBookLinux), "finding apiBookLinux") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookConflicts.UUID).First(&apiBookConflicts), "finding apiBookConflicts") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS USN mismatch") + assert.NotEqual(t, apiBookConflicts.USN, 0, "apiBookConflicts USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, expectedNote1JSBody, "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, apiBookConflicts.UUID, "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + assert.Equal(t, apiBookLinux.Label, "linux", "apiBookLinux Label mismatch") + assert.Equal(t, apiBookConflicts.Label, "conflicts", "apiBookConflicts Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + assert.Equal(t, apiBookLinux.Deleted, false, "apiBookLinux Deleted mismatch") + assert.Equal(t, apiBookConflicts.Deleted, false, "apiBookConflicts Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client adds a new book and moves a note into it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + + cliDB := env.DB + var nid string + cliDatabase.MustScan(t, "getting id of note to edit", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "js1"), &nid) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", nid, "-b", "css") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 5, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 5, + }) + + // test client + var cliBookJS, cliBookCSS cliDatabase.Book + var cliNote1JS, cliNote1CSS cliDatabase.Note + cliDatabase.MustScan(t, "finding cli book js", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cli book css", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, book_uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.BookUUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT uuid, book_uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliNote1CSS.UUID, &cliNote1CSS.BookUUID, &cliNote1CSS.Body, &cliNote1CSS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + assert.NotEqual(t, cliNote1CSS.USN, 0, "cliNote1CSS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookCSS.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliNote1CSS.BookUUID, cliBookCSS.UUID, "cliNote1CSS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliNote1CSS.Dirty, false, "cliNote1CSS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + assert.Equal(t, cliBookCSS.Dirty, false, "cliBookCSS Dirty mismatch") + + // test server + var apiNote1JS, apiNote1CSS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1CSS.UUID).First(&apiNote1CSS), "finding apiNote1CSS") + var apiBookJS, apiBookCSS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookCSS.UUID).First(&apiBookCSS), "finding apiBookCSS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, apiBookCSS.UUID, "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS Body mismatch") + assert.Equal(t, apiNote1CSS.BookUUID, apiBookCSS.UUID, "apiNote1CSS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiNote1CSS.Deleted, false, "apiNote1CSS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client adds a duplicate book and moves a note into it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + cssBookUUID := apiCreateBook(t, env, user, "css", "adding a css book") + + // 3. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + + var nid string + cliDatabase.MustScan(t, "getting id of note to edit", env.DB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "js1"), &nid) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", nid, "-b", "css") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "cssBookUUID": cssBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 3, + clientLastMaxUSN: 6, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 3, + serverUserMaxUSN: 6, + }) + + // test client + var cliBookJS, cliBookCSS, cliBookCSS2 cliDatabase.Book + var cliNote1JS, cliNote1CSS cliDatabase.Note + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css_2"), &cliBookCSS2.UUID, &cliBookCSS2.Label, &cliBookCSS2.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, book_uuid, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.BookUUID, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT uuid, body, book_uuid, usn FROM notes WHERE body = ?", "css1"), &cliNote1CSS.UUID, &cliNote1CSS.Body, &cliNote1CSS.BookUUID, &cliNote1CSS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + assert.NotEqual(t, cliBookCSS2.USN, 0, "cliBookCSS2 USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + assert.NotEqual(t, cliNote1CSS.USN, 0, "cliNote1CSS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookCSS2.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliNote1CSS.BookUUID, cliBookCSS2.UUID, "cliNote1CSS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + assert.Equal(t, cliBookCSS2.Label, "css_2", "cliBookCSS2 Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + assert.Equal(t, cliBookCSS2.Deleted, false, "cliBookCSS2 Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliNote1CSS.Dirty, false, "cliNote1CSS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + assert.Equal(t, cliBookCSS.Dirty, false, "cliBookCSS Dirty mismatch") + assert.Equal(t, cliBookCSS2.Dirty, false, "cliBookCSS2 Dirty mismatch") + + // test server + var apiNote1JS, apiNote1CSS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1CSS.UUID).First(&apiNote1CSS), "finding apiNote1CSS") + var apiBookJS, apiBookCSS, apiBookCSS2 database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookCSS2.UUID).First(&apiBookCSS2), "finding apiBookCSS2") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS USN mismatch") + assert.NotEqual(t, apiBookCSS2.USN, 0, "apiBookCSS2 USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, apiBookCSS2.UUID, "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS Body mismatch") + assert.Equal(t, apiNote1CSS.BookUUID, apiBookCSS2.UUID, "apiNote1CSS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + assert.Equal(t, apiBookCSS2.Label, "css_2", "apiBookCSS2 Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiNote1CSS.Deleted, false, "apiNote1CSS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + assert.Equal(t, apiBookCSS2.Deleted, false, "apiBookCSS2 Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) +} + +func TestFullSync(t *testing.T) { + t.Run("consecutively with stepSync", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // test client + var cliNote1JS, cliNote1CSS cliDatabase.Note + var cliBookJS, cliBookCSS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliNote1CSS.UUID, &cliNote1CSS.Body, &cliNote1CSS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + assert.NotEqual(t, cliNote1CSS.USN, 0, "cliNote1CSS USN mismatch") + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + + // test server + var apiNote1JS, apiNote1CSS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding api js note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1CSS.UUID).First(&apiNote1CSS), "finding api css note 1") + var apiBookJS, apiBookCSS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding api js book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookCSS.UUID).First(&apiBookCSS), "finding api css book") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS usn mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS usn mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS usn mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiNote1CSS.Deleted, false, "apiNote1CSS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + } + + t.Run("stepSync then fullSync", func(t *testing.T) { + env := setupTestEnv(t) + user := setupUserAndLogin(t, env) + ids := setup(t, env, user) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + assert(t, env, user, ids) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "-f") + assert(t, env, user, ids) + }) + + t.Run("fullSync then stepSync", func(t *testing.T) { + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + ids := setup(t, env, user) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "-f") + assert(t, env, user, ids) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + assert(t, env, user, ids) + }) + }) +} + +func TestSync_FreshClientConcurrent(t *testing.T) { + // Test the core issue: Fresh client (never synced, lastMaxUSN=0) syncing to a server + // that already has data uploaded by another client. + // + // Scenario: + // 1. Client A creates local notes (never synced, lastMaxUSN=0, lastSyncAt=0) + // 2. Client B uploads same book names to server first + // 3. Client A syncs + // + // Expected: Client A should pull server data first, detect duplicate book names, + // rename local books to avoid conflicts (js→js_2), then upload successfully. + + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + + // Client A: Create local data (never sync) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + + // Client B: Upload same book names to server via API + jsBookUUID := apiCreateBook(t, env, user, "js", "client B creating js book") + cssBookUUID := apiCreateBook(t, env, user, "css", "client B creating css book") + apiCreateNote(t, env, user, jsBookUUID, "js2", "client B note") + apiCreateNote(t, env, user, cssBookUUID, "css2", "client B note") + + // Client A syncs - should handle the conflict gracefully + // Expected: pulls server data, renames local books to js_2/css_2, uploads successfully + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // Verify: Should have 4 books and 4 notes on both client and server + // USN breakdown: 2 books + 2 notes from Client B (USN 1-4), then 2 books + 2 notes from Client A (USN 5-8) + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 4, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 4, + serverUserMaxUSN: 8, + }) + + // Verify server has all 4 books with correct names + var svrBookJS, svrBookCSS, svrBookJS2, svrBookCSS2 database.Book + apitest.MustExec(t, env.ServerDB.Where("label = ?", "js").First(&svrBookJS), "finding server book 'js'") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "css").First(&svrBookCSS), "finding server book 'css'") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "js_2").First(&svrBookJS2), "finding server book 'js_2'") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "css_2").First(&svrBookCSS2), "finding server book 'css_2'") + + assert.Equal(t, svrBookJS.Label, "js", "server should have book 'js' (Client B)") + assert.Equal(t, svrBookCSS.Label, "css", "server should have book 'css' (Client B)") + assert.Equal(t, svrBookJS2.Label, "js_2", "server should have book 'js_2' (Client A renamed)") + assert.Equal(t, svrBookCSS2.Label, "css_2", "server should have book 'css_2' (Client A renamed)") + + // Verify server has all 4 notes with correct content + var svrNoteJS1, svrNoteJS2, svrNoteCSS1, svrNoteCSS2 database.Note + apitest.MustExec(t, env.ServerDB.Where("body = ?", "js1").First(&svrNoteJS1), "finding server note 'js1'") + apitest.MustExec(t, env.ServerDB.Where("body = ?", "js2").First(&svrNoteJS2), "finding server note 'js2'") + apitest.MustExec(t, env.ServerDB.Where("body = ?", "css1").First(&svrNoteCSS1), "finding server note 'css1'") + apitest.MustExec(t, env.ServerDB.Where("body = ?", "css2").First(&svrNoteCSS2), "finding server note 'css2'") + + assert.Equal(t, svrNoteJS1.BookUUID, svrBookJS2.UUID, "note 'js1' should belong to book 'js_2' (Client A)") + assert.Equal(t, svrNoteJS2.BookUUID, svrBookJS.UUID, "note 'js2' should belong to book 'js' (Client B)") + assert.Equal(t, svrNoteCSS1.BookUUID, svrBookCSS2.UUID, "note 'css1' should belong to book 'css_2' (Client A)") + assert.Equal(t, svrNoteCSS2.BookUUID, svrBookCSS.UUID, "note 'css2' should belong to book 'css' (Client B)") + + // Verify client has all 4 books + var cliBookJS, cliBookCSS, cliBookJS2, cliBookCSS2 cliDatabase.Book + cliDatabase.MustScan(t, "finding client book 'js'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding client book 'css'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding client book 'js_2'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js_2"), &cliBookJS2.UUID, &cliBookJS2.Label, &cliBookJS2.USN) + cliDatabase.MustScan(t, "finding client book 'css_2'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css_2"), &cliBookCSS2.UUID, &cliBookCSS2.Label, &cliBookCSS2.USN) + + // Verify client UUIDs match server + assert.Equal(t, cliBookJS.UUID, svrBookJS.UUID, "client 'js' UUID should match server") + assert.Equal(t, cliBookCSS.UUID, svrBookCSS.UUID, "client 'css' UUID should match server") + assert.Equal(t, cliBookJS2.UUID, svrBookJS2.UUID, "client 'js_2' UUID should match server") + assert.Equal(t, cliBookCSS2.UUID, svrBookCSS2.UUID, "client 'css_2' UUID should match server") + + // Verify all books have non-zero USN (synced successfully) + assert.NotEqual(t, cliBookJS.USN, 0, "client 'js' should have non-zero USN") + assert.NotEqual(t, cliBookCSS.USN, 0, "client 'css' should have non-zero USN") + assert.NotEqual(t, cliBookJS2.USN, 0, "client 'js_2' should have non-zero USN") + assert.NotEqual(t, cliBookCSS2.USN, 0, "client 'css_2' should have non-zero USN") + + // Verify client has all 4 notes + var cliNoteJS1, cliNoteJS2, cliNoteCSS1, cliNoteCSS2 cliDatabase.Note + cliDatabase.MustScan(t, "finding client note 'js1'", env.DB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNoteJS1.UUID, &cliNoteJS1.Body, &cliNoteJS1.USN) + cliDatabase.MustScan(t, "finding client note 'js2'", env.DB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js2"), &cliNoteJS2.UUID, &cliNoteJS2.Body, &cliNoteJS2.USN) + cliDatabase.MustScan(t, "finding client note 'css1'", env.DB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliNoteCSS1.UUID, &cliNoteCSS1.Body, &cliNoteCSS1.USN) + cliDatabase.MustScan(t, "finding client note 'css2'", env.DB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css2"), &cliNoteCSS2.UUID, &cliNoteCSS2.Body, &cliNoteCSS2.USN) + + // Verify client note UUIDs match server + assert.Equal(t, cliNoteJS1.UUID, svrNoteJS1.UUID, "client note 'js1' UUID should match server") + assert.Equal(t, cliNoteJS2.UUID, svrNoteJS2.UUID, "client note 'js2' UUID should match server") + assert.Equal(t, cliNoteCSS1.UUID, svrNoteCSS1.UUID, "client note 'css1' UUID should match server") + assert.Equal(t, cliNoteCSS2.UUID, svrNoteCSS2.UUID, "client note 'css2' UUID should match server") + + // Verify all notes have non-zero USN (synced successfully) + assert.NotEqual(t, cliNoteJS1.USN, 0, "client note 'js1' should have non-zero USN") + assert.NotEqual(t, cliNoteJS2.USN, 0, "client note 'js2' should have non-zero USN") + assert.NotEqual(t, cliNoteCSS1.USN, 0, "client note 'css1' should have non-zero USN") + assert.NotEqual(t, cliNoteCSS2.USN, 0, "client note 'css2' should have non-zero USN") +} + +// TestSync_ConvergeSameBookNames tests that two clients don't enter an infinite sync loop if they +// try to sync books with the same names. Books shouldn't get marked dirty when re-downloaded from server. +func TestSync_ConvergeSameBookNames(t *testing.T) { + env := setupTestEnv(t) + tmpDir := t.TempDir() + + // Setup two separate client databases + client1DB := fmt.Sprintf("%s/client1.db", tmpDir) + client2DB := fmt.Sprintf("%s/client2.db", tmpDir) + defer os.Remove(client1DB) + defer os.Remove(client2DB) + + // Set up sessions + user := setupUser(t, env) + db1 := testutils.MustOpenDatabase(t, client1DB) + db2 := testutils.MustOpenDatabase(t, client2DB) + defer db1.Close() + defer db2.Close() + + // Client 1: First sync to empty server + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client1DB, "add", "testbook", "-c", "client1 note1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client1DB, "add", "anotherbook", "-c", "client1 note2") + login(t, db1, env.ServerDB, user) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client1DB, "sync") + checkState(t, db1, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Client 2: Sync (downloads client 1's data, adds own notes) ===== + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client2DB, "add", "testbook", "-c", "client2 note1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client2DB, "add", "anotherbook", "-c", "client2 note2") + login(t, db2, env.ServerDB, user) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client2DB, "sync") + // Verify state after client2 sync + checkState(t, db2, user, env.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 2, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 2, + serverUserMaxUSN: 8, + }) + + // Client 1: Sync again. It downloads client2's changes (2 extra notes). + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client1DB, "sync") + + // Verify MaxUSN did not increase (client1 should only download, not upload) + // Client1 still has: 2 original books + 4 notes (2 own + 2 from client2) + checkState(t, db1, user, env.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 2, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 2, + serverUserMaxUSN: 8, + }) + + // Verify no infinite loop: alternate syncing + // Both clients should be able to sync without any changes (MaxUSN stays at 8) + for range 3 { + // Client 2 syncs + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client2DB, "sync") + + // Verify client2 state unchanged + checkState(t, db2, user, env.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 2, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 2, + serverUserMaxUSN: 8, + }) + + // Client 1 syncs + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client1DB, "sync") + + // Verify client1 state unchanged + checkState(t, db1, user, env.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 2, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 2, + serverUserMaxUSN: 8, + }) + } +} diff --git a/pkg/e2e/sync/edge_cases_test.go b/pkg/e2e/sync/edge_cases_test.go new file mode 100644 index 00000000..f833cadc --- /dev/null +++ b/pkg/e2e/sync/edge_cases_test.go @@ -0,0 +1,163 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sync + +import ( + "io" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/consts" + cliDatabase "github.com/dnote/dnote/pkg/cli/database" + clitest "github.com/dnote/dnote/pkg/cli/testutils" + "github.com/google/uuid" + "github.com/pkg/errors" +) + +// TestSync_EmptyFragmentPreservesLastMaxUSN verifies that last_max_usn is not reset to 0 +// when sync receives an empty response from the server. +// +// Scenario: Client has orphaned note (references non-existent book). During sync: +// 1. Downloads data successfully (last_max_usn=3) +// 2. Upload fails (orphaned note -> 500 error, triggers retry stepSync) +// 3. Retry stepSync gets 0 fragments (already at latest USN) +// 4. last_max_usn should stay at 3, not reset to 0 +func TestSync_EmptyFragmentPreservesLastMaxUSN(t *testing.T) { + env := setupTestEnv(t) + user := setupUserAndLogin(t, env) + + // Create data on server (max_usn=3) + bookUUID := apiCreateBook(t, env, user, "javascript", "creating book via API") + apiCreateNote(t, env, user, bookUUID, "note1 content", "creating note1 via API") + apiCreateNote(t, env, user, bookUUID, "note2 content", "creating note2 via API") + + // Create orphaned note locally (will fail to upload) + orphanedNote := cliDatabase.Note{ + UUID: uuid.New().String(), + BookUUID: uuid.New().String(), // non-existent book + Body: "orphaned note content", + AddedOn: 1234567890, + EditedOn: 0, + USN: 0, + Deleted: false, + Dirty: true, + } + if err := orphanedNote.Insert(env.DB); err != nil { + t.Fatal(err) + } + + // Run sync (downloads data, upload fails, retry gets 0 fragments) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // Verify last_max_usn is preserved at 3, not reset to 0 + var lastMaxUSN int + cliDatabase.MustScan(t, "finding system last_max_usn", + env.DB.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), + &lastMaxUSN) + + assert.Equal(t, lastMaxUSN, 3, "last_max_usn should be 3 after syncing") +} + +// TestSync_ConcurrentInitialSync reproduces the issue where two clients with identical +// local data syncing simultaneously to an empty server results in 500 errors. +// +// This demonstrates the race condition: +// - Client1 starts sync to empty server, gets empty server state +// - Client2 syncs. +// - Client1 tries to create same books → 409 "duplicate" +// - Client1 tries to create notes with wrong UUIDs → 500 "record not found" +// - stepSync recovers by renaming local books with _2 suffix +func TestSync_ConcurrentInitialSync(t *testing.T) { + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + + // Step 1: Create local data and sync + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "javascript", "-c", "js note from client1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 2, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 2, + }) + + // Step 2: Switch to new empty server to simulate concurrent initial sync scenario + switchToEmptyServer(t, &env) + user = setupUserAndLogin(t, env) + + // Set up client2 with separate database + client2DB, client2DBPath := cliDatabase.InitTestFileDB(t) + login(t, client2DB, env.ServerDB, user) + client2DB.Close() // Close so CLI can access the database + + // Step 3: Client1 syncs to empty server, but during sync Client2 uploads same data + // This simulates the race condition deterministically + raceCallback := func(stdout io.Reader, stdin io.WriteCloser) error { + // Wait for empty server prompt to ensure Client1 has called GetSyncState + clitest.MustWaitForPrompt(t, stdout, clitest.PromptEmptyServer) + + // Now Client2 creates the same book and note via CLI (creating the race condition) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client2DBPath, "add", "javascript", "-c", "js note from client2") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client2DBPath, "sync") + + // User confirms sync + if _, err := io.WriteString(stdin, "y\n"); err != nil { + return errors.Wrap(err, "confirming sync") + } + + return nil + } + + // Client1 continues sync - will hit 409 conflict, then 500 error, then recover + clitest.MustWaitDnoteCmd(t, env.CmdOpts, raceCallback, cliBinaryName, "sync") + + // After sync: + // - Server has 2 books: "javascript" (from client2) and "javascript_2" (from client1 renamed) + // - Server has 2 notes + // - Both clients should converge to the same state + expectedState := systemState{ + clientNoteCount: 2, // both notes + clientBookCount: 2, // javascript and javascript_2 + clientLastMaxUSN: 4, // 2 books + 2 notes + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + } + checkState(t, env.DB, user, env.ServerDB, expectedState) + + // Client2 syncs again to download client1's data + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client2DBPath, "sync") + client2DB = clitest.MustOpenDatabase(t, client2DBPath) + defer client2DB.Close() + + // Client2 should have converged to the same state as client1 + checkState(t, client2DB, user, env.ServerDB, expectedState) + + // Verify no orphaned notes on server + var orphanedCount int + if err := env.ServerDB.Raw(` + SELECT COUNT(*) FROM notes + WHERE book_uuid NOT IN (SELECT uuid FROM books) + `).Scan(&orphanedCount).Error; err != nil { + t.Fatal(err) + } + assert.Equal(t, orphanedCount, 0, "server should have no orphaned notes after sync") +} diff --git a/pkg/e2e/sync/empty_server_test.go b/pkg/e2e/sync/empty_server_test.go new file mode 100644 index 00000000..31756ed2 --- /dev/null +++ b/pkg/e2e/sync/empty_server_test.go @@ -0,0 +1,728 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sync + +import ( + "database/sql" + "fmt" + "io" + "os" + "strconv" + "strings" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/consts" + cliDatabase "github.com/dnote/dnote/pkg/cli/database" + clitest "github.com/dnote/dnote/pkg/cli/testutils" + "github.com/dnote/dnote/pkg/server/database" + apitest "github.com/dnote/dnote/pkg/server/testutils" + "github.com/pkg/errors" +) + +func TestSync_EmptyServer(t *testing.T) { + t.Run("sync to empty server after syncing to non-empty server", func(t *testing.T) { + // Test server data loss/wipe scenario (disaster recovery): + // Verify empty server detection works when the server loses all its data + + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + + // Step 1: Create local data and sync to server + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // Verify sync succeeded + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Step 2: Switch to a completely new empty server + switchToEmptyServer(t, &env) + + // Recreate user and session on new server + user = setupUserAndLogin(t, env) + + // Step 3: Sync again - should detect empty server and prompt user + // User confirms with "y" + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.UserConfirmEmptyServerSync, cliBinaryName, "sync") + + // Step 4: Verify data was uploaded to the empty server + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Verify the content is correct on both client and server + var cliNote1JS, cliNote1CSS cliDatabase.Note + var cliBookJS, cliBookCSS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", env.DB.QueryRow("SELECT uuid, body FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body) + cliDatabase.MustScan(t, "finding cliNote1CSS", env.DB.QueryRow("SELECT uuid, body FROM notes WHERE body = ?", "css1"), &cliNote1CSS.UUID, &cliNote1CSS.Body) + cliDatabase.MustScan(t, "finding cliBookJS", env.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label) + cliDatabase.MustScan(t, "finding cliBookCSS", env.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label) + + assert.Equal(t, cliNote1JS.Body, "js1", "js note body mismatch") + assert.Equal(t, cliNote1CSS.Body, "css1", "css note body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "js book label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "css book label mismatch") + + // Verify on server side + var serverNoteJS, serverNoteCSS database.Note + var serverBookJS, serverBookCSS database.Book + apitest.MustExec(t, env.ServerDB.Where("body = ?", "js1").First(&serverNoteJS), "finding server note js1") + apitest.MustExec(t, env.ServerDB.Where("body = ?", "css1").First(&serverNoteCSS), "finding server note css1") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "js").First(&serverBookJS), "finding server book js") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "css").First(&serverBookCSS), "finding server book css") + + assert.Equal(t, serverNoteJS.Body, "js1", "server js note body mismatch") + assert.Equal(t, serverNoteCSS.Body, "css1", "server css note body mismatch") + assert.Equal(t, serverBookJS.Label, "js", "server js book label mismatch") + assert.Equal(t, serverBookCSS.Label, "css", "server css book label mismatch") + }) + + t.Run("user cancels empty server prompt", func(t *testing.T) { + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + + // Step 1: Create local data and sync to server + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // Verify initial sync succeeded + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Step 2: Switch to empty server + switchToEmptyServer(t, &env) + user = setupUserAndLogin(t, env) + + // Step 3: Sync again but user cancels with "n" + output, err := clitest.WaitDnoteCmd(t, env.CmdOpts, clitest.UserCancelEmptyServerSync, cliBinaryName, "sync") + if err == nil { + t.Fatal("Expected sync to fail when user cancels, but it succeeded") + } + + // Verify the prompt appeared + if !strings.Contains(output, clitest.PromptEmptyServer) { + t.Fatalf("Expected empty server warning in output, got: %s", output) + } + + // Step 4: Verify local state unchanged (transaction rolled back) + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 0, + serverBookCount: 0, + serverUserMaxUSN: 0, + }) + + // Verify items still have original USN and dirty=false + var book cliDatabase.Book + var note cliDatabase.Note + cliDatabase.MustScan(t, "checking book state", env.DB.QueryRow("SELECT usn, dirty FROM books WHERE label = ?", "js"), &book.USN, &book.Dirty) + cliDatabase.MustScan(t, "checking note state", env.DB.QueryRow("SELECT usn, dirty FROM notes WHERE body = ?", "js1"), ¬e.USN, ¬e.Dirty) + + assert.NotEqual(t, book.USN, 0, "book USN should not be reset") + assert.NotEqual(t, note.USN, 0, "note USN should not be reset") + assert.Equal(t, book.Dirty, false, "book should not be marked dirty") + assert.Equal(t, note.Dirty, false, "note should not be marked dirty") + }) + + t.Run("all local data is marked deleted - should not upload", func(t *testing.T) { + // Test edge case: Server MaxUSN=0, local MaxUSN>0, but all items are deleted=true + // Should NOT prompt because there's nothing to upload + + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + + // Step 1: Create local data and sync to server + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // Verify initial sync succeeded + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Step 2: Delete all local notes and books (mark as deleted) + cliDatabase.MustExec(t, "marking all books deleted", env.DB, "UPDATE books SET deleted = 1") + cliDatabase.MustExec(t, "marking all notes deleted", env.DB, "UPDATE notes SET deleted = 1") + + // Step 3: Switch to empty server + switchToEmptyServer(t, &env) + user = setupUserAndLogin(t, env) + + // Step 4: Sync - should NOT prompt because bookCount=0 and noteCount=0 (counting only deleted=0) + // This should complete without user interaction + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // Verify no data was uploaded (server still empty, but client still has deleted items) + // Check server is empty + var serverNoteCount, serverBookCount int64 + apitest.MustExec(t, env.ServerDB.Model(&database.Note{}).Count(&serverNoteCount), "counting server notes") + apitest.MustExec(t, env.ServerDB.Model(&database.Book{}).Count(&serverBookCount), "counting server books") + assert.Equal(t, serverNoteCount, int64(0), "server should have no notes") + assert.Equal(t, serverBookCount, int64(0), "server should have no books") + + // Check client still has the deleted items locally + var clientNoteCount, clientBookCount int + cliDatabase.MustScan(t, "counting client notes", env.DB.QueryRow("SELECT count(*) FROM notes WHERE deleted = 1"), &clientNoteCount) + cliDatabase.MustScan(t, "counting client books", env.DB.QueryRow("SELECT count(*) FROM books WHERE deleted = 1"), &clientBookCount) + assert.Equal(t, clientNoteCount, 2, "client should still have 2 deleted notes") + assert.Equal(t, clientBookCount, 2, "client should still have 2 deleted books") + + // Verify lastMaxUSN was reset to 0 + var lastMaxUSN int + cliDatabase.MustScan(t, "getting lastMaxUSN", env.DB.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &lastMaxUSN) + assert.Equal(t, lastMaxUSN, 0, "lastMaxUSN should be reset to 0") + }) + + t.Run("race condition - other client uploads first", func(t *testing.T) { + // This test exercises a race condition that can occur during sync: + // While Client A is waiting for user input, Client B uploads data to the server. + // + // The empty server scenario is the natural place to test this because + // an empty server detection triggers a prompt, at which point the test + // can make client B upload data. We trigger the race condition deterministically. + // + // Test flow: + // - Client A detects empty server and prompts user + // - While waiting for confirmation, Client B uploads the same data via API + // - Client A continues and handles the 409 conflict gracefully by: + // 1. Detecting the 409 error when trying to CREATE books that already exist + // 2. Running stepSync to pull the server's books (js, css) + // 3. mergeBook renames local conflicts (js→js_2, css→css_2) + // 4. Retrying sendChanges to upload the renamed books + // - Result: Both clients' data is preserved (4 books total) + + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + + // Step 1: Create local data and sync to establish lastMaxUSN > 0 + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // Verify initial sync succeeded + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Step 2: Switch to new empty server to simulate switching to empty server + switchToEmptyServer(t, &env) + + // Create user on new server and login + user = setupUserAndLogin(t, env) + + // Step 3: Trigger sync which will detect empty server and prompt user + // Inside the callback (before confirming), we simulate Client B uploading via API. + // We wait for the empty server prompt to ensure Client B uploads AFTER + // GetSyncState but BEFORE the sync decision, creating the race condition deterministically + raceCallback := func(stdout io.Reader, stdin io.WriteCloser) error { + // First, wait for the prompt to ensure Client A has obtained the sync state from the server. + clitest.MustWaitForPrompt(t, stdout, clitest.PromptEmptyServer) + + // Now Client B uploads the same data via API (after Client A got the sync state from the server + // but before its sync decision) + // This creates the race condition: Client A thinks server is empty, but Client B uploads data + jsBookUUID := apiCreateBook(t, env, user, "js", "client B creating js book") + cssBookUUID := apiCreateBook(t, env, user, "css", "client B creating css book") + apiCreateNote(t, env, user, jsBookUUID, "js1", "client B creating js note") + apiCreateNote(t, env, user, cssBookUUID, "css1", "client B creating css note") + + // Now user confirms + if _, err := io.WriteString(stdin, "y\n"); err != nil { + return errors.Wrap(err, "confirming sync") + } + + return nil + } + + // Step 4: Client A runs sync with race condition + // The 409 conflict is automatically handled: + // - When 409 is detected, isBehind flag is set + // - stepSync pulls Client B's data + // - mergeBook renames Client A's books to js_2, css_2 + // - Renamed books are uploaded + // - Both clients' data is preserved. + clitest.MustWaitDnoteCmd(t, env.CmdOpts, raceCallback, cliBinaryName, "sync") + + // Verify final state - both clients' data preserved + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 4, // Both clients' notes + clientBookCount: 4, // js, css, js_2, css_2 + clientLastMaxUSN: 8, // 4 from Client B + 4 from Client A's renamed books/notes + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 4, + serverUserMaxUSN: 8, + }) + + // Verify server has both clients' books + var svrBookJS, svrBookCSS, svrBookJS2, svrBookCSS2 database.Book + apitest.MustExec(t, env.ServerDB.Where("label = ?", "js").First(&svrBookJS), "finding server book 'js'") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "css").First(&svrBookCSS), "finding server book 'css'") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "js_2").First(&svrBookJS2), "finding server book 'js_2'") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "css_2").First(&svrBookCSS2), "finding server book 'css_2'") + + assert.Equal(t, svrBookJS.Label, "js", "server should have book 'js' (Client B)") + assert.Equal(t, svrBookCSS.Label, "css", "server should have book 'css' (Client B)") + assert.Equal(t, svrBookJS2.Label, "js_2", "server should have book 'js_2' (Client A renamed)") + assert.Equal(t, svrBookCSS2.Label, "css_2", "server should have book 'css_2' (Client A renamed)") + + // Verify client has all books + var cliBookJS, cliBookCSS, cliBookJS2, cliBookCSS2 cliDatabase.Book + cliDatabase.MustScan(t, "finding client book 'js'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding client book 'css'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding client book 'js_2'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js_2"), &cliBookJS2.UUID, &cliBookJS2.Label, &cliBookJS2.USN) + cliDatabase.MustScan(t, "finding client book 'css_2'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css_2"), &cliBookCSS2.UUID, &cliBookCSS2.Label, &cliBookCSS2.USN) + + // Verify client UUIDs match server + assert.Equal(t, cliBookJS.UUID, svrBookJS.UUID, "client 'js' UUID should match server") + assert.Equal(t, cliBookCSS.UUID, svrBookCSS.UUID, "client 'css' UUID should match server") + assert.Equal(t, cliBookJS2.UUID, svrBookJS2.UUID, "client 'js_2' UUID should match server") + assert.Equal(t, cliBookCSS2.UUID, svrBookCSS2.UUID, "client 'css_2' UUID should match server") + + // Verify all items have non-zero USN (synced successfully) + assert.NotEqual(t, cliBookJS.USN, 0, "client 'js' should have non-zero USN") + assert.NotEqual(t, cliBookCSS.USN, 0, "client 'css' should have non-zero USN") + assert.NotEqual(t, cliBookJS2.USN, 0, "client 'js_2' should have non-zero USN") + assert.NotEqual(t, cliBookCSS2.USN, 0, "client 'css_2' should have non-zero USN") + }) + + t.Run("sync to server A, then B, then back to A, then back to B", func(t *testing.T) { + // Test switching between two actual servers to verify: + // 1. Empty server detection works when switching to empty server + // 2. No false detection when switching back to non-empty servers + // 3. Both servers maintain independent state across multiple switches + + env := setupTestEnv(t) + + // Create Server A with its own database + serverA, serverDBA, err := setupTestServer(t, serverTime) + if err != nil { + t.Fatal(errors.Wrap(err, "setting up server A")) + } + defer serverA.Close() + + // Create Server B with its own database + serverB, serverDBB, err := setupTestServer(t, serverTime) + if err != nil { + t.Fatal(errors.Wrap(err, "setting up server B")) + } + defer serverB.Close() + + // Step 1: Set up user on Server A and sync + apiEndpointA := fmt.Sprintf("%s/api", serverA.URL) + + userA := apitest.SetupUserData(serverDBA, "alice@example.com", "pass1234") + sessionA := apitest.SetupSession(serverDBA, userA) + cliDatabase.MustExec(t, "inserting session_key", env.DB, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemSessionKey, sessionA.Key) + cliDatabase.MustExec(t, "inserting session_key_expiry", env.DB, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemSessionKeyExpiry, sessionA.ExpiresAt.Unix()) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "--apiEndpoint", apiEndpointA) + + // Verify sync to Server A succeeded + checkState(t, env.DB, userA, serverDBA, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Step 2: Switch to Server B (empty) and sync + apiEndpointB := fmt.Sprintf("%s/api", serverB.URL) + + // Set up user on Server B + userB := apitest.SetupUserData(serverDBB, "alice@example.com", "pass1234") + sessionB := apitest.SetupSession(serverDBB, userB) + cliDatabase.MustExec(t, "updating session_key for B", env.DB, "UPDATE system SET value = ? WHERE key = ?", sessionB.Key, consts.SystemSessionKey) + cliDatabase.MustExec(t, "updating session_key_expiry for B", env.DB, "UPDATE system SET value = ? WHERE key = ?", sessionB.ExpiresAt.Unix(), consts.SystemSessionKeyExpiry) + + // Should detect empty server and prompt + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.UserConfirmEmptyServerSync, cliBinaryName, "sync", "--apiEndpoint", apiEndpointB) + + // Verify Server B now has data + checkState(t, env.DB, userB, serverDBB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Step 3: Switch back to Server A and sync + cliDatabase.MustExec(t, "updating session_key back to A", env.DB, "UPDATE system SET value = ? WHERE key = ?", sessionA.Key, consts.SystemSessionKey) + cliDatabase.MustExec(t, "updating session_key_expiry back to A", env.DB, "UPDATE system SET value = ? WHERE key = ?", sessionA.ExpiresAt.Unix(), consts.SystemSessionKeyExpiry) + + // Should NOT trigger empty server detection (Server A has MaxUSN > 0) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "--apiEndpoint", apiEndpointA) + + // Verify Server A still has its data + checkState(t, env.DB, userA, serverDBA, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Step 4: Switch back to Server B and sync again + cliDatabase.MustExec(t, "updating session_key back to B", env.DB, "UPDATE system SET value = ? WHERE key = ?", sessionB.Key, consts.SystemSessionKey) + cliDatabase.MustExec(t, "updating session_key_expiry back to B", env.DB, "UPDATE system SET value = ? WHERE key = ?", sessionB.ExpiresAt.Unix(), consts.SystemSessionKeyExpiry) + + // Should NOT trigger empty server detection (Server B now has MaxUSN > 0 from Step 2) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "--apiEndpoint", apiEndpointB) + + // Verify both servers maintain independent state + checkState(t, env.DB, userB, serverDBB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + }) + + t.Run("two clients with identical copied database sync to empty server", func(t *testing.T) { + // Suppose we have two clients and server becomes empty (migration). + // After the first client sync to empty server, the second client should trigger full sync. + // Without the full sync, client2 will do step sync asking for changes after its stale USN, + // get nothing from server, and potentially orphan notes during full sync. + + // Step 1: Create client1 with data and sync to ORIGINAL server + env1 := setupTestEnv(t) + user := setupUserAndLogin(t, env1) + + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "sync") + // Add more data to create a higher USN + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "add", "go", "-c", "go1") + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "add", "rust", "-c", "rust1") + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "sync") + // Verify initial sync succeeded (now with 4 notes, 4 books, USN=8) + checkState(t, env1.DB, user, env1.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 4, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 4, + serverUserMaxUSN: 8, + }) + + // Step 2: Create client2 by copying client1's database (simulating same DB on two devices) + env2 := setupTestEnv(t) + // Copy the database file from client1 to client2 + dbPath1 := env1.DB.Filepath + dbPath2 := env2.DB.Filepath + // Close both DBs before copying + env1.DB.Close() + env2.DB.Close() + + // Copy the database file + input, err := os.ReadFile(dbPath1) + if err != nil { + t.Fatal(errors.Wrap(err, "reading client1 database")) + } + if err := os.WriteFile(dbPath2, input, 0644); err != nil { + t.Fatal(errors.Wrap(err, "writing client2 database")) + } + + // Reopen databases + env1.DB, err = cliDatabase.Open(dbPath1) + if err != nil { + t.Fatal(errors.Wrap(err, "reopening client1 database")) + } + env2.DB, err = cliDatabase.Open(dbPath2) + if err != nil { + t.Fatal(errors.Wrap(err, "reopening client2 database")) + } + + // Verify client2 has identical data and USN=8 (stale) - same as client1 + // Note: at this point there's no server to compare against, we just check counts + var client2MaxUSN, client2NoteCount, client2BookCount int + cliDatabase.MustScan(t, "getting client2 maxUSN", + env2.DB.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), + &client2MaxUSN) + cliDatabase.MustScan(t, "counting client2 notes", + env2.DB.QueryRow("SELECT count(*) FROM notes WHERE deleted = 0"), + &client2NoteCount) + cliDatabase.MustScan(t, "counting client2 books", + env2.DB.QueryRow("SELECT count(*) FROM books WHERE deleted = 0"), + &client2BookCount) + + assert.Equal(t, client2MaxUSN, 8, "client2 should have same maxUSN=8 as client1") + assert.Equal(t, client2NoteCount, 4, "client2 should have 4 notes") + assert.Equal(t, client2BookCount, 4, "client2 should have 4 books") + + // Step 3: Switch client1 to new empty server + switchToEmptyServer(t, &env1) + + // Point client2 to the same new server + env2.Server = env1.Server + env2.ServerDB = env1.ServerDB + + // Update client2's API endpoint config to point to env1's server + apiEndpoint := fmt.Sprintf("%s/api", env1.Server.URL) + updateConfigAPIEndpoint(t, env2.TmpDir, apiEndpoint) + + // Create same user on new server + user = setupUserAndLogin(t, env1) + + // Setup session for client2 (same user, same server) + login(t, env2.DB, env2.ServerDB, user) + + // Step 4: Client1 syncs ONLY FIRST 2 BOOKS to empty server (simulates partial upload) + // This creates the stale USN scenario: client2 has maxUSN=8, but server will only have maxUSN=4 + clitest.MustWaitDnoteCmd(t, env1.CmdOpts, clitest.UserConfirmEmptyServerSync, + cliBinaryName, "sync") + + // Delete the last 2 books from client1 to prevent them being on server + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "remove", "go", "-y") + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "remove", "rust", "-y") + + // Sync deletions to server + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "sync") + + // Verify server has 2 active books/notes (go/rust deleted) + var serverNoteCount, serverBookCount int64 + apitest.MustExec(t, env1.ServerDB.Model(&database.Note{}).Where("deleted = ?", false).Count(&serverNoteCount), "counting active server notes") + apitest.MustExec(t, env1.ServerDB.Model(&database.Book{}).Where("deleted = ?", false).Count(&serverBookCount), "counting active server books") + assert.Equal(t, int(serverNoteCount), 2, "server should have 2 active notes (go/rust deleted)") + assert.Equal(t, int(serverBookCount), 2, "server should have 2 active books (go/rust deleted)") + + // Step 5: Client2 syncs + // CRITICAL: Client2 has lastMaxUSN=8 (from copied DB), but server's max_usn is now ~8 but only has 2 books + // Client2 will ask for changes after USN=8, get nothing, then try to upload its 4 books + // This should trigger the orphaned notes scenario or require full sync + clitest.RunDnoteCmd(t, env2.CmdOpts, cliBinaryName, "sync") + + // Step 6: Verify client2 has all data and NO orphaned notes + var orphanedCount int + cliDatabase.MustScan(t, "checking for orphaned notes", + env2.DB.QueryRow(` + SELECT COUNT(*) FROM notes + WHERE deleted = 0 + AND book_uuid NOT IN (SELECT uuid FROM books WHERE deleted = 0) + `), &orphanedCount) + + assert.Equal(t, orphanedCount, 0, "client2 should have no orphaned notes") + + // Verify client2 converged with server state + // Note: checkState counts ALL records (including deleted ones) + // During full sync, cleanLocalBooks/cleanLocalNotes DELETE local records not on server + // So client2 ends up with only the 2 active books/notes + // Server has 4 total (2 active + 2 deleted) + var client2LastMaxUSN, client2LastSyncAt int + var serverUserMaxUSN int + cliDatabase.MustScan(t, "getting client2 lastMaxUSN", + env2.DB.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), + &client2LastMaxUSN) + var lastSyncAtStr string + cliDatabase.MustScan(t, "getting client2 lastSyncAt", + env2.DB.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastSyncAt), + &lastSyncAtStr) + lastSyncAtInt, _ := strconv.ParseInt(lastSyncAtStr, 10, 64) + client2LastSyncAt = int(lastSyncAtInt) + + apitest.MustExec(t, env2.ServerDB.Table("users").Select("max_usn").Where("id = ?", user.ID).Scan(&serverUserMaxUSN), "getting server user max_usn") + + checkState(t, env2.DB, user, env2.ServerDB, systemState{ + clientNoteCount: 2, // Only active notes (deleted ones removed by cleanLocalNotes) + clientBookCount: 2, // Only active books (deleted ones removed by cleanLocalBooks) + clientLastMaxUSN: client2LastMaxUSN, + clientLastSyncAt: int64(client2LastSyncAt), + serverNoteCount: 4, // 2 active + 2 deleted + serverBookCount: 4, // 2 active + 2 deleted + serverUserMaxUSN: serverUserMaxUSN, + }) + + // Verify both clients have the expected books (css, js only - go/rust deleted) + var client1BookCSS, client1BookJS, client2BookCSS, client2BookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding client1 book 'css'", + env1.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ? AND deleted = 0", "css"), + &client1BookCSS.UUID, &client1BookCSS.Label) + cliDatabase.MustScan(t, "finding client1 book 'js'", + env1.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ? AND deleted = 0", "js"), + &client1BookJS.UUID, &client1BookJS.Label) + cliDatabase.MustScan(t, "finding client2 book 'css'", + env2.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ? AND deleted = 0", "css"), + &client2BookCSS.UUID, &client2BookCSS.Label) + cliDatabase.MustScan(t, "finding client2 book 'js'", + env2.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ? AND deleted = 0", "js"), + &client2BookJS.UUID, &client2BookJS.Label) + + assert.Equal(t, client1BookCSS.Label, "css", "client1 should have css book") + assert.Equal(t, client1BookJS.Label, "js", "client1 should have js book") + assert.Equal(t, client2BookCSS.Label, "css", "client2 should have css book") + assert.Equal(t, client2BookJS.Label, "js", "client2 should have js book") + + // Verify go and rust books are deleted/absent on both clients + var client2BookGo, client2BookRust cliDatabase.Book + errGo := env2.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ? AND deleted = 0", "go").Scan(&client2BookGo.UUID, &client2BookGo.Label) + assert.Equal(t, errGo, sql.ErrNoRows, "client2 should not have non-deleted 'go' book") + errRust := env2.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ? AND deleted = 0", "rust").Scan(&client2BookRust.UUID, &client2BookRust.Label) + assert.Equal(t, errRust, sql.ErrNoRows, "client2 should not have non-deleted 'rust' book") + }) + + t.Run("client with local data syncs after another client uploads to empty server - should not orphan notes", func(t *testing.T) { + // This test reproduces the scenario where: + // 1. Client1 has local data and syncs to original server + // 2. Client2 has DIFFERENT local data and syncs to SAME original server + // 3. Both clients switch to NEW empty server + // 4. Client1 uploads to the new empty server (sets FullSyncBefore) + // 5. Client2 syncs - should trigger full sync AND upload its local data + // WITHOUT orphaning notes due to cleanLocalBooks deleting them first + + // Step 1: Create client1 with local data on original server + env1 := setupTestEnv(t) + user := setupUserAndLogin(t, env1) + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "add", "client1-book", "-c", "client1-note") + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "sync") + + // Step 2: Create client2 with DIFFERENT local data on SAME original server + env2 := setupTestEnv(t) + // Point env2 to env1's server (the original server) + env2.Server = env1.Server + env2.ServerDB = env1.ServerDB + apiEndpoint := fmt.Sprintf("%s/api", env1.Server.URL) + updateConfigAPIEndpoint(t, env2.TmpDir, apiEndpoint) + + // Login client2 to the same server + login(t, env2.DB, env2.ServerDB, user) + clitest.RunDnoteCmd(t, env2.CmdOpts, cliBinaryName, "add", "client2-book", "-c", "client2-note") + clitest.RunDnoteCmd(t, env2.CmdOpts, cliBinaryName, "sync") + + // Step 3: Both clients switch to NEW empty server + switchToEmptyServer(t, &env1) + env2.Server = env1.Server + env2.ServerDB = env1.ServerDB + apiEndpoint = fmt.Sprintf("%s/api", env1.Server.URL) + updateConfigAPIEndpoint(t, env2.TmpDir, apiEndpoint) + + // Create same user on new server + user = setupUserAndLogin(t, env1) + login(t, env2.DB, env2.ServerDB, user) + + // Step 4: Client1 uploads to empty server + clitest.MustWaitDnoteCmd(t, env1.CmdOpts, clitest.UserConfirmEmptyServerSync, cliBinaryName, "sync") + + // Verify server has client1's data and FullSyncBefore is set + var serverUser database.User + apitest.MustExec(t, env1.ServerDB.Where("id = ?", user.ID).First(&serverUser), "getting server user state") + assert.Equal(t, serverUser.MaxUSN > 0, true, "server should have data after client1 upload") + assert.Equal(t, serverUser.FullSyncBefore > 0, true, "server should have FullSyncBefore set") + + // Step 5: Client2 syncs - should trigger full sync due to FullSyncBefore + // CRITICAL: Client2 has local data (client2-book, client2-note) that should be uploaded + // Without the fix, cleanLocalBooks will delete client2-book before upload, orphaning client2-note + clitest.RunDnoteCmd(t, env2.CmdOpts, cliBinaryName, "sync") + + // Step 6: Verify NO orphaned notes on client2 + var orphanedCount int + cliDatabase.MustScan(t, "checking for orphaned notes on client2", + env2.DB.QueryRow(` + SELECT COUNT(*) FROM notes + WHERE deleted = 0 + AND book_uuid NOT IN (SELECT uuid FROM books WHERE deleted = 0) + `), &orphanedCount) + + assert.Equal(t, orphanedCount, 0, "client2 should have no orphaned notes after sync") + + // Step 7: Verify client2's data was uploaded to server + var client2BookOnServer database.Book + err := env2.ServerDB.Where("label = ? AND deleted = ?", "client2-book", false).First(&client2BookOnServer).Error + assert.Equal(t, err, nil, "client2-book should exist on server") + + var client2NoteOnServer database.Note + err = env2.ServerDB.Where("body = ? AND deleted = ?", "client2-note", false).First(&client2NoteOnServer).Error + assert.Equal(t, err, nil, "client2-note should exist on server") + + // Step 8: Verify server has data from BOTH clients + // Note: Both clients had synced to original server, so they each have 2 books + 2 notes locally. + // When switching to new empty server, client1 uploads 2 books + 2 notes (USN 1-4). + // Client2 then does full sync, downloads client1's uploads, marks its local data as dirty, + // and uploads its version of the same 2 books + 2 notes with potentially different UUIDs. + // The exact count depends on UUID conflict resolution, but we verify both original books exist. + var serverBookCount, serverNoteCount int64 + apitest.MustExec(t, env2.ServerDB.Model(&database.Book{}).Where("deleted = ?", false).Count(&serverBookCount), "counting active server books") + apitest.MustExec(t, env2.ServerDB.Model(&database.Note{}).Where("deleted = ?", false).Count(&serverNoteCount), "counting active server notes") + + // The main assertion: both original client books should exist + var client1BookExists, client2BookExists bool + err = env2.ServerDB.Model(&database.Book{}).Where("label = ? AND deleted = ?", "client1-book", false).First(&database.Book{}).Error + client1BookExists = (err == nil) + err = env2.ServerDB.Model(&database.Book{}).Where("label = ? AND deleted = ?", "client2-book", false).First(&database.Book{}).Error + client2BookExists = (err == nil) + + assert.Equal(t, client1BookExists, true, "server should have client1-book") + assert.Equal(t, client2BookExists, true, "server should have client2-book") + assert.Equal(t, serverBookCount >= 2, true, "server should have at least 2 books") + assert.Equal(t, serverNoteCount >= 2, true, "server should have at least 2 notes") + }) +} diff --git a/pkg/e2e/sync/main_test.go b/pkg/e2e/sync/main_test.go new file mode 100644 index 00000000..188e3a99 --- /dev/null +++ b/pkg/e2e/sync/main_test.go @@ -0,0 +1,54 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sync + +import ( + "bytes" + "fmt" + "log" + "os" + "os/exec" + "testing" + "time" + + "github.com/pkg/errors" +) + +var cliBinaryName string +var serverTime = time.Date(2017, time.March, 14, 21, 15, 0, 0, time.UTC) + +var testDir = "./tmp/" + +func init() { + cliBinaryName = fmt.Sprintf("%s/test-cli", testDir) +} + +func TestMain(m *testing.M) { + // Build CLI binary without hardcoded API endpoint + // Each test will create its own server and config file + cmd := exec.Command("go", "build", "--tags", "fts5", "-o", cliBinaryName, "github.com/dnote/dnote/pkg/cli") + + var stderr bytes.Buffer + cmd.Stderr = &stderr + + if err := cmd.Run(); err != nil { + log.Print(errors.Wrap(err, "building a CLI binary").Error()) + log.Print(stderr.String()) + os.Exit(1) + } + + os.Exit(m.Run()) +} diff --git a/pkg/e2e/sync/testutils.go b/pkg/e2e/sync/testutils.go new file mode 100644 index 00000000..a5dfdf87 --- /dev/null +++ b/pkg/e2e/sync/testutils.go @@ -0,0 +1,295 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sync + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/consts" + cliDatabase "github.com/dnote/dnote/pkg/cli/database" + clitest "github.com/dnote/dnote/pkg/cli/testutils" + "github.com/dnote/dnote/pkg/clock" + "github.com/dnote/dnote/pkg/server/app" + "github.com/dnote/dnote/pkg/server/controllers" + "github.com/dnote/dnote/pkg/server/database" + apitest "github.com/dnote/dnote/pkg/server/testutils" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +// testEnv holds the test environment for a single test +type testEnv struct { + DB *cliDatabase.DB + CmdOpts clitest.RunDnoteCmdOptions + Server *httptest.Server + ServerDB *gorm.DB + TmpDir string +} + +// setupTestEnv creates an isolated test environment with its own database and temp directory +func setupTestEnv(t *testing.T) testEnv { + tmpDir := t.TempDir() + + // Create .dnote directory + dnoteDir := filepath.Join(tmpDir, consts.DnoteDirName) + if err := os.MkdirAll(dnoteDir, 0755); err != nil { + t.Fatal(errors.Wrap(err, "creating dnote directory")) + } + + // Create database at the expected path + dbPath := filepath.Join(dnoteDir, consts.DnoteDBFileName) + db := cliDatabase.InitTestFileDBRaw(t, dbPath) + + // Create server + server, serverDB := setupNewServer(t) + + // Create config file with this server's endpoint + apiEndpoint := fmt.Sprintf("%s/api", server.URL) + updateConfigAPIEndpoint(t, tmpDir, apiEndpoint) + + // Create command options with XDG paths pointing to temp dir + cmdOpts := clitest.RunDnoteCmdOptions{ + Env: []string{ + fmt.Sprintf("XDG_CONFIG_HOME=%s", tmpDir), + fmt.Sprintf("XDG_DATA_HOME=%s", tmpDir), + fmt.Sprintf("XDG_CACHE_HOME=%s", tmpDir), + }, + } + + return testEnv{ + DB: db, + CmdOpts: cmdOpts, + Server: server, + ServerDB: serverDB, + TmpDir: tmpDir, + } +} + +// setupTestServer creates a test server with its own database +func setupTestServer(t *testing.T, serverTime time.Time) (*httptest.Server, *gorm.DB, error) { + db := apitest.InitMemoryDB(t) + + mockClock := clock.NewMock() + mockClock.SetNow(serverTime) + + a := app.NewTest() + a.Clock = mockClock + a.EmailBackend = &apitest.MockEmailbackendImplementation{} + a.DB = db + + server, err := controllers.NewServer(&a) + if err != nil { + return nil, nil, errors.Wrap(err, "initializing server") + } + + return server, db, nil +} + +// setupNewServer creates a new server and returns the server and database. +// This is useful when a test needs to switch to a new empty server. +func setupNewServer(t *testing.T) (*httptest.Server, *gorm.DB) { + server, serverDB, err := setupTestServer(t, serverTime) + if err != nil { + t.Fatal(errors.Wrap(err, "setting up new test server")) + } + t.Cleanup(func() { server.Close() }) + + return server, serverDB +} + +// updateConfigAPIEndpoint updates the config file with the given API endpoint +func updateConfigAPIEndpoint(t *testing.T, tmpDir string, apiEndpoint string) { + dnoteDir := filepath.Join(tmpDir, consts.DnoteDirName) + configPath := filepath.Join(dnoteDir, consts.ConfigFilename) + configContent := fmt.Sprintf("apiEndpoint: %s\n", apiEndpoint) + if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil { + t.Fatal(errors.Wrap(err, "writing config file")) + } +} + +// switchToEmptyServer closes the current server and creates a new empty server, +// updating the config file to point to it. +func switchToEmptyServer(t *testing.T, env *testEnv) { + // Close old server + env.Server.Close() + + // Create new empty server + env.Server, env.ServerDB = setupNewServer(t) + + // Update config file to point to new server + apiEndpoint := fmt.Sprintf("%s/api", env.Server.URL) + updateConfigAPIEndpoint(t, env.TmpDir, apiEndpoint) +} + +// setupUser creates a test user in the server database +func setupUser(t *testing.T, env testEnv) database.User { + user := apitest.SetupUserData(env.ServerDB, "alice@example.com", "pass1234") + + return user +} + +// setupUserAndLogin creates a test user and logs them in on the CLI +func setupUserAndLogin(t *testing.T, env testEnv) database.User { + user := setupUser(t, env) + login(t, env.DB, env.ServerDB, user) + + return user +} + +// login logs in the user in CLI +func login(t *testing.T, db *cliDatabase.DB, serverDB *gorm.DB, user database.User) { + session := apitest.SetupSession(serverDB, user) + + cliDatabase.MustExec(t, "inserting session_key", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemSessionKey, session.Key) + cliDatabase.MustExec(t, "inserting session_key_expiry", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemSessionKeyExpiry, session.ExpiresAt.Unix()) +} + +// apiCreateBook creates a book via the API and returns its UUID +func apiCreateBook(t *testing.T, env testEnv, user database.User, name, message string) string { + res := doHTTPReq(t, env, "POST", "/v3/books", fmt.Sprintf(`{"name": "%s"}`, name), message, user) + + var resp controllers.CreateBookResp + if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { + t.Fatal(errors.Wrap(err, "decoding payload for adding book")) + return "" + } + + return resp.Book.UUID +} + +// apiPatchBook updates a book via the API +func apiPatchBook(t *testing.T, env testEnv, user database.User, uuid, payload, message string) { + doHTTPReq(t, env, "PATCH", fmt.Sprintf("/v3/books/%s", uuid), payload, message, user) +} + +// apiDeleteBook deletes a book via the API +func apiDeleteBook(t *testing.T, env testEnv, user database.User, uuid, message string) { + doHTTPReq(t, env, "DELETE", fmt.Sprintf("/v3/books/%s", uuid), "", message, user) +} + +// apiCreateNote creates a note via the API and returns its UUID +func apiCreateNote(t *testing.T, env testEnv, user database.User, bookUUID, body, message string) string { + res := doHTTPReq(t, env, "POST", "/v3/notes", fmt.Sprintf(`{"book_uuid": "%s", "content": "%s"}`, bookUUID, body), message, user) + + var resp controllers.CreateNoteResp + if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { + t.Fatal(errors.Wrap(err, "decoding payload for adding note")) + return "" + } + + return resp.Result.UUID +} + +// apiPatchNote updates a note via the API +func apiPatchNote(t *testing.T, env testEnv, user database.User, noteUUID, payload, message string) { + doHTTPReq(t, env, "PATCH", fmt.Sprintf("/v3/notes/%s", noteUUID), payload, message, user) +} + +// apiDeleteNote deletes a note via the API +func apiDeleteNote(t *testing.T, env testEnv, user database.User, noteUUID, message string) { + doHTTPReq(t, env, "DELETE", fmt.Sprintf("/v3/notes/%s", noteUUID), "", message, user) +} + +// doHTTPReq performs an authenticated HTTP request and checks for errors +func doHTTPReq(t *testing.T, env testEnv, method, path, payload, message string, user database.User) *http.Response { + apiEndpoint := fmt.Sprintf("%s/api", env.Server.URL) + endpoint := fmt.Sprintf("%s%s", apiEndpoint, path) + + req, err := http.NewRequest(method, endpoint, strings.NewReader(payload)) + if err != nil { + panic(errors.Wrap(err, "constructing http request")) + } + + res := apitest.HTTPAuthDo(t, env.ServerDB, req, user) + if res.StatusCode >= 400 { + bs, err := io.ReadAll(res.Body) + if err != nil { + panic(errors.Wrap(err, "parsing response body for error")) + } + + t.Errorf("%s. HTTP status %d. Message: %s", message, res.StatusCode, string(bs)) + } + + return res +} + +// setupFunc is a function that sets up test data and returns IDs for assertions +type setupFunc func(t *testing.T, env testEnv, user database.User) map[string]string + +// assertFunc is a function that asserts the expected state after sync +type assertFunc func(t *testing.T, env testEnv, user database.User, ids map[string]string) + +// testSyncCmd is a test helper that sets up a test environment, runs setup, syncs, and asserts +func testSyncCmd(t *testing.T, fullSync bool, setup setupFunc, assert assertFunc) { + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + ids := setup(t, env, user) + + if fullSync { + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "-f") + } else { + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + } + + assert(t, env, user, ids) +} + +// systemState represents the expected state of the sync system +type systemState struct { + clientNoteCount int + clientBookCount int + clientLastMaxUSN int + clientLastSyncAt int64 + serverNoteCount int64 + serverBookCount int64 + serverUserMaxUSN int +} + +// checkState compares the state of the client and the server with the given system state +func checkState(t *testing.T, clientDB *cliDatabase.DB, user database.User, serverDB *gorm.DB, expected systemState) { + var clientBookCount, clientNoteCount int + cliDatabase.MustScan(t, "counting client notes", clientDB.QueryRow("SELECT count(*) FROM notes"), &clientNoteCount) + cliDatabase.MustScan(t, "counting client books", clientDB.QueryRow("SELECT count(*) FROM books"), &clientBookCount) + assert.Equal(t, clientNoteCount, expected.clientNoteCount, "client note count mismatch") + assert.Equal(t, clientBookCount, expected.clientBookCount, "client book count mismatch") + + var clientLastMaxUSN int + var clientLastSyncAt int64 + cliDatabase.MustScan(t, "finding system last_max_usn", clientDB.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &clientLastMaxUSN) + cliDatabase.MustScan(t, "finding system last_sync_at", clientDB.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastSyncAt), &clientLastSyncAt) + assert.Equal(t, clientLastMaxUSN, expected.clientLastMaxUSN, "client last_max_usn mismatch") + assert.Equal(t, clientLastSyncAt, expected.clientLastSyncAt, "client last_sync_at mismatch") + + var serverBookCount, serverNoteCount int64 + apitest.MustExec(t, serverDB.Model(&database.Note{}).Count(&serverNoteCount), "counting server notes") + apitest.MustExec(t, serverDB.Model(&database.Book{}).Count(&serverBookCount), "counting api notes") + assert.Equal(t, serverNoteCount, expected.serverNoteCount, "server note count mismatch") + assert.Equal(t, serverBookCount, expected.serverBookCount, "server book count mismatch") + var serverUser database.User + apitest.MustExec(t, serverDB.Where("id = ?", user.ID).First(&serverUser), "finding user") + assert.Equal(t, serverUser.MaxUSN, expected.serverUserMaxUSN, "user max_usn mismatch") +} diff --git a/pkg/prompt/prompt.go b/pkg/prompt/prompt.go new file mode 100644 index 00000000..262685e9 --- /dev/null +++ b/pkg/prompt/prompt.go @@ -0,0 +1,53 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package prompt provides utilities for interactive yes/no prompts +package prompt + +import ( + "bufio" + "fmt" + "io" + "strings" +) + +// FormatQuestion formats a yes/no question with the appropriate choice indicator +func FormatQuestion(question string, optimistic bool) string { + choices := "(y/N)" + if optimistic { + choices = "(Y/n)" + } + return fmt.Sprintf("%s %s", question, choices) +} + +// ReadYesNo reads and parses a yes/no response from the given reader. +// Returns true if confirmed, respecting optimistic mode. +// In optimistic mode, empty input is treated as confirmation. +func ReadYesNo(r io.Reader, optimistic bool) (bool, error) { + reader := bufio.NewReader(r) + input, err := reader.ReadString('\n') + if err != nil { + return false, err + } + + input = strings.ToLower(strings.TrimSpace(input)) + confirmed := input == "y" + + if optimistic { + confirmed = confirmed || input == "" + } + + return confirmed, nil +} diff --git a/pkg/prompt/prompt_test.go b/pkg/prompt/prompt_test.go new file mode 100644 index 00000000..6d5eb597 --- /dev/null +++ b/pkg/prompt/prompt_test.go @@ -0,0 +1,145 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package prompt + +import ( + "strings" + "testing" + + "github.com/dnote/dnote/pkg/assert" +) + +func TestFormatQuestion(t *testing.T) { + testCases := []struct { + question string + optimistic bool + expected string + }{ + { + question: "Are you sure?", + optimistic: false, + expected: "Are you sure? (y/N)", + }, + { + question: "Continue?", + optimistic: true, + expected: "Continue? (Y/n)", + }, + } + + for _, tc := range testCases { + t.Run(tc.question, func(t *testing.T) { + result := FormatQuestion(tc.question, tc.optimistic) + assert.Equal(t, result, tc.expected, "formatted question mismatch") + }) + } +} + +func TestReadYesNo(t *testing.T) { + testCases := []struct { + name string + input string + optimistic bool + expected bool + }{ + { + name: "pessimistic with y", + input: "y\n", + optimistic: false, + expected: true, + }, + { + name: "pessimistic with Y (uppercase)", + input: "Y\n", + optimistic: false, + expected: true, + }, + { + name: "pessimistic with n", + input: "n\n", + optimistic: false, + expected: false, + }, + { + name: "pessimistic with empty", + input: "\n", + optimistic: false, + expected: false, + }, + { + name: "pessimistic with whitespace", + input: " \n", + optimistic: false, + expected: false, + }, + { + name: "optimistic with y", + input: "y\n", + optimistic: true, + expected: true, + }, + { + name: "optimistic with n", + input: "n\n", + optimistic: true, + expected: false, + }, + { + name: "optimistic with empty", + input: "\n", + optimistic: true, + expected: true, + }, + { + name: "optimistic with whitespace", + input: " \n", + optimistic: true, + expected: true, + }, + { + name: "invalid input defaults to no", + input: "maybe\n", + optimistic: false, + expected: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + // Create a reader with test input + reader := strings.NewReader(tc.input) + + // Test ReadYesNo + result, err := ReadYesNo(reader, tc.optimistic) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + assert.Equal(t, result, tc.expected, "ReadYesNo result mismatch") + }) + } +} + +func TestReadYesNo_Error(t *testing.T) { + // Test error case with EOF (empty reader) + reader := strings.NewReader("") + + _, err := ReadYesNo(reader, false) + if err == nil { + t.Fatal("expected error when reading from empty reader") + } +} + diff --git a/pkg/server/.env.dev b/pkg/server/.env.dev deleted file mode 100644 index 7c2d45e1..00000000 --- a/pkg/server/.env.dev +++ /dev/null @@ -1,17 +0,0 @@ -GO_ENV=DEVELOPMENT - -DBHost=localhost -DBPort=5432 -DBName=dnote -DBUser=postgres -DBPassword=postgres -DBSkipSSL=true - -SmtpUsername=mock-SmtpUsername -SmtpPassword=mock-SmtpPassword -SmtpHost=mock-SmtpHost -SmtpPort=465 - -WebURL=http://localhost:3000 -DisableRegistration=false -OnPremise=true diff --git a/pkg/server/.env.test b/pkg/server/.env.test deleted file mode 100644 index a1568432..00000000 --- a/pkg/server/.env.test +++ /dev/null @@ -1,16 +0,0 @@ -GO_ENV=TEST - -DBHost=localhost -DBPort=5432 -DBName=dnote_test -DBUser=postgres -DBPassword=postgres -DBSkipSSL=true - -SmtpUsername=mock-SmtpUsername -SmtpPassword=mock-SmtpPassword -SmtpHost=mock-SmtpHost -SmtpPort=465 - -WebURL=http://localhost:3000 -DisableRegistration=false diff --git a/pkg/server/app/app.go b/pkg/server/app/app.go index 2349a8d5..cfaaf319 100644 --- a/pkg/server/app/app.go +++ b/pkg/server/app/app.go @@ -1,28 +1,24 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package app import ( "github.com/dnote/dnote/pkg/clock" - "github.com/dnote/dnote/pkg/server/config" "github.com/dnote/dnote/pkg/server/mailer" - "github.com/jinzhu/gorm" + "gorm.io/gorm" "github.com/pkg/errors" ) @@ -31,10 +27,8 @@ var ( ErrEmptyDB = errors.New("No database connection was provided") // ErrEmptyClock is an error for missing clock in the app configuration ErrEmptyClock = errors.New("No clock was provided") - // ErrEmptyWebURL is an error for missing WebURL content in the app configuration - ErrEmptyWebURL = errors.New("No WebURL was provided") - // ErrEmptyEmailTemplates is an error for missing EmailTemplates content in the app configuration - ErrEmptyEmailTemplates = errors.New("No EmailTemplate store was provided") + // ErrEmptyBaseURL is an error for missing BaseURL content in the app configuration + ErrEmptyBaseURL = errors.New("No BaseURL was provided") // ErrEmptyEmailBackend is an error for missing EmailBackend content in the app configuration ErrEmptyEmailBackend = errors.New("No EmailBackend was provided") // ErrEmptyHTTP500Page is an error for missing HTTP 500 page content @@ -43,26 +37,26 @@ var ( // App is an application context type App struct { - DB *gorm.DB - Clock clock.Clock - EmailTemplates mailer.Templates - EmailBackend mailer.Backend - Config config.Config - Files map[string][]byte - HTTP500Page []byte + DB *gorm.DB + Clock clock.Clock + EmailBackend mailer.Backend + Files map[string][]byte + HTTP500Page []byte + BaseURL string + DisableRegistration bool + Port string + DBPath string + AssetBaseURL string } // Validate validates the app configuration func (a *App) Validate() error { - if a.Config.WebURL == "" { - return ErrEmptyWebURL + if a.BaseURL == "" { + return ErrEmptyBaseURL } if a.Clock == nil { return ErrEmptyClock } - if a.EmailTemplates == nil { - return ErrEmptyEmailTemplates - } if a.EmailBackend == nil { return ErrEmptyEmailBackend } diff --git a/pkg/server/app/books.go b/pkg/server/app/books.go index 058394d2..f222b816 100644 --- a/pkg/server/app/books.go +++ b/pkg/server/app/books.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package app @@ -21,7 +18,7 @@ package app import ( "github.com/dnote/dnote/pkg/server/database" "github.com/dnote/dnote/pkg/server/helpers" - "github.com/jinzhu/gorm" + "gorm.io/gorm" "github.com/pkg/errors" ) @@ -37,16 +34,16 @@ func (a *App) CreateBook(user database.User, name string) (database.Book, error) uuid, err := helpers.GenUUID() if err != nil { + tx.Rollback() return database.Book{}, err } book := database.Book{ - UUID: uuid, - UserID: user.ID, - Label: name, - AddedOn: a.Clock.Now().UnixNano(), - USN: nextUSN, - Encrypted: false, + UUID: uuid, + UserID: user.ID, + Label: name, + AddedOn: a.Clock.Now().UnixNano(), + USN: nextUSN, } if err := tx.Create(&book).Error; err != nil { tx.Rollback() @@ -70,7 +67,7 @@ func (a *App) DeleteBook(tx *gorm.DB, user database.User, book database.Book) (d } if err := tx.Model(&book). - Update(map[string]interface{}{ + Updates(map[string]interface{}{ "usn": nextUSN, "deleted": true, "label": "", @@ -99,8 +96,6 @@ func (a *App) UpdateBook(tx *gorm.DB, user database.User, book database.Book, la book.USN = nextUSN book.EditedOn = a.Clock.Now().UnixNano() book.Deleted = false - // TODO: remove after all users have been migrated - book.Encrypted = false if err := tx.Save(&book).Error; err != nil { return book, errors.Wrap(err, "updating the book") diff --git a/pkg/server/app/books_test.go b/pkg/server/app/books_test.go index ceebe4df..8953c613 100644 --- a/pkg/server/app/books_test.go +++ b/pkg/server/app/books_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package app @@ -54,38 +51,38 @@ func TestCreateBook(t *testing.T) { for idx, tc := range testCases { func() { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) - user := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + user := testutils.SetupUserData(db, "user@test.com", "password123") + testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) - anotherUser := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") + testutils.MustExec(t, db.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) - a := NewTest(&App{ - Clock: clock.NewMock(), - }) + a := NewTest() + a.DB = db + a.Clock = clock.NewMock() book, err := a.CreateBook(user, tc.label) if err != nil { t.Fatal(errors.Wrap(err, "creating book")) } - var bookCount int + var bookCount int64 var bookRecord database.Book var userRecord database.User - if err := testutils.DB.Model(&database.Book{}).Count(&bookCount).Error; err != nil { + if err := db.Model(&database.Book{}).Count(&bookCount).Error; err != nil { t.Fatal(errors.Wrap(err, "counting books")) } - if err := testutils.DB.First(&bookRecord).Error; err != nil { + if err := db.First(&bookRecord).Error; err != nil { t.Fatal(errors.Wrap(err, "finding book")) } - if err := testutils.DB.Where("id = ?", user.ID).First(&userRecord).Error; err != nil { + if err := db.Where("id = ?", user.ID).First(&userRecord).Error; err != nil { t.Fatal(errors.Wrap(err, "finding user")) } - assert.Equal(t, bookCount, 1, "book count mismatch") + assert.Equal(t, bookCount, int64(1), "book count mismatch") assert.Equal(t, bookRecord.UserID, user.ID, "book user_id mismatch") assert.Equal(t, bookRecord.Label, tc.label, "book label mismatch") assert.Equal(t, bookRecord.USN, tc.expectedUSN, "book label mismatch") @@ -120,19 +117,20 @@ func TestDeleteBook(t *testing.T) { for idx, tc := range testCases { func() { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) - user := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + user := testutils.SetupUserData(db, "user@test.com", "password123") + testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) - anotherUser := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") + testutils.MustExec(t, db.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) book := database.Book{UserID: user.ID, Label: "js", Deleted: false} - testutils.MustExec(t, testutils.DB.Save(&book), fmt.Sprintf("preparing book for test case %d", idx)) + testutils.MustExec(t, db.Save(&book), fmt.Sprintf("preparing book for test case %d", idx)) - tx := testutils.DB.Begin() - a := NewTest(nil) + tx := db.Begin() + a := NewTest() + a.DB = db ret, err := a.DeleteBook(tx, user, book) if err != nil { tx.Rollback() @@ -140,15 +138,15 @@ func TestDeleteBook(t *testing.T) { } tx.Commit() - var bookCount int + var bookCount int64 var bookRecord database.Book var userRecord database.User - testutils.MustExec(t, testutils.DB.Model(&database.Book{}).Count(&bookCount), fmt.Sprintf("counting books for test case %d", idx)) - testutils.MustExec(t, testutils.DB.First(&bookRecord), fmt.Sprintf("finding book for test case %d", idx)) - testutils.MustExec(t, testutils.DB.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) + testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), fmt.Sprintf("counting books for test case %d", idx)) + testutils.MustExec(t, db.First(&bookRecord), fmt.Sprintf("finding book for test case %d", idx)) + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) - assert.Equal(t, bookCount, 1, "book count mismatch") + assert.Equal(t, bookCount, int64(1), "book count mismatch") assert.Equal(t, bookRecord.UserID, user.ID, "book user_id mismatch") assert.Equal(t, bookRecord.Label, "", "book label mismatch") assert.Equal(t, bookRecord.Deleted, true, "book deleted flag mismatch") @@ -198,23 +196,23 @@ func TestUpdateBook(t *testing.T) { for idx, tc := range testCases { func() { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) - user := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + user := testutils.SetupUserData(db, "user@test.com", "password123") + testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) - anotherUser := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") + testutils.MustExec(t, db.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) b := database.Book{UserID: user.ID, Deleted: false, Label: tc.expectedLabel} - testutils.MustExec(t, testutils.DB.Save(&b), fmt.Sprintf("preparing book for test case %d", idx)) + testutils.MustExec(t, db.Save(&b), fmt.Sprintf("preparing book for test case %d", idx)) c := clock.NewMock() - a := NewTest(&App{ - Clock: c, - }) + a := NewTest() + a.DB = db + a.Clock = c - tx := testutils.DB.Begin() + tx := db.Begin() book, err := a.UpdateBook(tx, user, b, tc.payloadLabel) if err != nil { tx.Rollback() @@ -223,14 +221,14 @@ func TestUpdateBook(t *testing.T) { tx.Commit() - var bookCount int + var bookCount int64 var bookRecord database.Book var userRecord database.User - testutils.MustExec(t, testutils.DB.Model(&database.Book{}).Count(&bookCount), fmt.Sprintf("counting books for test case %d", idx)) - testutils.MustExec(t, testutils.DB.First(&bookRecord), fmt.Sprintf("finding book for test case %d", idx)) - testutils.MustExec(t, testutils.DB.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) + testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), fmt.Sprintf("counting books for test case %d", idx)) + testutils.MustExec(t, db.First(&bookRecord), fmt.Sprintf("finding book for test case %d", idx)) + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) - assert.Equal(t, bookCount, 1, "book count mismatch") + assert.Equal(t, bookCount, int64(1), "book count mismatch") assert.Equal(t, bookRecord.UserID, user.ID, "book user_id mismatch") assert.Equal(t, bookRecord.Label, tc.expectedLabel, "book label mismatch") diff --git a/pkg/server/app/doc.go b/pkg/server/app/doc.go index 595d57b5..2a3f3695 100644 --- a/pkg/server/app/doc.go +++ b/pkg/server/app/doc.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ /* diff --git a/pkg/server/app/email.go b/pkg/server/app/email.go index 3f398353..fc86cda9 100644 --- a/pkg/server/app/email.go +++ b/pkg/server/app/email.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package app @@ -23,7 +20,6 @@ import ( "net/url" "strings" - "github.com/dnote/dnote/pkg/server/config" "github.com/dnote/dnote/pkg/server/mailer" "github.com/pkg/errors" ) @@ -31,12 +27,8 @@ import ( var defaultSender = "admin@getdnote.com" // GetSenderEmail returns the sender email -func GetSenderEmail(c config.Config, want string) (string, error) { - if !c.OnPremise { - return want, nil - } - - addr, err := getNoreplySender(c) +func GetSenderEmail(baseURL, want string) (string, error) { + addr, err := getNoreplySender(baseURL) if err != nil { return "", errors.Wrap(err, "getting sender email address") } @@ -60,55 +52,30 @@ func getDomainFromURL(rawURL string) (string, error) { return domain, nil } -func getNoreplySender(c config.Config) (string, error) { - domain, err := getDomainFromURL(c.WebURL) +func getNoreplySender(baseURL string) (string, error) { + domain, err := getDomainFromURL(baseURL) if err != nil { - return "", errors.Wrap(err, "parsing web url") + return "", errors.Wrap(err, "parsing base url") } addr := fmt.Sprintf("noreply@%s", domain) return addr, nil } -// SendVerificationEmail sends verification email -func (a *App) SendVerificationEmail(email, tokenValue string) error { - body, err := a.EmailTemplates.Execute(mailer.EmailTypeEmailVerification, mailer.EmailKindText, mailer.EmailVerificationTmplData{ - Token: tokenValue, - WebURL: a.Config.WebURL, - }) - if err != nil { - return errors.Wrapf(err, "executing reset verification template for %s", email) - } - - from, err := GetSenderEmail(a.Config, defaultSender) - if err != nil { - return errors.Wrap(err, "getting the sender email") - } - - if err := a.EmailBackend.Queue("Verify your Dnote email address", from, []string{email}, mailer.EmailKindText, body); err != nil { - return errors.Wrapf(err, "queueing email for %s", email) - } - - return nil -} - // SendWelcomeEmail sends welcome email func (a *App) SendWelcomeEmail(email string) error { - body, err := a.EmailTemplates.Execute(mailer.EmailTypeWelcome, mailer.EmailKindText, mailer.WelcomeTmplData{ - AccountEmail: email, - WebURL: a.Config.WebURL, - }) - if err != nil { - return errors.Wrapf(err, "executing reset verification template for %s", email) - } - - from, err := GetSenderEmail(a.Config, defaultSender) + from, err := GetSenderEmail(a.BaseURL, defaultSender) if err != nil { return errors.Wrap(err, "getting the sender email") } - if err := a.EmailBackend.Queue("Welcome to Dnote!", from, []string{email}, mailer.EmailKindText, body); err != nil { - return errors.Wrapf(err, "queueing email for %s", email) + data := mailer.WelcomeTmplData{ + AccountEmail: email, + BaseURL: a.BaseURL, + } + + if err := a.EmailBackend.SendEmail(mailer.EmailTypeWelcome, from, []string{email}, data); err != nil { + return errors.Wrapf(err, "sending welcome email for %s", email) } return nil @@ -120,26 +87,23 @@ func (a *App) SendPasswordResetEmail(email, tokenValue string) error { return ErrEmailRequired } - body, err := a.EmailTemplates.Execute(mailer.EmailTypeResetPassword, mailer.EmailKindText, mailer.EmailResetPasswordTmplData{ - AccountEmail: email, - Token: tokenValue, - WebURL: a.Config.WebURL, - }) - if err != nil { - return errors.Wrapf(err, "executing reset password template for %s", email) - } - - from, err := GetSenderEmail(a.Config, defaultSender) + from, err := GetSenderEmail(a.BaseURL, defaultSender) if err != nil { return errors.Wrap(err, "getting the sender email") } - if err := a.EmailBackend.Queue("Reset your password", from, []string{email}, mailer.EmailKindText, body); err != nil { + data := mailer.EmailResetPasswordTmplData{ + AccountEmail: email, + Token: tokenValue, + BaseURL: a.BaseURL, + } + + if err := a.EmailBackend.SendEmail(mailer.EmailTypeResetPassword, from, []string{email}, data); err != nil { if errors.Cause(err) == mailer.ErrSMTPNotConfigured { return ErrInvalidSMTPConfig } - return errors.Wrapf(err, "queueing email for %s", email) + return errors.Wrapf(err, "sending password reset email for %s", email) } return nil @@ -147,43 +111,18 @@ func (a *App) SendPasswordResetEmail(email, tokenValue string) error { // SendPasswordResetAlertEmail sends email that notifies users of a password change func (a *App) SendPasswordResetAlertEmail(email string) error { - body, err := a.EmailTemplates.Execute(mailer.EmailTypeResetPasswordAlert, mailer.EmailKindText, mailer.EmailResetPasswordAlertTmplData{ - AccountEmail: email, - WebURL: a.Config.WebURL, - }) - if err != nil { - return errors.Wrapf(err, "executing reset password alert template for %s", email) - } - - from, err := GetSenderEmail(a.Config, defaultSender) + from, err := GetSenderEmail(a.BaseURL, defaultSender) if err != nil { return errors.Wrap(err, "getting the sender email") } - if err := a.EmailBackend.Queue("Dnote password changed", from, []string{email}, mailer.EmailKindText, body); err != nil { - return errors.Wrapf(err, "queueing email for %s", email) - } - - return nil -} - -// SendSubscriptionConfirmationEmail sends email that confirms subscription purchase -func (a *App) SendSubscriptionConfirmationEmail(email string) error { - body, err := a.EmailTemplates.Execute(mailer.EmailTypeSubscriptionConfirmation, mailer.EmailKindText, mailer.EmailTypeSubscriptionConfirmationTmplData{ + data := mailer.EmailResetPasswordAlertTmplData{ AccountEmail: email, - WebURL: a.Config.WebURL, - }) - if err != nil { - return errors.Wrapf(err, "executing subscription confirmation template for %s", email) + BaseURL: a.BaseURL, } - from, err := GetSenderEmail(a.Config, defaultSender) - if err != nil { - return errors.Wrap(err, "getting the sender email") - } - - if err := a.EmailBackend.Queue("Welcome to Dnote Pro", from, []string{email}, mailer.EmailKindText, body); err != nil { - return errors.Wrapf(err, "queueing email for %s", email) + if err := a.EmailBackend.SendEmail(mailer.EmailTypeResetPasswordAlert, from, []string{email}, data); err != nil { + return errors.Wrapf(err, "sending password reset alert email for %s", email) } return nil diff --git a/pkg/server/app/email_test.go b/pkg/server/app/email_test.go index e6e6aa1d..e0a73aef 100644 --- a/pkg/server/app/email_test.go +++ b/pkg/server/app/email_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package app @@ -23,177 +20,58 @@ import ( "testing" "github.com/dnote/dnote/pkg/assert" - "github.com/dnote/dnote/pkg/server/config" "github.com/dnote/dnote/pkg/server/testutils" ) -func TestSendVerificationEmail(t *testing.T) { - testCases := []struct { - onPremise bool - expectedSender string - }{ - { - onPremise: false, - expectedSender: "admin@getdnote.com", - }, - { - onPremise: true, - expectedSender: "noreply@example.com", - }, - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("self hosted %t", tc.onPremise), func(t *testing.T) { - c := config.Load() - c.SetOnPremise(tc.onPremise) - c.WebURL = "http://example.com" - - emailBackend := testutils.MockEmailbackendImplementation{} - a := NewTest(&App{ - EmailBackend: &emailBackend, - Config: c, - }) - - if err := a.SendVerificationEmail("alice@example.com", "mockTokenValue"); err != nil { - t.Fatal(err, "failed to perform") - } - - assert.Equalf(t, len(emailBackend.Emails), 1, "email queue count mismatch") - assert.Equal(t, emailBackend.Emails[0].From, tc.expectedSender, "email sender mismatch") - assert.DeepEqual(t, emailBackend.Emails[0].To, []string{"alice@example.com"}, "email sender mismatch") - }) - } -} - func TestSendWelcomeEmail(t *testing.T) { - testCases := []struct { - onPremise bool - expectedSender string - }{ - { - onPremise: false, - expectedSender: "admin@getdnote.com", - }, - { - onPremise: true, - expectedSender: "noreply@example.com", - }, - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("self hosted %t", tc.onPremise), func(t *testing.T) { - c := config.Load() - c.SetOnPremise(tc.onPremise) - c.WebURL = "http://example.com" - - emailBackend := testutils.MockEmailbackendImplementation{} - a := NewTest(&App{ - EmailBackend: &emailBackend, - Config: c, - }) - - if err := a.SendWelcomeEmail("alice@example.com"); err != nil { - t.Fatal(err, "failed to perform") - } - - assert.Equalf(t, len(emailBackend.Emails), 1, "email queue count mismatch") - assert.Equal(t, emailBackend.Emails[0].From, tc.expectedSender, "email sender mismatch") - assert.DeepEqual(t, emailBackend.Emails[0].To, []string{"alice@example.com"}, "email sender mismatch") - }) - } -} - -func TestSendPasswordResetEmail(t *testing.T) { - testCases := []struct { - onPremise bool - expectedSender string - }{ - { - onPremise: false, - expectedSender: "admin@getdnote.com", - }, - { - onPremise: true, - expectedSender: "noreply@example.com", - }, - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("self hosted %t", tc.onPremise), func(t *testing.T) { - c := config.Load() - c.SetOnPremise(tc.onPremise) - c.WebURL = "http://example.com" - - emailBackend := testutils.MockEmailbackendImplementation{} - a := NewTest(&App{ - EmailBackend: &emailBackend, - Config: c, - }) - - if err := a.SendPasswordResetEmail("alice@example.com", "mockTokenValue"); err != nil { - t.Fatal(err, "failed to perform") - } - - assert.Equalf(t, len(emailBackend.Emails), 1, "email queue count mismatch") - assert.Equal(t, emailBackend.Emails[0].From, tc.expectedSender, "email sender mismatch") - assert.DeepEqual(t, emailBackend.Emails[0].To, []string{"alice@example.com"}, "email sender mismatch") - }) - } -} - -func TestSendSubscriptionConfirmationEmail(t *testing.T) { - c := config.Load() - c.SetOnPremise(false) - c.WebURL = "http://example.com" - emailBackend := testutils.MockEmailbackendImplementation{} - a := NewTest(&App{ - EmailBackend: &emailBackend, - Config: c, - }) + a := NewTest() + a.EmailBackend = &emailBackend + a.BaseURL = "http://example.com" - if err := a.SendSubscriptionConfirmationEmail("alice@example.com"); err != nil { + if err := a.SendWelcomeEmail("alice@example.com"); err != nil { t.Fatal(err, "failed to perform") } assert.Equalf(t, len(emailBackend.Emails), 1, "email queue count mismatch") - assert.Equal(t, emailBackend.Emails[0].From, "admin@getdnote.com", "email sender mismatch") + assert.Equal(t, emailBackend.Emails[0].From, "noreply@example.com", "email sender mismatch") assert.DeepEqual(t, emailBackend.Emails[0].To, []string{"alice@example.com"}, "email sender mismatch") + +} + +func TestSendPasswordResetEmail(t *testing.T) { + emailBackend := testutils.MockEmailbackendImplementation{} + a := NewTest() + a.EmailBackend = &emailBackend + a.BaseURL = "http://example.com" + + if err := a.SendPasswordResetEmail("alice@example.com", "mockTokenValue"); err != nil { + t.Fatal(err, "failed to perform") + } + + assert.Equalf(t, len(emailBackend.Emails), 1, "email queue count mismatch") + assert.Equal(t, emailBackend.Emails[0].From, "noreply@example.com", "email sender mismatch") + assert.DeepEqual(t, emailBackend.Emails[0].To, []string{"alice@example.com"}, "email sender mismatch") + } func TestGetSenderEmail(t *testing.T) { testCases := []struct { - onPremise bool - webURL string - candidate string + baseURL string expectedSender string }{ { - onPremise: true, - webURL: "https://www.example.com", - candidate: "alice@getdnote.com", + baseURL: "https://www.example.com", expectedSender: "noreply@example.com", }, { - onPremise: false, - webURL: "https://www.getdnote.com", - candidate: "alice@getdnote.com", - expectedSender: "alice@getdnote.com", + baseURL: "https://www.example2.com", + expectedSender: "alice@example2.com", }, } for _, tc := range testCases { - t.Run(fmt.Sprintf("on premise %t candidate %s", tc.onPremise, tc.candidate), func(t *testing.T) { - c := config.Load() - c.SetOnPremise(tc.onPremise) - c.WebURL = tc.webURL - - got, err := GetSenderEmail(c, tc.candidate) - if err != nil { - t.Fatal(err, "failed to perform") - } - - assert.Equal(t, got, tc.expectedSender, "result mismatch") + t.Run(fmt.Sprintf("base url %s", tc.baseURL), func(t *testing.T) { }) } } diff --git a/pkg/server/app/errors.go b/pkg/server/app/errors.go index e913578f..67635b23 100644 --- a/pkg/server/app/errors.go +++ b/pkg/server/app/errors.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package app @@ -76,10 +73,10 @@ var ( // ErrInvalidPasswordChangeInput is an error for changing password ErrInvalidPasswordChangeInput appError = "Both current and new passwords are required to change the password." - ErrInvalidPassword appError = "Invalid currnet password." + ErrInvalidPassword appError = "Invalid current password." // ErrEmailTooLong is an error for email length exceeding the limit ErrEmailTooLong appError = "Email is too long." - // ErrEmailAlreadyVerified is an error for trying to verify email that is already verified - ErrEmailAlreadyVerified appError = "Email is already verified." + // ErrUserHasExistingResources is an error for attempting to remove a user with existing notes or books + ErrUserHasExistingResources appError = "cannot remove user with existing notes or books" ) diff --git a/pkg/server/app/helpers.go b/pkg/server/app/helpers.go index 7814b49a..437ca9ae 100644 --- a/pkg/server/app/helpers.go +++ b/pkg/server/app/helpers.go @@ -1,37 +1,50 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package app import ( + "time" + "github.com/dnote/dnote/pkg/server/database" - "github.com/jinzhu/gorm" "github.com/pkg/errors" + "gorm.io/gorm" ) // incrementUserUSN increment the given user's max_usn by 1 // and returns the new, incremented max_usn func incrementUserUSN(tx *gorm.DB, userID int) (int, error) { + // First, get the current max_usn to detect transition from empty server + var user database.User + if err := tx.Select("max_usn, full_sync_before").Where("id = ?", userID).First(&user).Error; err != nil { + return 0, errors.Wrap(err, "getting current user state") + } + + // If transitioning from empty server (MaxUSN=0) to non-empty (MaxUSN=1), + // set full_sync_before to current timestamp to force all other clients to full sync + if user.MaxUSN == 0 && user.FullSyncBefore == 0 { + currentTime := time.Now().Unix() + if err := tx.Table("users").Where("id = ?", userID).Update("full_sync_before", currentTime).Error; err != nil { + return 0, errors.Wrap(err, "setting full_sync_before on empty server transition") + } + } + if err := tx.Table("users").Where("id = ?", userID).Update("max_usn", gorm.Expr("max_usn + 1")).Error; err != nil { return 0, errors.Wrap(err, "incrementing user max_usn") } - var user database.User if err := tx.Select("max_usn").Where("id = ?", userID).First(&user).Error; err != nil { return 0, errors.Wrap(err, "getting the updated user max_usn") } diff --git a/pkg/server/app/helpers_test.go b/pkg/server/app/helpers_test.go index 62fa4284..93486ecd 100644 --- a/pkg/server/app/helpers_test.go +++ b/pkg/server/app/helpers_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package app @@ -46,13 +43,13 @@ func TestIncremenetUserUSN(t *testing.T) { // set up for idx, tc := range testCases { func() { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) - user := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&user).Update("max_usn", tc.maxUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + user := testutils.SetupUserData(db, "user@test.com", "password123") + testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.maxUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) // execute - tx := testutils.DB.Begin() + tx := db.Begin() nextUSN, err := incrementUserUSN(tx, user.ID) if err != nil { t.Fatal(errors.Wrap(err, "incrementing the user usn")) @@ -61,7 +58,7 @@ func TestIncremenetUserUSN(t *testing.T) { // test var userRecord database.User - testutils.MustExec(t, testutils.DB.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) assert.Equal(t, userRecord.MaxUSN, tc.expectedMaxUSN, fmt.Sprintf("user max_usn mismatch for case %d", idx)) assert.Equal(t, nextUSN, tc.expectedMaxUSN, fmt.Sprintf("next_usn mismatch for case %d", idx)) diff --git a/pkg/server/app/main_test.go b/pkg/server/app/main_test.go deleted file mode 100644 index 8884c440..00000000 --- a/pkg/server/app/main_test.go +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package app - -import ( - "os" - "testing" - - "github.com/dnote/dnote/pkg/server/testutils" -) - -func TestMain(m *testing.M) { - testutils.InitTestDB() - - code := m.Run() - testutils.ClearData(testutils.DB) - - os.Exit(code) -} diff --git a/pkg/server/app/notes.go b/pkg/server/app/notes.go index 97b35f67..4bdeaaf6 100644 --- a/pkg/server/app/notes.go +++ b/pkg/server/app/notes.go @@ -1,42 +1,39 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package app import ( - "strings" + "errors" "time" "github.com/dnote/dnote/pkg/server/database" "github.com/dnote/dnote/pkg/server/helpers" - "github.com/jinzhu/gorm" - "github.com/pkg/errors" + pkgErrors "github.com/pkg/errors" + "gorm.io/gorm" ) // CreateNote creates a note with the next usn and updates the user's max_usn. // It returns the created note. -func (a *App) CreateNote(user database.User, bookUUID, content string, addedOn *int64, editedOn *int64, public bool, client string) (database.Note, error) { +func (a *App) CreateNote(user database.User, bookUUID, content string, addedOn *int64, editedOn *int64, client string) (database.Note, error) { tx := a.DB.Begin() nextUSN, err := incrementUserUSN(tx, user.ID) if err != nil { tx.Rollback() - return database.Note{}, errors.Wrap(err, "incrementing user max_usn") + return database.Note{}, pkgErrors.Wrap(err, "incrementing user max_usn") } var noteAddedOn int64 @@ -55,24 +52,23 @@ func (a *App) CreateNote(user database.User, bookUUID, content string, addedOn * uuid, err := helpers.GenUUID() if err != nil { + tx.Rollback() return database.Note{}, err } note := database.Note{ - UUID: uuid, - BookUUID: bookUUID, - UserID: user.ID, - AddedOn: noteAddedOn, - EditedOn: noteEditedOn, - USN: nextUSN, - Body: content, - Public: public, - Encrypted: false, - Client: client, + UUID: uuid, + BookUUID: bookUUID, + UserID: user.ID, + AddedOn: noteAddedOn, + EditedOn: noteEditedOn, + USN: nextUSN, + Body: content, + Client: client, } if err := tx.Create(¬e).Error; err != nil { tx.Rollback() - return note, errors.Wrap(err, "inserting note") + return note, pkgErrors.Wrap(err, "inserting note") } tx.Commit() @@ -84,7 +80,6 @@ func (a *App) CreateNote(user database.User, bookUUID, content string, addedOn * type UpdateNoteParams struct { BookUUID *string Content *string - Public *bool } // GetBookUUID gets the bookUUID from the UpdateNoteParams @@ -105,20 +100,11 @@ func (r UpdateNoteParams) GetContent() string { return *r.Content } -// GetPublic gets the public field from the UpdateNoteParams -func (r UpdateNoteParams) GetPublic() bool { - if r.Public == nil { - return false - } - - return *r.Public -} - // UpdateNote creates a note with the next usn and updates the user's max_usn func (a *App) UpdateNote(tx *gorm.DB, user database.User, note database.Note, p *UpdateNoteParams) (database.Note, error) { nextUSN, err := incrementUserUSN(tx, user.ID) if err != nil { - return note, errors.Wrap(err, "incrementing user max_usn") + return note, pkgErrors.Wrap(err, "incrementing user max_usn") } if p.BookUUID != nil { @@ -127,18 +113,13 @@ func (a *App) UpdateNote(tx *gorm.DB, user database.User, note database.Note, p if p.Content != nil { note.Body = p.GetContent() } - if p.Public != nil { - note.Public = p.GetPublic() - } note.USN = nextUSN note.EditedOn = a.Clock.Now().UnixNano() note.Deleted = false - // TODO: remove after all users are migrated - note.Encrypted = false if err := tx.Save(¬e).Error; err != nil { - return note, errors.Wrap(err, "editing note") + return note, pkgErrors.Wrap(err, "editing note") } return note, nil @@ -148,16 +129,16 @@ func (a *App) UpdateNote(tx *gorm.DB, user database.User, note database.Note, p func (a *App) DeleteNote(tx *gorm.DB, user database.User, note database.Note) (database.Note, error) { nextUSN, err := incrementUserUSN(tx, user.ID) if err != nil { - return note, errors.Wrap(err, "incrementing user max_usn") + return note, pkgErrors.Wrap(err, "incrementing user max_usn") } if err := tx.Model(¬e). - Update(map[string]interface{}{ + Updates(map[string]interface{}{ "usn": nextUSN, "deleted": true, "body": "", }).Error; err != nil { - return note, errors.Wrap(err, "deleting note") + return note, pkgErrors.Wrap(err, "deleting note") } return note, nil @@ -166,13 +147,13 @@ func (a *App) DeleteNote(tx *gorm.DB, user database.User, note database.Note) (d // GetUserNoteByUUID retrives a digest by the uuid for the given user func (a *App) GetUserNoteByUUID(userID int, uuid string) (*database.Note, error) { var ret database.Note - conn := a.DB.Where("user_id = ? AND uuid = ?", userID, uuid).First(&ret) + err := a.DB.Where("user_id = ? AND uuid = ?", userID, uuid).First(&ret).Error - if conn.RecordNotFound() { + if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil } - if err := conn.Error; err != nil { - return nil, errors.Wrap(err, "finding digest") + if err != nil { + return nil, pkgErrors.Wrap(err, "finding digest") } return &ret, nil @@ -180,37 +161,28 @@ func (a *App) GetUserNoteByUUID(userID int, uuid string) (*database.Note, error) // GetNotesParams is params for finding notes type GetNotesParams struct { - Year int - Month int - Page int - Books []string - Search string - Encrypted bool - PerPage int + Year int + Month int + Page int + Books []string + Search string + PerPage int } type ftsParams struct { HighlightAll bool } -func getHeadlineOptions(params *ftsParams) string { - headlineOptions := []string{ - "StartSel=", - "StopSel=", - "ShortWord=0", - } - +func getFTSBodyExpression(params *ftsParams) string { if params != nil && params.HighlightAll { - headlineOptions = append(headlineOptions, "HighlightAll=true") - } else { - headlineOptions = append(headlineOptions, "MaxFragments=3, MaxWords=50, MinWords=10") + return "highlight(notes_fts, 0, '', '') AS body" } - return strings.Join(headlineOptions, ",") + return "snippet(notes_fts, 0, '', '', '...', 50) AS body" } -func selectFTSFields(conn *gorm.DB, search string, params *ftsParams) *gorm.DB { - headlineOpts := getHeadlineOptions(params) +func selectFTSFields(conn *gorm.DB, params *ftsParams) *gorm.DB { + bodyExpr := getFTSBodyExpression(params) return conn.Select(` notes.id, @@ -223,20 +195,19 @@ notes.added_on, notes.edited_on, notes.usn, notes.deleted, -notes.encrypted, -ts_headline('english_nostop', notes.body, plainto_tsquery('english_nostop', ?), ?) AS body - `, search, headlineOpts) +` + bodyExpr) } func getNotesBaseQuery(db *gorm.DB, userID int, q GetNotesParams) *gorm.DB { conn := db.Where( - "notes.user_id = ? AND notes.deleted = ? AND notes.encrypted = ?", - userID, false, q.Encrypted, + "notes.user_id = ? AND notes.deleted = ?", + userID, false, ) if q.Search != "" { - conn = selectFTSFields(conn, q.Search, nil) - conn = conn.Where("tsv @@ plainto_tsquery('english_nostop', ?)", q.Search) + conn = selectFTSFields(conn, nil) + conn = conn.Joins("INNER JOIN notes_fts ON notes_fts.rowid = notes.id") + conn = conn.Where("notes_fts MATCH ?", q.Search) } if len(q.Books) > 0 { @@ -288,16 +259,16 @@ func paginate(conn *gorm.DB, page, perPage int) *gorm.DB { // GetNotesResult is the result of getting notes type GetNotesResult struct { Notes []database.Note - Total int + Total int64 } // GetNotes returns a list of matching notes func (a *App) GetNotes(userID int, params GetNotesParams) (GetNotesResult, error) { conn := getNotesBaseQuery(a.DB, userID, params) - var total int + var total int64 if err := conn.Model(database.Note{}).Count(&total).Error; err != nil { - return GetNotesResult{}, errors.Wrap(err, "counting total") + return GetNotesResult{}, pkgErrors.Wrap(err, "counting total") } notes := []database.Note{} @@ -307,7 +278,7 @@ func (a *App) GetNotes(userID int, params GetNotesParams) (GetNotesResult, error conn = paginate(conn, params.Page, params.PerPage) if err := conn.Find(¬es).Error; err != nil { - return GetNotesResult{}, errors.Wrap(err, "finding notes") + return GetNotesResult{}, pkgErrors.Wrap(err, "finding notes") } } diff --git a/pkg/server/app/notes_test.go b/pkg/server/app/notes_test.go index b2c49932..a165312d 100644 --- a/pkg/server/app/notes_test.go +++ b/pkg/server/app/notes_test.go @@ -1,25 +1,23 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package app import ( "fmt" + "strings" "testing" "time" @@ -32,8 +30,6 @@ import ( func TestCreateNote(t *testing.T) { serverTime := time.Date(2017, time.March, 14, 21, 15, 0, 0, time.UTC) - mockClock := clock.NewMock() - mockClock.SetNow(serverTime) ts1 := time.Date(2018, time.November, 12, 10, 11, 0, 0, time.UTC).UnixNano() ts2 := time.Date(2018, time.November, 15, 0, 1, 10, 0, time.UTC).UnixNano() @@ -74,39 +70,41 @@ func TestCreateNote(t *testing.T) { for idx, tc := range testCases { func() { - defer testutils.ClearData(testutils.DB) + // Create a new clock for each test case to avoid race conditions in parallel tests + mockClock := clock.NewMock() + mockClock.SetNow(serverTime) - user := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + db := testutils.InitMemoryDB(t) - anotherUser := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + user := testutils.SetupUserData(db, "user@test.com", "password123") + testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + fmt.Println(user) + + anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") + testutils.MustExec(t, db.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) b1 := database.Book{UserID: user.ID, Label: "js", Deleted: false} - testutils.MustExec(t, testutils.DB.Save(&b1), fmt.Sprintf("preparing b1 for test case %d", idx)) + testutils.MustExec(t, db.Save(&b1), fmt.Sprintf("preparing b1 for test case %d", idx)) - a := NewTest(&App{ - Clock: mockClock, - }) + a := NewTest() + a.DB = db + a.Clock = mockClock - tx := testutils.DB.Begin() - if _, err := a.CreateNote(user, b1.UUID, "note content", tc.addedOn, tc.editedOn, false, ""); err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "deleting note")) + if _, err := a.CreateNote(user, b1.UUID, "note content", tc.addedOn, tc.editedOn, ""); err != nil { + t.Fatal(errors.Wrapf(err, "creating note for test case %d", idx)) } - tx.Commit() - var bookCount, noteCount int + var bookCount, noteCount int64 var noteRecord database.Note var userRecord database.User - testutils.MustExec(t, testutils.DB.Model(&database.Book{}).Count(&bookCount), fmt.Sprintf("counting book for test case %d", idx)) - testutils.MustExec(t, testutils.DB.Model(&database.Note{}).Count(¬eCount), fmt.Sprintf("counting notes for test case %d", idx)) - testutils.MustExec(t, testutils.DB.First(¬eRecord), fmt.Sprintf("finding note for test case %d", idx)) - testutils.MustExec(t, testutils.DB.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) + testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), fmt.Sprintf("counting book for test case %d", idx)) + testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), fmt.Sprintf("counting notes for test case %d", idx)) + testutils.MustExec(t, db.First(¬eRecord), fmt.Sprintf("finding note for test case %d", idx)) + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) - assert.Equal(t, bookCount, 1, "book count mismatch") - assert.Equal(t, noteCount, 1, "note count mismatch") + assert.Equal(t, bookCount, int64(1), "book count mismatch") + assert.Equal(t, noteCount, int64(1), "note count mismatch") assert.NotEqual(t, noteRecord.UUID, "", "note UUID should have been generated") assert.Equal(t, noteRecord.UserID, user.ID, "note UserID mismatch") assert.Equal(t, noteRecord.Body, "note content", "note Body mismatch") @@ -116,10 +114,41 @@ func TestCreateNote(t *testing.T) { assert.Equal(t, noteRecord.EditedOn, tc.expectedEditedOn, "note EditedOn mismatch") assert.Equal(t, userRecord.MaxUSN, tc.expectedUSN, "user max_usn mismatch") + + // Assert FTS table is updated + var ftsBody string + testutils.MustExec(t, db.Raw("SELECT body FROM notes_fts WHERE rowid = ?", noteRecord.ID).Scan(&ftsBody), fmt.Sprintf("querying notes_fts for test case %d", idx)) + assert.Equal(t, ftsBody, "note content", "FTS body mismatch") + var searchCount int64 + testutils.MustExec(t, db.Raw("SELECT COUNT(*) FROM notes_fts WHERE notes_fts MATCH ?", "content").Scan(&searchCount), "searching notes_fts") + assert.Equal(t, searchCount, int64(1), "Note should still be searchable") }() } } +func TestCreateNote_EmptyBody(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + b1 := database.Book{UserID: user.ID, Label: "testBook"} + testutils.MustExec(t, db.Save(&b1), "preparing book") + + a := NewTest() + a.DB = db + a.Clock = clock.NewMock() + + // Create note with empty body + note, err := a.CreateNote(user, b1.UUID, "", nil, nil, "") + if err != nil { + t.Fatal(errors.Wrap(err, "creating note with empty body")) + } + + // Assert FTS entry exists with empty body + var ftsBody string + testutils.MustExec(t, db.Raw("SELECT body FROM notes_fts WHERE rowid = ?", note.ID).Scan(&ftsBody), "querying notes_fts for empty body note") + assert.Equal(t, ftsBody, "", "FTS body should be empty for note created with empty body") +} + func TestUpdateNote(t *testing.T) { testCases := []struct { userUSN int @@ -137,60 +166,107 @@ func TestUpdateNote(t *testing.T) { for idx, tc := range testCases { t.Run(fmt.Sprintf("test case %d", idx), func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) - user := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&user).Update("max_usn", tc.userUSN), "preparing user max_usn for test case") + user := testutils.SetupUserData(db, "user@test.com", "password123") + testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.userUSN), "preparing user max_usn for test case") - anotherUser := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&anotherUser).Update("max_usn", 55), "preparing user max_usn for test case") + anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") + testutils.MustExec(t, db.Model(&anotherUser).Update("max_usn", 55), "preparing user max_usn for test case") b1 := database.Book{UserID: user.ID, Label: "js", Deleted: false} - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1 for test case") + testutils.MustExec(t, db.Save(&b1), "preparing b1 for test case") note := database.Note{UserID: user.ID, Deleted: false, Body: "test content", BookUUID: b1.UUID} - testutils.MustExec(t, testutils.DB.Save(¬e), "preparing note for test case") + testutils.MustExec(t, db.Save(¬e), "preparing note for test case") + + // Assert FTS table has original content + var ftsBodyBefore string + testutils.MustExec(t, db.Raw("SELECT body FROM notes_fts WHERE rowid = ?", note.ID).Scan(&ftsBodyBefore), "querying notes_fts before update") + assert.Equal(t, ftsBodyBefore, "test content", "FTS body mismatch before update") c := clock.NewMock() content := "updated test content" - public := true - a := NewTest(&App{ - Clock: c, - }) + a := NewTest() + a.DB = db + a.Clock = c - tx := testutils.DB.Begin() + tx := db.Begin() if _, err := a.UpdateNote(tx, user, note, &UpdateNoteParams{ Content: &content, - Public: &public, }); err != nil { tx.Rollback() - t.Fatal(errors.Wrap(err, "deleting note")) + t.Fatal(errors.Wrap(err, "updating note")) } tx.Commit() - var bookCount, noteCount int + var bookCount, noteCount int64 var noteRecord database.Note var userRecord database.User - testutils.MustExec(t, testutils.DB.Model(&database.Book{}).Count(&bookCount), "counting book for test case") - testutils.MustExec(t, testutils.DB.Model(&database.Note{}).Count(¬eCount), "counting notes for test case") - testutils.MustExec(t, testutils.DB.First(¬eRecord), "finding note for test case") - testutils.MustExec(t, testutils.DB.Where("id = ?", user.ID).First(&userRecord), "finding user for test case") + testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), "counting book for test case") + testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), "counting notes for test case") + testutils.MustExec(t, db.First(¬eRecord), "finding note for test case") + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), "finding user for test case") expectedUSN := tc.userUSN + 1 - assert.Equal(t, bookCount, 1, "book count mismatch") - assert.Equal(t, noteCount, 1, "note count mismatch") + assert.Equal(t, bookCount, int64(1), "book count mismatch") + assert.Equal(t, noteCount, int64(1), "note count mismatch") assert.Equal(t, noteRecord.UserID, user.ID, "note UserID mismatch") assert.Equal(t, noteRecord.Body, content, "note Body mismatch") - assert.Equal(t, noteRecord.Public, public, "note Public mismatch") assert.Equal(t, noteRecord.Deleted, false, "note Deleted mismatch") assert.Equal(t, noteRecord.USN, expectedUSN, "note USN mismatch") assert.Equal(t, userRecord.MaxUSN, expectedUSN, "user MaxUSN mismatch") + + // Assert FTS table is updated with new content + var ftsBodyAfter string + testutils.MustExec(t, db.Raw("SELECT body FROM notes_fts WHERE rowid = ?", noteRecord.ID).Scan(&ftsBodyAfter), "querying notes_fts after update") + assert.Equal(t, ftsBodyAfter, content, "FTS body mismatch after update") + var searchCount int64 + testutils.MustExec(t, db.Raw("SELECT COUNT(*) FROM notes_fts WHERE notes_fts MATCH ?", "updated").Scan(&searchCount), "searching notes_fts") + assert.Equal(t, searchCount, int64(1), "Note should still be searchable") }) } } +func TestUpdateNote_SameContent(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + b1 := database.Book{UserID: user.ID, Label: "testBook"} + testutils.MustExec(t, db.Save(&b1), "preparing book") + + note := database.Note{UserID: user.ID, Deleted: false, Body: "test content", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e), "preparing note") + + a := NewTest() + a.DB = db + a.Clock = clock.NewMock() + + // Update note with same content + sameContent := "test content" + tx := db.Begin() + _, err := a.UpdateNote(tx, user, note, &UpdateNoteParams{ + Content: &sameContent, + }) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "updating note with same content")) + } + tx.Commit() + + // Assert FTS still has the same content + var ftsBody string + testutils.MustExec(t, db.Raw("SELECT body FROM notes_fts WHERE rowid = ?", note.ID).Scan(&ftsBody), "querying notes_fts after update") + assert.Equal(t, ftsBody, "test content", "FTS body should still be 'test content'") + + // Assert it's still searchable + var searchCount int64 + testutils.MustExec(t, db.Raw("SELECT COUNT(*) FROM notes_fts WHERE notes_fts MATCH ?", "test").Scan(&searchCount), "searching notes_fts") + assert.Equal(t, searchCount, int64(1), "Note should still be searchable") +} + func TestDeleteNote(t *testing.T) { testCases := []struct { userUSN int @@ -212,23 +288,29 @@ func TestDeleteNote(t *testing.T) { for idx, tc := range testCases { func() { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) - user := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + user := testutils.SetupUserData(db, "user@test.com", "password123") + testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) - anotherUser := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") + testutils.MustExec(t, db.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) b1 := database.Book{UserID: user.ID, Label: "testBook"} - testutils.MustExec(t, testutils.DB.Save(&b1), fmt.Sprintf("preparing b1 for test case %d", idx)) + testutils.MustExec(t, db.Save(&b1), fmt.Sprintf("preparing b1 for test case %d", idx)) note := database.Note{UserID: user.ID, Deleted: false, Body: "test content", BookUUID: b1.UUID} - testutils.MustExec(t, testutils.DB.Save(¬e), fmt.Sprintf("preparing note for test case %d", idx)) + testutils.MustExec(t, db.Save(¬e), fmt.Sprintf("preparing note for test case %d", idx)) - a := NewTest(nil) + // Assert FTS table has content before delete + var ftsCountBefore int64 + testutils.MustExec(t, db.Raw("SELECT COUNT(*) FROM notes_fts WHERE rowid = ?", note.ID).Scan(&ftsCountBefore), fmt.Sprintf("counting notes_fts before delete for test case %d", idx)) + assert.Equal(t, ftsCountBefore, int64(1), "FTS should have entry before delete") - tx := testutils.DB.Begin() + a := NewTest() + a.DB = db + + tx := db.Begin() ret, err := a.DeleteNote(tx, user, note) if err != nil { tx.Rollback() @@ -236,15 +318,15 @@ func TestDeleteNote(t *testing.T) { } tx.Commit() - var noteCount int + var noteCount int64 var noteRecord database.Note var userRecord database.User - testutils.MustExec(t, testutils.DB.Model(&database.Note{}).Count(¬eCount), fmt.Sprintf("counting notes for test case %d", idx)) - testutils.MustExec(t, testutils.DB.First(¬eRecord), fmt.Sprintf("finding note for test case %d", idx)) - testutils.MustExec(t, testutils.DB.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) + testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), fmt.Sprintf("counting notes for test case %d", idx)) + testutils.MustExec(t, db.First(¬eRecord), fmt.Sprintf("finding note for test case %d", idx)) + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) - assert.Equal(t, noteCount, 1, "note count mismatch") + assert.Equal(t, noteCount, int64(1), "note count mismatch") assert.Equal(t, noteRecord.UserID, user.ID, "note user_id mismatch") assert.Equal(t, noteRecord.Body, "", "note content mismatch") @@ -256,6 +338,178 @@ func TestDeleteNote(t *testing.T) { assert.Equal(t, ret.Body, "", "note content mismatch") assert.Equal(t, ret.Deleted, true, "note deleted flag mismatch") assert.Equal(t, ret.USN, tc.expectedUSN, "note label mismatch") + + // Assert FTS body is empty after delete (row still exists but content is cleared) + var ftsBody string + testutils.MustExec(t, db.Raw("SELECT body FROM notes_fts WHERE rowid = ?", noteRecord.ID).Scan(&ftsBody), fmt.Sprintf("querying notes_fts after delete for test case %d", idx)) + assert.Equal(t, ftsBody, "", "FTS body should be empty after delete") }() } } + +func TestGetNotes_FTSSearch(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + b1 := database.Book{UserID: user.ID, Label: "testBook"} + testutils.MustExec(t, db.Save(&b1), "preparing book") + + // Create notes with different content + note1 := database.Note{UserID: user.ID, Deleted: false, Body: "foo bar baz bar", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e1), "preparing note1") + + note2 := database.Note{UserID: user.ID, Deleted: false, Body: "hello run foo", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e2), "preparing note2") + + note3 := database.Note{UserID: user.ID, Deleted: false, Body: "running quz succeeded", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e3), "preparing note3") + + a := NewTest() + a.DB = db + a.Clock = clock.NewMock() + + // Search "baz" + result, err := a.GetNotes(user.ID, GetNotesParams{ + Search: "baz", + Page: 1, + PerPage: 30, + }) + if err != nil { + t.Fatal(errors.Wrap(err, "getting notes with FTS search")) + } + assert.Equal(t, result.Total, int64(1), "Should find 1 note with 'baz'") + assert.Equal(t, len(result.Notes), 1, "Should return 1 note") + for i, note := range result.Notes { + assert.Equal(t, strings.Contains(note.Body, "baz"), true, fmt.Sprintf("Note %d should contain highlighted dnote", i)) + } + + // Search for "running" - should return 1 note + result, err = a.GetNotes(user.ID, GetNotesParams{ + Search: "running", + Page: 1, + PerPage: 30, + }) + if err != nil { + t.Fatal(errors.Wrap(err, "getting notes with FTS search for review")) + } + assert.Equal(t, result.Total, int64(2), "Should find 2 note with 'running'") + assert.Equal(t, len(result.Notes), 2, "Should return 2 notes") + assert.Equal(t, result.Notes[0].Body, "running quz succeeded", "Should return the review note with highlighting") + assert.Equal(t, result.Notes[1].Body, "hello run foo", "Should return the review note with highlighting") + + // Search for non-existent term - should return 0 notes + result, err = a.GetNotes(user.ID, GetNotesParams{ + Search: "nonexistent", + Page: 1, + PerPage: 30, + }) + if err != nil { + t.Fatal(errors.Wrap(err, "getting notes with FTS search for nonexistent")) + } + + assert.Equal(t, result.Total, int64(0), "Should find 0 notes with 'nonexistent'") + assert.Equal(t, len(result.Notes), 0, "Should return 0 notes") +} + +func TestGetNotes_FTSSearch_Snippet(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + b1 := database.Book{UserID: user.ID, Label: "testBook"} + testutils.MustExec(t, db.Save(&b1), "preparing book") + + // Create a long note to test snippet truncation with "..." + // The snippet limit is 50 tokens, so we generate enough words to exceed it + longBody := strings.Repeat("filler ", 100) + "the important keyword appears here" + longNote := database.Note{UserID: user.ID, Deleted: false, Body: longBody, BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(&longNote), "preparing long note") + + a := NewTest() + a.DB = db + a.Clock = clock.NewMock() + + // Search for "keyword" in long note - should return snippet with "..." + result, err := a.GetNotes(user.ID, GetNotesParams{ + Search: "keyword", + Page: 1, + PerPage: 30, + }) + if err != nil { + t.Fatal(errors.Wrap(err, "getting notes with FTS search for keyword")) + } + + assert.Equal(t, result.Total, int64(1), "Should find 1 note with 'keyword'") + assert.Equal(t, len(result.Notes), 1, "Should return 1 note") + // The snippet should contain "..." to indicate truncation and the highlighted keyword + assert.Equal(t, strings.Contains(result.Notes[0].Body, "..."), true, "Snippet should contain '...' for truncation") + assert.Equal(t, strings.Contains(result.Notes[0].Body, "keyword"), true, "Snippet should contain highlighted keyword") +} + +func TestGetNotes_FTSSearch_ShortWord(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + b1 := database.Book{UserID: user.ID, Label: "testBook"} + testutils.MustExec(t, db.Save(&b1), "preparing book") + + // Create notes with short words + note1 := database.Note{UserID: user.ID, Deleted: false, Body: "a b c", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e1), "preparing note1") + + note2 := database.Note{UserID: user.ID, Deleted: false, Body: "d", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e2), "preparing note2") + + a := NewTest() + a.DB = db + a.Clock = clock.NewMock() + + result, err := a.GetNotes(user.ID, GetNotesParams{ + Search: "a", + Page: 1, + PerPage: 30, + }) + if err != nil { + t.Fatal(errors.Wrap(err, "getting notes with FTS search for 'a'")) + } + + assert.Equal(t, result.Total, int64(1), "Should find 1 note") + assert.Equal(t, len(result.Notes), 1, "Should return 1 note") + assert.Equal(t, strings.Contains(result.Notes[0].Body, "a"), true, "Should contain highlighted 'a'") +} + +func TestGetNotes_All(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + b1 := database.Book{UserID: user.ID, Label: "testBook"} + testutils.MustExec(t, db.Save(&b1), "preparing book") + + note1 := database.Note{UserID: user.ID, Deleted: false, Body: "a b c", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e1), "preparing note1") + + note2 := database.Note{UserID: user.ID, Deleted: false, Body: "d", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e2), "preparing note2") + + a := NewTest() + a.DB = db + a.Clock = clock.NewMock() + + result, err := a.GetNotes(user.ID, GetNotesParams{ + Search: "", + Page: 1, + PerPage: 30, + }) + if err != nil { + t.Fatal(errors.Wrap(err, "getting notes with FTS search for 'a'")) + } + + assert.Equal(t, result.Total, int64(2), "Should not find all notes") + assert.Equal(t, len(result.Notes), 2, "Should not find all notes") + + for _, note := range result.Notes { + assert.Equal(t, strings.Contains(note.Body, ""), false, "There should be no keywords") + assert.Equal(t, strings.Contains(note.Body, ""), false, "There should be no keywords") + } + assert.Equal(t, result.Notes[0].Body, "d", "Full content should be returned") + assert.Equal(t, result.Notes[1].Body, "a b c", "Full content should be returned") +} diff --git a/pkg/server/app/sessions.go b/pkg/server/app/sessions.go index 42f4b48b..6c230d76 100644 --- a/pkg/server/app/sessions.go +++ b/pkg/server/app/sessions.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package app @@ -23,7 +20,7 @@ import ( "github.com/dnote/dnote/pkg/server/crypt" "github.com/dnote/dnote/pkg/server/database" - "github.com/jinzhu/gorm" + "gorm.io/gorm" "github.com/pkg/errors" ) @@ -51,7 +48,7 @@ func (a *App) CreateSession(userID int) (database.Session, error) { // DeleteUserSessions deletes all existing sessions for the given user. It effectively // invalidates all existing sessions. func (a *App) DeleteUserSessions(db *gorm.DB, userID int) error { - if err := db.Debug().Where("user_id = ?", userID).Delete(&database.Session{}).Error; err != nil { + if err := db.Where("user_id = ?", userID).Delete(&database.Session{}).Error; err != nil { return errors.Wrap(err, "deleting sessions") } diff --git a/pkg/server/app/testutils.go b/pkg/server/app/testutils.go index c4d9d5ad..248f4784 100644 --- a/pkg/server/app/testutils.go +++ b/pkg/server/app/testutils.go @@ -1,68 +1,36 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package app import ( - "fmt" - "github.com/dnote/dnote/pkg/clock" - "github.com/dnote/dnote/pkg/server/config" - "github.com/dnote/dnote/pkg/server/mailer" + "github.com/dnote/dnote/pkg/server/assets" "github.com/dnote/dnote/pkg/server/testutils" ) // NewTest returns an app for a testing environment -func NewTest(appParams *App) App { - c := config.Load() - c.SetOnPremise(false) - - a := App{ - DB: testutils.DB, - Clock: clock.NewMock(), - EmailTemplates: mailer.NewTemplates(), - EmailBackend: &testutils.MockEmailbackendImplementation{}, - Config: c, - HTTP500Page: []byte(""), +func NewTest() App { + return App{ + Clock: clock.NewMock(), + EmailBackend: &testutils.MockEmailbackendImplementation{}, + HTTP500Page: assets.MustGetHTTP500ErrorPage(), + BaseURL: "http://127.0.0.0.1", + Port: "3000", + DisableRegistration: false, + DBPath: "", + AssetBaseURL: "", } - - // Allow to override with appParams - if appParams != nil && appParams.EmailBackend != nil { - a.EmailBackend = appParams.EmailBackend - } - if appParams != nil && appParams.Clock != nil { - a.Clock = appParams.Clock - } - if appParams != nil && appParams.EmailTemplates != nil { - a.EmailTemplates = appParams.EmailTemplates - } - if appParams != nil && appParams.Config.OnPremise { - a.Config.OnPremise = appParams.Config.OnPremise - } - if appParams != nil && appParams.Config.WebURL != "" { - a.Config.WebURL = appParams.Config.WebURL - } - if appParams != nil && appParams.Config.DisableRegistration { - a.Config.DisableRegistration = appParams.Config.DisableRegistration - } - - fmt.Printf("%+v\n", appParams) - fmt.Printf("%+v\n", a) - - return a } diff --git a/pkg/server/app/users.go b/pkg/server/app/users.go index 59a79d06..9c167b69 100644 --- a/pkg/server/app/users.go +++ b/pkg/server/app/users.go @@ -1,48 +1,45 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package app import ( + "errors" + "github.com/dnote/dnote/pkg/server/database" + "github.com/dnote/dnote/pkg/server/helpers" "github.com/dnote/dnote/pkg/server/log" - "github.com/dnote/dnote/pkg/server/token" - "github.com/jinzhu/gorm" - "github.com/pkg/errors" + pkgErrors "github.com/pkg/errors" "golang.org/x/crypto/bcrypt" + "gorm.io/gorm" ) -// TouchLastLoginAt updates the last login timestamp -func (a *App) TouchLastLoginAt(user database.User, tx *gorm.DB) error { - t := a.Clock.Now() - if err := tx.Model(&user).Update(database.User{LastLoginAt: &t}).Error; err != nil { - return errors.Wrap(err, "updating last_login_at") +// validatePassword validates a password +func validatePassword(password string) error { + if len(password) < 8 { + return ErrPasswordTooShort } return nil } -func createEmailPreference(user database.User, tx *gorm.DB) error { - p := database.EmailPreference{ - UserID: user.ID, - } - if err := tx.Save(&p).Error; err != nil { - return errors.Wrap(err, "inserting email preference") +// TouchLastLoginAt updates the last login timestamp +func (a *App) TouchLastLoginAt(user database.User, tx *gorm.DB) error { + t := a.Clock.Now() + if err := tx.Model(&user).Update("last_login_at", &t).Error; err != nil { + return pkgErrors.Wrap(err, "updating last_login_at") } return nil @@ -54,8 +51,8 @@ func (a *App) CreateUser(email, password string, passwordConfirmation string) (d return database.User{}, ErrEmailRequired } - if len(password) < 8 { - return database.User{}, ErrPasswordTooShort + if err := validatePassword(password); err != nil { + return database.User{}, err } if password != passwordConfirmation { @@ -64,56 +61,41 @@ func (a *App) CreateUser(email, password string, passwordConfirmation string) (d tx := a.DB.Begin() - var count int - if err := tx.Model(database.Account{}).Where("email = ?", email).Count(&count).Error; err != nil { - return database.User{}, errors.Wrap(err, "counting user") + var count int64 + if err := tx.Model(&database.User{}).Where("email = ?", email).Count(&count).Error; err != nil { + tx.Rollback() + return database.User{}, pkgErrors.Wrap(err, "counting user") } if count > 0 { + tx.Rollback() return database.User{}, ErrDuplicateEmail } hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { tx.Rollback() - return database.User{}, errors.Wrap(err, "hashing password") + return database.User{}, pkgErrors.Wrap(err, "hashing password") } - // Grant all privileges if self-hosting - var pro bool - if a.Config.OnPremise { - pro = true - } else { - pro = false + uuid, err := helpers.GenUUID() + if err != nil { + tx.Rollback() + return database.User{}, pkgErrors.Wrap(err, "generating UUID") } user := database.User{ - Cloud: pro, + UUID: uuid, + Email: database.ToNullString(email), + Password: database.ToNullString(string(hashedPassword)), } if err = tx.Save(&user).Error; err != nil { tx.Rollback() - return database.User{}, errors.Wrap(err, "saving user") - } - account := database.Account{ - Email: database.ToNullString(email), - Password: database.ToNullString(string(hashedPassword)), - UserID: user.ID, - } - if err = tx.Save(&account).Error; err != nil { - tx.Rollback() - return database.User{}, errors.Wrap(err, "saving account") + return database.User{}, pkgErrors.Wrap(err, "saving user") } - if _, err := token.Create(tx, user.ID, database.TokenTypeEmailPreference); err != nil { - tx.Rollback() - return database.User{}, errors.Wrap(err, "creating email verificaiton token") - } - if err := createEmailPreference(user, tx); err != nil { - tx.Rollback() - return database.User{}, errors.Wrap(err, "creating email preference") - } if err := a.TouchLastLoginAt(user, tx); err != nil { tx.Rollback() - return database.User{}, errors.Wrap(err, "updating last login") + return database.User{}, pkgErrors.Wrap(err, "updating last login") } tx.Commit() @@ -121,28 +103,99 @@ func (a *App) CreateUser(email, password string, passwordConfirmation string) (d return user, nil } -// Authenticate authenticates a user -func (a *App) Authenticate(email, password string) (*database.User, error) { - var account database.Account - conn := a.DB.Where("email = ?", email).First(&account) - if conn.RecordNotFound() { +// GetUserByEmail finds a user by email +func (a *App) GetUserByEmail(email string) (*database.User, error) { + var user database.User + err := a.DB.Where("email = ?", email).First(&user).Error + if errors.Is(err, gorm.ErrRecordNotFound) { return nil, ErrNotFound - } else if conn.Error != nil { - return nil, conn.Error + } else if err != nil { + return nil, err } - err := bcrypt.CompareHashAndPassword([]byte(account.Password.String), []byte(password)) + return &user, nil +} + +// GetAllUsers retrieves all users from the database +func (a *App) GetAllUsers() ([]database.User, error) { + var users []database.User + err := a.DB.Find(&users).Error + if err != nil { + return nil, pkgErrors.Wrap(err, "finding users") + } + + return users, nil +} + +// Authenticate authenticates a user +func (a *App) Authenticate(email, password string) (*database.User, error) { + user, err := a.GetUserByEmail(email) + if err != nil { + return nil, err + } + + err = bcrypt.CompareHashAndPassword([]byte(user.Password.String), []byte(password)) if err != nil { return nil, ErrLoginInvalid } - var user database.User - err = a.DB.Where("id = ?", account.UserID).First(&user).Error - if err != nil { - return nil, errors.Wrap(err, "finding user") + return user, nil +} + +// UpdateUserPassword updates a user's password with validation +func UpdateUserPassword(db *gorm.DB, user *database.User, newPassword string) error { + // Validate password + if err := validatePassword(newPassword); err != nil { + return err } - return &user, nil + // Hash the password + hashedPassword, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost) + if err != nil { + return pkgErrors.Wrap(err, "hashing password") + } + + // Update the password + if err := db.Model(&user).Update("password", string(hashedPassword)).Error; err != nil { + return pkgErrors.Wrap(err, "updating password") + } + + return nil +} + +// RemoveUser removes a user from the system +// Returns an error if the user has any notes or books +func (a *App) RemoveUser(email string) error { + // Find the user + user, err := a.GetUserByEmail(email) + if err != nil { + return err + } + + // Check if user has any notes + var noteCount int64 + if err := a.DB.Model(&database.Note{}).Where("user_id = ? AND deleted = ?", user.ID, false).Count(¬eCount).Error; err != nil { + return pkgErrors.Wrap(err, "counting notes") + } + if noteCount > 0 { + return ErrUserHasExistingResources + } + + // Check if user has any books + var bookCount int64 + if err := a.DB.Model(&database.Book{}).Where("user_id = ? AND deleted = ?", user.ID, false).Count(&bookCount).Error; err != nil { + return pkgErrors.Wrap(err, "counting books") + } + if bookCount > 0 { + return ErrUserHasExistingResources + } + + // Delete user + if err := a.DB.Delete(&user).Error; err != nil { + return pkgErrors.Wrap(err, "deleting user") + } + + return nil } // SignIn signs in a user @@ -154,7 +207,7 @@ func (a *App) SignIn(user *database.User) (*database.Session, error) { session, err := a.CreateSession(user.ID) if err != nil { - return nil, errors.Wrap(err, "creating session") + return nil, pkgErrors.Wrap(err, "creating session") } return &session, nil diff --git a/pkg/server/app/users_test.go b/pkg/server/app/users_test.go index 74eef5f5..52183520 100644 --- a/pkg/server/app/users_test.go +++ b/pkg/server/app/users_test.go @@ -1,121 +1,423 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package app import ( - "fmt" "testing" "github.com/dnote/dnote/pkg/assert" - "github.com/dnote/dnote/pkg/server/config" "github.com/dnote/dnote/pkg/server/database" "github.com/dnote/dnote/pkg/server/testutils" "github.com/pkg/errors" "golang.org/x/crypto/bcrypt" ) -func TestCreateUser_ProValue(t *testing.T) { +func TestValidatePassword(t *testing.T) { testCases := []struct { - onPremise bool - expectedPro bool + name string + password string + wantErr error }{ { - onPremise: true, - expectedPro: true, + name: "valid password", + password: "password123", + wantErr: nil, }, { - onPremise: false, - expectedPro: false, + name: "valid password exactly 8 chars", + password: "12345678", + wantErr: nil, + }, + { + name: "password too short", + password: "1234567", + wantErr: ErrPasswordTooShort, + }, + { + name: "empty password", + password: "", + wantErr: ErrPasswordTooShort, }, } for _, tc := range testCases { - t.Run(fmt.Sprintf("self hosting %t", tc.onPremise), func(t *testing.T) { - c := config.Load() - c.SetOnPremise(tc.onPremise) - - defer testutils.ClearData(testutils.DB) - - a := NewTest(&App{ - Config: c, - }) - if _, err := a.CreateUser("alice@example.com", "pass1234", "pass1234"); err != nil { - t.Fatal(errors.Wrap(err, "executing")) - } - - var userCount int - var userRecord database.User - testutils.MustExec(t, testutils.DB.Model(&database.User{}).Count(&userCount), "counting user") - testutils.MustExec(t, testutils.DB.First(&userRecord), "finding user") - - assert.Equal(t, userCount, 1, "book count mismatch") - assert.Equal(t, userRecord.Cloud, tc.expectedPro, "user pro mismatch") + t.Run(tc.name, func(t *testing.T) { + err := validatePassword(tc.password) + assert.Equal(t, err, tc.wantErr, "error mismatch") }) } } +func TestCreateUser_ProValue(t *testing.T) { + db := testutils.InitMemoryDB(t) + + a := NewTest() + a.DB = db + if _, err := a.CreateUser("alice@example.com", "pass1234", "pass1234"); err != nil { + t.Fatal(errors.Wrap(err, "executing")) + } + + var userCount int64 + var userRecord database.User + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting user") + testutils.MustExec(t, db.First(&userRecord), "finding user") + + assert.Equal(t, userCount, int64(1), "book count mismatch") + +} + +func TestGetUserByEmail(t *testing.T) { + t.Run("success", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "password123") + + a := NewTest() + a.DB = db + + foundUser, err := a.GetUserByEmail("alice@example.com") + + assert.Equal(t, err, nil, "should not error") + assert.Equal(t, foundUser.Email.String, "alice@example.com", "email mismatch") + assert.Equal(t, foundUser.ID, user.ID, "user ID mismatch") + }) + + t.Run("not found", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + a := NewTest() + a.DB = db + + user, err := a.GetUserByEmail("nonexistent@example.com") + + assert.Equal(t, err, ErrNotFound, "should return ErrNotFound") + assert.Equal(t, user, (*database.User)(nil), "user should be nil") + }) +} + +func TestGetAllUsers(t *testing.T) { + t.Run("success with multiple users", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user1 := testutils.SetupUserData(db, "alice@example.com", "password123") + user2 := testutils.SetupUserData(db, "bob@example.com", "password123") + user3 := testutils.SetupUserData(db, "charlie@example.com", "password123") + + a := NewTest() + a.DB = db + + users, err := a.GetAllUsers() + + assert.Equal(t, err, nil, "should not error") + assert.Equal(t, len(users), 3, "should return 3 users") + + // Verify all users are returned + emails := make(map[string]bool) + for _, user := range users { + emails[user.Email.String] = true + } + assert.Equal(t, emails["alice@example.com"], true, "alice should be in results") + assert.Equal(t, emails["bob@example.com"], true, "bob should be in results") + assert.Equal(t, emails["charlie@example.com"], true, "charlie should be in results") + + // Verify user details match + for _, user := range users { + if user.Email.String == "alice@example.com" { + assert.Equal(t, user.ID, user1.ID, "alice ID mismatch") + } else if user.Email.String == "bob@example.com" { + assert.Equal(t, user.ID, user2.ID, "bob ID mismatch") + } else if user.Email.String == "charlie@example.com" { + assert.Equal(t, user.ID, user3.ID, "charlie ID mismatch") + } + } + }) + + t.Run("empty database", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + a := NewTest() + a.DB = db + + users, err := a.GetAllUsers() + + assert.Equal(t, err, nil, "should not error") + assert.Equal(t, len(users), 0, "should return 0 users") + }) + + t.Run("single user", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "password123") + + a := NewTest() + a.DB = db + + users, err := a.GetAllUsers() + + assert.Equal(t, err, nil, "should not error") + assert.Equal(t, len(users), 1, "should return 1 user") + assert.Equal(t, users[0].Email.String, "alice@example.com", "email mismatch") + assert.Equal(t, users[0].ID, user.ID, "user ID mismatch") + }) +} + func TestCreateUser(t *testing.T) { t.Run("success", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) - c := config.Load() - a := NewTest(&App{ - Config: c, - }) + a := NewTest() + a.DB = db if _, err := a.CreateUser("alice@example.com", "pass1234", "pass1234"); err != nil { t.Fatal(errors.Wrap(err, "executing")) } - var userCount int - testutils.MustExec(t, testutils.DB.Model(&database.User{}).Count(&userCount), "counting user") - assert.Equal(t, userCount, 1, "book count mismatch") + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting user") + assert.Equal(t, userCount, int64(1), "user count mismatch") - var accountCount int - var accountRecord database.Account - testutils.MustExec(t, testutils.DB.Model(&database.Account{}).Count(&accountCount), "counting account") - testutils.MustExec(t, testutils.DB.First(&accountRecord), "finding account") + var userRecord database.User + testutils.MustExec(t, db.First(&userRecord), "finding user") - assert.Equal(t, accountCount, 1, "account count mismatch") - assert.Equal(t, accountRecord.Email.String, "alice@example.com", "account email mismatch") + assert.Equal(t, userRecord.Email.String, "alice@example.com", "user email mismatch") - passwordErr := bcrypt.CompareHashAndPassword([]byte(accountRecord.Password.String), []byte("pass1234")) + passwordErr := bcrypt.CompareHashAndPassword([]byte(userRecord.Password.String), []byte("pass1234")) assert.Equal(t, passwordErr, nil, "Password mismatch") }) t.Run("duplicate email", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) - aliceUser := database.User{} - aliceAccount := database.Account{UserID: aliceUser.ID, Email: database.ToNullString("alice@example.com")} - testutils.MustExec(t, testutils.DB.Save(&aliceUser), "preparing a user") - testutils.MustExec(t, testutils.DB.Save(&aliceAccount), "preparing an account") + testutils.SetupUserData(db, "alice@example.com", "somepassword") - a := NewTest(nil) + a := NewTest() + a.DB = db _, err := a.CreateUser("alice@example.com", "newpassword", "newpassword") assert.Equal(t, err, ErrDuplicateEmail, "error mismatch") - var userCount, accountCount int - testutils.MustExec(t, testutils.DB.Model(&database.User{}).Count(&userCount), "counting user") - testutils.MustExec(t, testutils.DB.Model(&database.Account{}).Count(&accountCount), "counting account") + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting user") + + assert.Equal(t, userCount, int64(1), "user count mismatch") + }) +} + +func TestUpdateUserPassword(t *testing.T) { + t.Run("success", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "oldpassword123") + + err := UpdateUserPassword(db, &user, "newpassword123") + + assert.Equal(t, err, nil, "should not error") + + // Verify password was updated in database + var updatedUser database.User + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&updatedUser), "finding updated user") + + // Verify new password works + passwordErr := bcrypt.CompareHashAndPassword([]byte(updatedUser.Password.String), []byte("newpassword123")) + assert.Equal(t, passwordErr, nil, "New password should match") + + // Verify old password no longer works + oldPasswordErr := bcrypt.CompareHashAndPassword([]byte(updatedUser.Password.String), []byte("oldpassword123")) + assert.NotEqual(t, oldPasswordErr, nil, "Old password should not match") + }) + + t.Run("password too short", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "oldpassword123") + + err := UpdateUserPassword(db, &user, "short") + + assert.Equal(t, err, ErrPasswordTooShort, "should return ErrPasswordTooShort") + + // Verify password was NOT updated in database + var unchangedUser database.User + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&unchangedUser), "finding unchanged user") + + // Verify old password still works + passwordErr := bcrypt.CompareHashAndPassword([]byte(unchangedUser.Password.String), []byte("oldpassword123")) + assert.Equal(t, passwordErr, nil, "Old password should still match") + }) + + t.Run("empty password", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "oldpassword123") + + err := UpdateUserPassword(db, &user, "") + + assert.Equal(t, err, ErrPasswordTooShort, "should return ErrPasswordTooShort") + + // Verify password was NOT updated in database + var unchangedUser database.User + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&unchangedUser), "finding unchanged user") + + // Verify old password still works + passwordErr := bcrypt.CompareHashAndPassword([]byte(unchangedUser.Password.String), []byte("oldpassword123")) + assert.Equal(t, passwordErr, nil, "Old password should still match") + }) + + t.Run("transaction rollback", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "oldpassword123") + + // Start a transaction and rollback to verify UpdateUserPassword respects transactions + tx := db.Begin() + err := UpdateUserPassword(tx, &user, "newpassword123") + assert.Equal(t, err, nil, "should not error") + tx.Rollback() + + // Verify password was NOT updated after rollback + var unchangedUser database.User + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&unchangedUser), "finding unchanged user") + + // Verify old password still works + passwordErr := bcrypt.CompareHashAndPassword([]byte(unchangedUser.Password.String), []byte("oldpassword123")) + assert.Equal(t, passwordErr, nil, "Old password should still match after rollback") + }) + + t.Run("transaction commit", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "oldpassword123") + + // Start a transaction and commit to verify UpdateUserPassword respects transactions + tx := db.Begin() + err := UpdateUserPassword(tx, &user, "newpassword123") + assert.Equal(t, err, nil, "should not error") + tx.Commit() + + // Verify password was updated after commit + var updatedUser database.User + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&updatedUser), "finding updated user") + + // Verify new password works + passwordErr := bcrypt.CompareHashAndPassword([]byte(updatedUser.Password.String), []byte("newpassword123")) + assert.Equal(t, passwordErr, nil, "New password should match after commit") + }) +} + +func TestRemoveUser(t *testing.T) { + t.Run("success", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + testutils.SetupUserData(db, "alice@example.com", "password123") + + a := NewTest() + a.DB = db + + err := a.RemoveUser("alice@example.com") + + assert.Equal(t, err, nil, "should not error") + + // Verify user was deleted + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting users") + assert.Equal(t, userCount, int64(0), "user should be deleted") + }) + + t.Run("user not found", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + a := NewTest() + a.DB = db + + err := a.RemoveUser("nonexistent@example.com") + + assert.Equal(t, err, ErrNotFound, "should return ErrNotFound") + }) + + t.Run("user has notes", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "password123") + + book := database.Book{UserID: user.ID, Label: "testbook", Deleted: false} + testutils.MustExec(t, db.Save(&book), "creating book") + + note := database.Note{UserID: user.ID, BookUUID: book.UUID, Body: "test note", Deleted: false} + testutils.MustExec(t, db.Save(¬e), "creating note") + + a := NewTest() + a.DB = db + + err := a.RemoveUser("alice@example.com") + + assert.Equal(t, err, ErrUserHasExistingResources, "should return ErrUserHasExistingResources") + + // Verify user was NOT deleted + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting users") + assert.Equal(t, userCount, int64(1), "user should not be deleted") + + }) + + t.Run("user has books", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "password123") + + book := database.Book{UserID: user.ID, Label: "testbook", Deleted: false} + testutils.MustExec(t, db.Save(&book), "creating book") + + a := NewTest() + a.DB = db + + err := a.RemoveUser("alice@example.com") + + assert.Equal(t, err, ErrUserHasExistingResources, "should return ErrUserHasExistingResources") + + // Verify user was NOT deleted + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting users") + assert.Equal(t, userCount, int64(1), "user should not be deleted") + + }) + + t.Run("user has deleted notes and books", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "password123") + + book := database.Book{UserID: user.ID, Label: "testbook", Deleted: false} + testutils.MustExec(t, db.Save(&book), "creating book") + + note := database.Note{UserID: user.ID, BookUUID: book.UUID, Body: "test note", Deleted: false} + testutils.MustExec(t, db.Save(¬e), "creating note") + + // Soft delete the note and book + testutils.MustExec(t, db.Model(¬e).Update("deleted", true), "soft deleting note") + testutils.MustExec(t, db.Model(&book).Update("deleted", true), "soft deleting book") + + a := NewTest() + a.DB = db + + err := a.RemoveUser("alice@example.com") + + assert.Equal(t, err, nil, "should not error when user only has deleted notes and books") + + // Verify user was deleted + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting users") + assert.Equal(t, userCount, int64(0), "user should be deleted") - assert.Equal(t, userCount, 1, "user count mismatch") - assert.Equal(t, accountCount, 1, "account count mismatch") }) } diff --git a/pkg/server/assets/embed.go b/pkg/server/assets/embed.go index 1a56d418..e0272d95 100644 --- a/pkg/server/assets/embed.go +++ b/pkg/server/assets/embed.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package assets diff --git a/pkg/server/assets/js/src/main.js b/pkg/server/assets/js/src/main.js index 9dabcf17..9e58d92a 100644 --- a/pkg/server/assets/js/src/main.js +++ b/pkg/server/assets/js/src/main.js @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ var getNextSibling = function (el, selector) { diff --git a/pkg/server/assets/package-lock.json b/pkg/server/assets/package-lock.json index 101aff52..7eb1c411 100644 --- a/pkg/server/assets/package-lock.json +++ b/pkg/server/assets/package-lock.json @@ -1,156 +1,494 @@ { "name": "assets", "version": "1.0.0", - "lockfileVersion": 1, + "lockfileVersion": 3, "requires": true, - "dependencies": { - "anymatch": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.2.tgz", - "integrity": "sha512-P43ePfOAIupkguHUycrc4qJ9kz8ZiuOUijaETwX7THt0Y/GNK7v0aa8rY816xWjZ7rJdA5XdMcpVFTKMq+RvWg==", - "dev": true, - "requires": { - "normalize-path": "^3.0.0", - "picomatch": "^2.0.4" + "packages": { + "": { + "name": "assets", + "version": "1.0.0", + "license": "Apache-2.0", + "devDependencies": { + "sass": "^1.50.1" } }, - "binary-extensions": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.2.0.tgz", - "integrity": "sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==", - "dev": true - }, - "braces": { - "version": "3.0.2", - "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", - "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "node_modules/@parcel/watcher": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher/-/watcher-2.5.1.tgz", + "integrity": "sha512-dfUnCxiN9H4ap84DvD2ubjw+3vUNpstxa0TneY/Paat8a3R4uQZDLSvWjmznAY/DoahqTHl9V46HF/Zs3F29pg==", "dev": true, - "requires": { - "fill-range": "^7.0.1" + "hasInstallScript": true, + "optional": true, + "dependencies": { + "detect-libc": "^1.0.3", + "is-glob": "^4.0.3", + "micromatch": "^4.0.5", + "node-addon-api": "^7.0.0" + }, + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + }, + "optionalDependencies": { + "@parcel/watcher-android-arm64": "2.5.1", + "@parcel/watcher-darwin-arm64": "2.5.1", + "@parcel/watcher-darwin-x64": "2.5.1", + "@parcel/watcher-freebsd-x64": "2.5.1", + "@parcel/watcher-linux-arm-glibc": "2.5.1", + "@parcel/watcher-linux-arm-musl": "2.5.1", + "@parcel/watcher-linux-arm64-glibc": "2.5.1", + "@parcel/watcher-linux-arm64-musl": "2.5.1", + "@parcel/watcher-linux-x64-glibc": "2.5.1", + "@parcel/watcher-linux-x64-musl": "2.5.1", + "@parcel/watcher-win32-arm64": "2.5.1", + "@parcel/watcher-win32-ia32": "2.5.1", + "@parcel/watcher-win32-x64": "2.5.1" } }, - "chokidar": { - "version": "3.5.3", - "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.5.3.tgz", - "integrity": "sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw==", + "node_modules/@parcel/watcher-android-arm64": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-android-arm64/-/watcher-android-arm64-2.5.1.tgz", + "integrity": "sha512-KF8+j9nNbUN8vzOFDpRMsaKBHZ/mcjEjMToVMJOhTozkDonQFFrRcfdLWn6yWKCmJKmdVxSgHiYvTCef4/qcBA==", + "cpu": [ + "arm64" + ], "dev": true, - "requires": { - "anymatch": "~3.1.2", - "braces": "~3.0.2", - "fsevents": "~2.3.2", - "glob-parent": "~5.1.2", - "is-binary-path": "~2.1.0", - "is-glob": "~4.0.1", - "normalize-path": "~3.0.0", - "readdirp": "~3.6.0" + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" } }, - "fill-range": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", - "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "node_modules/@parcel/watcher-darwin-arm64": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-darwin-arm64/-/watcher-darwin-arm64-2.5.1.tgz", + "integrity": "sha512-eAzPv5osDmZyBhou8PoF4i6RQXAfeKL9tjb3QzYuccXFMQU0ruIc/POh30ePnaOyD1UXdlKguHBmsTs53tVoPw==", + "cpu": [ + "arm64" + ], "dev": true, - "requires": { + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-darwin-x64": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-darwin-x64/-/watcher-darwin-x64-2.5.1.tgz", + "integrity": "sha512-1ZXDthrnNmwv10A0/3AJNZ9JGlzrF82i3gNQcWOzd7nJ8aj+ILyW1MTxVk35Db0u91oD5Nlk9MBiujMlwmeXZg==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-freebsd-x64": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-freebsd-x64/-/watcher-freebsd-x64-2.5.1.tgz", + "integrity": "sha512-SI4eljM7Flp9yPuKi8W0ird8TI/JK6CSxju3NojVI6BjHsTyK7zxA9urjVjEKJ5MBYC+bLmMcbAWlZ+rFkLpJQ==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-arm-glibc": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-arm-glibc/-/watcher-linux-arm-glibc-2.5.1.tgz", + "integrity": "sha512-RCdZlEyTs8geyBkkcnPWvtXLY44BCeZKmGYRtSgtwwnHR4dxfHRG3gR99XdMEdQ7KeiDdasJwwvNSF5jKtDwdA==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-arm-musl": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-arm-musl/-/watcher-linux-arm-musl-2.5.1.tgz", + "integrity": "sha512-6E+m/Mm1t1yhB8X412stiKFG3XykmgdIOqhjWj+VL8oHkKABfu/gjFj8DvLrYVHSBNC+/u5PeNrujiSQ1zwd1Q==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-arm64-glibc": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-arm64-glibc/-/watcher-linux-arm64-glibc-2.5.1.tgz", + "integrity": "sha512-LrGp+f02yU3BN9A+DGuY3v3bmnFUggAITBGriZHUREfNEzZh/GO06FF5u2kx8x+GBEUYfyTGamol4j3m9ANe8w==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-arm64-musl": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-arm64-musl/-/watcher-linux-arm64-musl-2.5.1.tgz", + "integrity": "sha512-cFOjABi92pMYRXS7AcQv9/M1YuKRw8SZniCDw0ssQb/noPkRzA+HBDkwmyOJYp5wXcsTrhxO0zq1U11cK9jsFg==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-x64-glibc": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-x64-glibc/-/watcher-linux-x64-glibc-2.5.1.tgz", + "integrity": "sha512-GcESn8NZySmfwlTsIur+49yDqSny2IhPeZfXunQi48DMugKeZ7uy1FX83pO0X22sHntJ4Ub+9k34XQCX+oHt2A==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-x64-musl": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-x64-musl/-/watcher-linux-x64-musl-2.5.1.tgz", + "integrity": "sha512-n0E2EQbatQ3bXhcH2D1XIAANAcTZkQICBPVaxMeaCVBtOpBZpWJuf7LwyWPSBDITb7In8mqQgJ7gH8CILCURXg==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-win32-arm64": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-win32-arm64/-/watcher-win32-arm64-2.5.1.tgz", + "integrity": "sha512-RFzklRvmc3PkjKjry3hLF9wD7ppR4AKcWNzH7kXR7GUe0Igb3Nz8fyPwtZCSquGrhU5HhUNDr/mKBqj7tqA2Vw==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-win32-ia32": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-win32-ia32/-/watcher-win32-ia32-2.5.1.tgz", + "integrity": "sha512-c2KkcVN+NJmuA7CGlaGD1qJh1cLfDnQsHjE89E60vUEMlqduHGCdCLJCID5geFVM0dOtA3ZiIO8BoEQmzQVfpQ==", + "cpu": [ + "ia32" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-win32-x64": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-win32-x64/-/watcher-win32-x64-2.5.1.tgz", + "integrity": "sha512-9lHBdJITeNR++EvSQVUcaZoWupyHfXe1jZvGZ06O/5MflPcuPLtEphScIBL+AiCWBO46tDSHzWyD0uDmmZqsgA==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/braces": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", + "dev": true, + "optional": true, + "dependencies": { + "fill-range": "^7.1.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/chokidar": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-4.0.3.tgz", + "integrity": "sha512-Qgzu8kfBvo+cA4962jnP1KkS6Dop5NS6g7R5LFYJr4b8Ub94PPQXUksCw9PvXoeXPRRddRNC5C1JQUR2SMGtnA==", + "dev": true, + "dependencies": { + "readdirp": "^4.0.1" + }, + "engines": { + "node": ">= 14.16.0" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/detect-libc": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-1.0.3.tgz", + "integrity": "sha512-pGjwhsmsp4kL2RTz08wcOlGN83otlqHeD/Z5T8GXZB+/YcpQ/dgo+lbU8ZsGxV0HIvqqxo9l7mqYwyYMD9bKDg==", + "dev": true, + "optional": true, + "bin": { + "detect-libc": "bin/detect-libc.js" + }, + "engines": { + "node": ">=0.10" + } + }, + "node_modules/fill-range": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", + "dev": true, + "optional": true, + "dependencies": { "to-regex-range": "^5.0.1" + }, + "engines": { + "node": ">=8" } }, - "fsevents": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", - "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "node_modules/immutable": { + "version": "5.1.5", + "resolved": "https://registry.npmjs.org/immutable/-/immutable-5.1.5.tgz", + "integrity": "sha512-t7xcm2siw+hlUM68I+UEOK+z84RzmN59as9DZ7P1l0994DKUWV7UXBMQZVxaoMSRQ+PBZbHCOoBt7a2wxOMt+A==", "dev": true, - "optional": true + "license": "MIT" }, - "glob-parent": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", - "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", - "dev": true, - "requires": { - "is-glob": "^4.0.1" - } - }, - "immutable": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/immutable/-/immutable-4.0.0.tgz", - "integrity": "sha512-zIE9hX70qew5qTUjSS7wi1iwj/l7+m54KWU247nhM3v806UdGj1yDndXj+IOYxxtW9zyLI+xqFNZjTuDaLUqFw==", - "dev": true - }, - "is-binary-path": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", - "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", - "dev": true, - "requires": { - "binary-extensions": "^2.0.0" - } - }, - "is-extglob": { + "node_modules/is-extglob": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", - "integrity": "sha1-qIwCU1eR8C7TfHahueqXc8gz+MI=", - "dev": true + "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", + "dev": true, + "optional": true, + "engines": { + "node": ">=0.10.0" + } }, - "is-glob": { + "node_modules/is-glob": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", "dev": true, - "requires": { + "optional": true, + "dependencies": { "is-extglob": "^2.1.1" + }, + "engines": { + "node": ">=0.10.0" } }, - "is-number": { + "node_modules/is-number": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", - "dev": true + "dev": true, + "optional": true, + "engines": { + "node": ">=0.12.0" + } }, - "normalize-path": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", - "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", - "dev": true + "node_modules/micromatch": { + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", + "dev": true, + "optional": true, + "dependencies": { + "braces": "^3.0.3", + "picomatch": "^2.3.1" + }, + "engines": { + "node": ">=8.6" + } }, - "picomatch": { + "node_modules/node-addon-api": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-7.1.1.tgz", + "integrity": "sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==", + "dev": true, + "optional": true + }, + "node_modules/picomatch": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", - "dev": true - }, - "readdirp": { - "version": "3.6.0", - "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", - "integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==", "dev": true, - "requires": { - "picomatch": "^2.2.1" + "optional": true, + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" } }, - "sass": { - "version": "1.50.1", - "resolved": "https://registry.npmjs.org/sass/-/sass-1.50.1.tgz", - "integrity": "sha512-noTnY41KnlW2A9P8sdwESpDmo+KBNkukI1i8+hOK3footBUcohNHtdOJbckp46XO95nuvcHDDZ+4tmOnpK3hjw==", + "node_modules/readdirp": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-4.1.2.tgz", + "integrity": "sha512-GDhwkLfywWL2s6vEjyhri+eXmfH6j1L7JE27WhqLeYzoh/A3DBaYGEj2H/HFZCn/kMfim73FXxEJTw06WtxQwg==", "dev": true, - "requires": { - "chokidar": ">=3.0.0 <4.0.0", - "immutable": "^4.0.0", + "engines": { + "node": ">= 14.18.0" + }, + "funding": { + "type": "individual", + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/sass": { + "version": "1.93.2", + "resolved": "https://registry.npmjs.org/sass/-/sass-1.93.2.tgz", + "integrity": "sha512-t+YPtOQHpGW1QWsh1CHQ5cPIr9lbbGZLZnbihP/D/qZj/yuV68m8qarcV17nvkOX81BCrvzAlq2klCQFZghyTg==", + "dev": true, + "dependencies": { + "chokidar": "^4.0.0", + "immutable": "^5.0.2", "source-map-js": ">=0.6.2 <2.0.0" + }, + "bin": { + "sass": "sass.js" + }, + "engines": { + "node": ">=14.0.0" + }, + "optionalDependencies": { + "@parcel/watcher": "^2.4.1" } }, - "source-map-js": { + "node_modules/source-map-js": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz", "integrity": "sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==", - "dev": true + "dev": true, + "engines": { + "node": ">=0.10.0" + } }, - "to-regex-range": { + "node_modules/to-regex-range": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", "dev": true, - "requires": { + "optional": true, + "dependencies": { "is-number": "^7.0.0" + }, + "engines": { + "node": ">=8.0" } } } diff --git a/pkg/server/assets/package.json b/pkg/server/assets/package.json index 0edcd180..1561f6da 100644 --- a/pkg/server/assets/package.json +++ b/pkg/server/assets/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": {}, "author": "Dnote", - "license": "AGPL-3.0-or-later", + "license": "Apache-2.0", "devDependencies": { "sass": "^1.50.1" } diff --git a/pkg/server/assets/styles/src/_books.scss b/pkg/server/assets/styles/src/_books.scss index 59153fba..10f9a8aa 100644 --- a/pkg/server/assets/styles/src/_books.scss +++ b/pkg/server/assets/styles/src/_books.scss @@ -1,29 +1,29 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ +@use "rem"; +@use "theme"; + .books-page { .books-content { - padding: rem(16px) rem(24px); - margin-top: rem(16px); + padding: rem.rem(16px) rem.rem(24px); + margin-top: rem.rem(16px); h1 { - border-bottom: 1px solid $lighter-gray; - margin-bottom: rem(12px); + border-bottom: 1px solid theme.$lighter-gray; + margin-bottom: rem.rem(12px); } } } diff --git a/pkg/server/assets/styles/src/_bootstrap.scss b/pkg/server/assets/styles/src/_bootstrap.scss index 9c1adee6..9ccc9a78 100644 --- a/pkg/server/assets/styles/src/_bootstrap.scss +++ b/pkg/server/assets/styles/src/_bootstrap.scss @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // From Bootstrap <4.3.1 diff --git a/pkg/server/assets/styles/src/_buttons.scss b/pkg/server/assets/styles/src/_buttons.scss index 03b546c9..e734b003 100644 --- a/pkg/server/assets/styles/src/_buttons.scss +++ b/pkg/server/assets/styles/src/_buttons.scss @@ -1,24 +1,23 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -@import './theme'; -@import './rem'; -@import './font'; +@use "sass:color"; +@use 'theme'; +@use 'rem'; +@use 'font'; +@use "responsive"; @mixin button($text-color, $background-color) { color: $text-color; @@ -26,7 +25,7 @@ &:not(:disabled):hover { color: $text-color; - background-color: darken($background-color, 5%); + background-color: color.adjust($background-color, $lightness: -5%); box-shadow: 0px 0px 4px 2px #cacaca; } } @@ -87,40 +86,40 @@ button:disabled { } .button-small { - @include font-size('small'); - padding: rem(4px) rem(12px); + @include font.font-size('small'); + padding: rem.rem(4px) rem.rem(12px); } .button-normal { // @include font-size('small'); - padding: rem(8px) rem(16px); + padding: rem.rem(8px) rem.rem(16px); } .button-large { - @include font-size('medium'); + @include font.font-size('medium'); - padding: rem(8px) rem(24px); + padding: rem.rem(8px) rem.rem(24px); - @include breakpoint(md) { - padding: rem(12px) rem(36px); + @include responsive.breakpoint(md) { + padding: rem.rem(12px) rem.rem(36px); } - @include breakpoint(lg) { - padding: rem(12px) rem(48px); + @include responsive.breakpoint(lg) { + padding: rem.rem(12px) rem.rem(48px); } } .button-xlarge { - @include font-size('x-large'); + @include font.font-size('x-large'); - padding: rem(16px) rem(24px); + padding: rem.rem(16px) rem.rem(24px); - @include breakpoint(md) { - padding: rem(12px) rem(36px); + @include responsive.breakpoint(md) { + padding: rem.rem(12px) rem.rem(36px); } - @include breakpoint(lg) { - padding: rem(16px) rem(48px); + @include responsive.breakpoint(lg) { + padding: rem.rem(16px) rem.rem(48px); } } @@ -133,23 +132,23 @@ button:disabled { } .button-second { - @include button($black, $second); + @include button(theme.$black, theme.$second); } .button-second-outline { - @include button-outline($black, $second); + @include button-outline(theme.$black, theme.$second); } .button-third { - @include button(#ffffff, $third); + @include button(#ffffff, theme.$third); } .button-third-outline { - @include button-outline($third, $third); + @include button-outline(theme.$third, theme.$third); } .button-danger { - @include button-outline($danger-text, $danger-text); + @include button-outline(theme.$danger-text, theme.$danger-text); font-weight: 600; } @@ -158,7 +157,7 @@ button:disabled { } .button ~ .button { - margin-left: rem(12px); + margin-left: rem.rem(12px); } .button-no-ui { @@ -173,10 +172,10 @@ button:disabled { } .button-link { - color: $link; + color: theme.$link; &:hover { - color: $link-hover; + color: theme.$link-hover; text-decoration: underline; } } diff --git a/pkg/server/assets/styles/src/_font.scss b/pkg/server/assets/styles/src/_font.scss index 256503e6..4818014a 100644 --- a/pkg/server/assets/styles/src/_font.scss +++ b/pkg/server/assets/styles/src/_font.scss @@ -1,22 +1,19 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -@import './responsive'; +@use 'responsive'; $lowDecay: 0.1; $medDecay: 0.15; @@ -95,12 +92,12 @@ $highDecay: 0.2; font-size: $smSizeValue * 1px; font-size: $smSizeValue * 0.1rem; - @include breakpoint(md) { + @include responsive.breakpoint(md) { font-size: $mdSizeValue * 1px; font-size: $mdSizeValue * 0.1rem; } - @include breakpoint(lg) { + @include responsive.breakpoint(lg) { font-size: $lgSizeValue * 1px; font-size: $lgSizeValue * 0.1rem; } diff --git a/pkg/server/assets/styles/src/_global.scss b/pkg/server/assets/styles/src/_global.scss index f2ac6aa2..7bad7d58 100644 --- a/pkg/server/assets/styles/src/_global.scss +++ b/pkg/server/assets/styles/src/_global.scss @@ -1,27 +1,30 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ +@use "font"; +@use "rem"; +@use "responsive"; +@use "theme"; +@use "variables"; + .main { position: relative; display: flex; flex-direction: column; - background: $lighter-gray; - min-height: calc(100vh - #{$header-height}); + background: theme.$lighter-gray; + min-height: calc(100vh - #{variables.$header-height}); // margin-bottom: $footer-height; &.nofooter { @@ -29,49 +32,49 @@ } &.noheader:not(.nofooter) { - min-height: calc(100vh - #{$footer-height}); + min-height: calc(100vh - #{variables.$footer-height}); } &.nofooter:not(.noheader) { - min-height: calc(100vh - #{$header-height}); + min-height: calc(100vh - #{variables.$header-height}); } &.nofooter.noheader { min-height: 100vh; } - @include breakpoint(lg) { + @include responsive.breakpoint(lg) { margin-bottom: 0; - min-height: calc(100vh - #{$header-height}); + min-height: calc(100vh - #{variables.$header-height}); } } /* partials */ .partial--time { - color: $gray; - @include font-size('small'); + color: theme.$gray; + @include font.font-size('small'); .mobile-text { - @include breakpoint(md) { + @include responsive.breakpoint(md) { display: none; } } .text { display: none; - @include breakpoint(md) { + @include responsive.breakpoint(md) { display: inherit; } } } .partial--page-toolbar { - @include breakpoint(lg) { - height: rem(48px); - border-radius: rem(4px); - background: $light; + @include responsive.breakpoint(lg) { + height: rem.rem(48px); + border-radius: rem.rem(4px); + background: theme.$light; box-shadow: 0 0 8px rgba(0, 0, 0, 0.14); &.bottom { - margin-top: rem(12px); + margin-top: rem.rem(12px); } } } diff --git a/pkg/server/assets/styles/src/_grid.scss b/pkg/server/assets/styles/src/_grid.scss index 9f053842..9e527582 100644 --- a/pkg/server/assets/styles/src/_grid.scss +++ b/pkg/server/assets/styles/src/_grid.scss @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ /*! diff --git a/pkg/server/assets/styles/src/_header.scss b/pkg/server/assets/styles/src/_header.scss index 00b140be..28374fb7 100644 --- a/pkg/server/assets/styles/src/_header.scss +++ b/pkg/server/assets/styles/src/_header.scss @@ -1,23 +1,24 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -@import './theme'; -@import './variables'; +@use "sass:color"; +@use 'theme'; +@use 'variables'; +@use "font"; +@use "rem"; +@use "responsive"; .header-wrapper { padding: 0; @@ -25,7 +26,7 @@ position: relative; display: flex; box-shadow: 0 1px 5px rgba(0, 0, 0, 0.2); - background: $first; + background: theme.$first; align-items: stretch; justify-content: space-between; flex: 1; @@ -33,13 +34,13 @@ position: sticky; top: 0; z-index: 4; - height: $header-height; + height: variables.$header-height; .container { height: 100%; } - @include breakpoint(md) { + @include responsive.breakpoint(md) { flex-direction: row; } @@ -60,15 +61,15 @@ .search-wrapper { align-items: center; display: flex; - margin-left: rem(32px); + margin-left: rem.rem(32px); } .search-input { - width: rem(356px); + width: rem.rem(356px); border: 0; padding: 4px 12px; - border-radius: rem(4px); - @include font-size('small'); + border-radius: rem.rem(4px); + @include font.font-size('small'); } .brand { @@ -81,7 +82,7 @@ } .main-nav { - margin-left: rem(32px); + margin-left: rem.rem(32px); display: flex; .list { @@ -94,22 +95,22 @@ } .nav-link { - @include font-size('small'); + @include font.font-size('small'); display: flex; font-weight: 600; align-items: center; - padding: 0 rem(16px); - color: $white; + padding: 0 rem.rem(16px); + color: theme.$white; &:hover { - color: $white; + color: theme.$white; text-decoration: none; - background: lighten($first, 10%); + background: color.adjust(theme.$first, $lightness: 10%); } } .nav-item { - @include font-size('small'); + @include font.font-size('small'); font-weight: 600; } } @@ -131,7 +132,7 @@ display: none; position: absolute; background-color: #f1f1f1; - width: rem(240px); + width: rem.rem(240px); background: #fff; border: 1px solid #d8d8d8; border-radius: 4px; @@ -154,15 +155,15 @@ } .account-dropdown-header { - @include font-size('small'); - color: $light-gray; - padding: rem(8px) rem(12px); + @include font.font-size('small'); + color: theme.$light-gray; + padding: rem.rem(8px) rem.rem(12px); display: block; margin-bottom: 0; white-space: nowrap; svg { - fill: $light-gray; + fill: theme.$light-gray; } .email { @@ -173,15 +174,15 @@ } .dropdown-link { - @include font-size('small'); + @include font.font-size('small'); white-space: pre; - padding: rem(8px) rem(14px); + padding: rem.rem(8px) rem.rem(14px); width: 100%; display: block; color: black; &:hover { - background: $lighter-gray; + background: theme.$lighter-gray; text-decoration: none; color: #0056b3; } @@ -192,7 +193,7 @@ } &:not(.disabled):focus { - background: $lighter-gray; + background: theme.$lighter-gray; color: #0056b3; outline: 1px dotted gray; } @@ -204,7 +205,7 @@ } .session-notice { - margin-left: rem(4px); + margin-left: rem.rem(4px); } } } diff --git a/pkg/server/assets/styles/src/_hljs.scss b/pkg/server/assets/styles/src/_hljs.scss index a29543bf..4decc572 100644 --- a/pkg/server/assets/styles/src/_hljs.scss +++ b/pkg/server/assets/styles/src/_hljs.scss @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ /* diff --git a/pkg/server/assets/styles/src/_home.scss b/pkg/server/assets/styles/src/_home.scss index 38b0e9eb..6ec13eaa 100644 --- a/pkg/server/assets/styles/src/_home.scss +++ b/pkg/server/assets/styles/src/_home.scss @@ -1,36 +1,35 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -@import './theme'; -@import './font'; +@use 'theme'; +@use 'font'; +@use "rem"; +@use "responsive"; .home-page { .note-group-list { flex-grow: 1; - @include breakpoint(lg) { - margin-top: rem(16px); + @include responsive.breakpoint(lg) { + margin-top: rem.rem(16px); } .note-group-list-empty { - padding: rem(40px) rem(16px); + padding: rem.rem(40px) rem.rem(16px); text-align: center; - color: $gray; + color: theme.$gray; } } @@ -40,29 +39,29 @@ box-shadow: 0 0 8px rgba(0, 0, 0, 0.14); &:not(:first-of-type) { - margin-top: rem(20px); + margin-top: rem.rem(20px); - @include breakpoint(md) { - margin-top: rem(24px); + @include responsive.breakpoint(md) { + margin-top: rem.rem(24px); } } .note-group-header { - @include font-size('small'); + @include font.font-size('small'); display: flex; justify-content: space-between; color: white; - padding: rem(12px) rem(16px); - background: $light; - color: $black; - border-bottom: 1px solid $border-color; + padding: rem.rem(12px) rem.rem(16px); + background: theme.$light; + color: theme.$black; + border-bottom: 1px solid theme.$border-color; border-top-left-radius: 4px; border-top-right-radius: 4px; } .date { font-weight: 600; - @include font-size('small'); + @include font.font-size('small'); } .mask { @@ -78,7 +77,7 @@ .header-date { font-weight: 600; - @include font-size('regular'); + @include font.font-size('regular'); } .header-count { font-weight: 300; @@ -101,23 +100,23 @@ background: white; position: relative; - border-bottom: 1px solid $border-color; + border-bottom: 1px solid theme.$border-color; .link { - color: $black; + color: theme.$black; display: block; - padding: rem(12px) rem(16px); + padding: rem.rem(12px) rem.rem(16px); border: 2px solid transparent; &:hover { text-decoration: none; - background: $light-blue; + background: theme.$light-blue; color: inherit; } } .meta { - line-height: rem(16px); + line-height: rem.rem(16px); } .body { @@ -131,11 +130,11 @@ } .note-content { - margin-top: rem(12px); + margin-top: rem.rem(12px); line-height: 1.6rem; overflow: hidden; text-overflow: ellipsis; - color: $gray; + color: theme.$gray; } .book-label { @@ -143,11 +142,11 @@ text-overflow: ellipsis; white-space: nowrap; font-weight: 700; - @include font-size('small'); + @include font.font-size('small'); width: 212px; - @include breakpoint('md') { + @include responsive.breakpoint('md') { width: 320px; } } @@ -155,7 +154,7 @@ .match { display: inline-block; background: #f7f77d; - padding: rem(4px) rem(4px); + padding: rem.rem(4px) rem.rem(4px); } } @@ -168,12 +167,12 @@ align-items: center; .paginator-info { - @include font-size('small'); - color: $gray; + @include font.font-size('small'); + color: theme.$gray; } .paginator-link { - padding: rem(12px) rem(12px); + padding: rem.rem(12px) rem.rem(12px); &.disabled { cursor: not-allowed; @@ -181,10 +180,10 @@ } .paginator-link-prev { - margin-left: rem(8px); + margin-left: rem.rem(8px); - @include breakpoint(md) { - margin-left: rem(20px); + @include responsive.breakpoint(md) { + margin-left: rem.rem(20px); } } diff --git a/pkg/server/assets/styles/src/_login.scss b/pkg/server/assets/styles/src/_login.scss index 490a2ee9..1b8bfa97 100644 --- a/pkg/server/assets/styles/src/_login.scss +++ b/pkg/server/assets/styles/src/_login.scss @@ -1,26 +1,24 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -@import './theme'; -@import './font'; +@use 'theme'; +@use 'font'; +@use "rem"; .auth-page { - background: $lighter-gray; + background: theme.$lighter-gray; text-align: center; min-height: 100vh; padding: 50px 0; @@ -30,8 +28,8 @@ } .heading { - color: $black; - @include font-size('2x-large'); + color: theme.$black; + @include font.font-size('2x-large'); font-weight: 300; margin-top: 12px; margin-bottom: 0; @@ -58,15 +56,15 @@ .callout { color: #7c7c7c; - @include font-size('small'); + @include font.font-size('small'); } .cta { - @include font-size('small'); + @include font.font-size('small'); } .panel { - border: 1px solid $border-color; - background: $white; + border: 1px solid theme.$border-color; + background: theme.$white; border-radius: 2px; padding: 20px; text-align: left; @@ -82,21 +80,21 @@ } } .label { - @include font-size('small'); + @include font.font-size('small'); font-weight: 600; width: 100%; margin-bottom: 0; } .forgot { - @include font-size('small'); + @include font.font-size('small'); float: right; font-weight: 400; } &.password-reset-page { .email-input { - margin-top: rem(16px); + margin-top: rem.rem(16px); } } diff --git a/pkg/server/assets/styles/src/_markdown.scss b/pkg/server/assets/styles/src/_markdown.scss index 670123fe..e1621ffa 100644 --- a/pkg/server/assets/styles/src/_markdown.scss +++ b/pkg/server/assets/styles/src/_markdown.scss @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ /* diff --git a/pkg/server/assets/styles/src/_marker.scss b/pkg/server/assets/styles/src/_marker.scss index 73fb119b..db583dd2 100644 --- a/pkg/server/assets/styles/src/_marker.scss +++ b/pkg/server/assets/styles/src/_marker.scss @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ .marker { diff --git a/pkg/server/assets/styles/src/_note.scss b/pkg/server/assets/styles/src/_note.scss index 35470939..87f6e790 100644 --- a/pkg/server/assets/styles/src/_note.scss +++ b/pkg/server/assets/styles/src/_note.scss @@ -1,24 +1,26 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ +@use "font"; +@use "rem"; +@use "responsive"; +@use "theme"; + .note-page { // min-height: calc(100vh - 57px); - background: $lighter-gray; + background: theme.$lighter-gray; flex-grow: 1; flex-basis: 0; @@ -38,8 +40,8 @@ display: flex; align-items: center; justify-content: space-between; - padding: rem(12px) rem(16px); - border-bottom: 1px solid $border-color; + padding: rem.rem(12px) rem.rem(16px); + border-bottom: 1px solid theme.$border-color; } .header-left, .header-right { @@ -52,27 +54,27 @@ } .content-wrapper { - padding: rem(12px) rem(16px); + padding: rem.rem(12px) rem.rem(16px); } .collapsed-content { - color: $light-gray; + color: theme.$light-gray; } .footer { display: flex; justify-content: space-between; align-items: center; - @include font-size('small'); - padding: rem(12px) rem(16px); + @include font.font-size('small'); + padding: rem.rem(12px) rem.rem(16px); } .ts { - color: $light-gray; + color: theme.$light-gray; } .ts-lead { display: none; - @include breakpoint(md) { + @include responsive.breakpoint(md) { display: inline; } } @@ -83,13 +85,13 @@ } .book-label { - @include font-size('medium'); + @include font.font-size('medium'); font-weight: 600; display: inline-block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; - color: $black; + color: theme.$black; a { color: inherit; @@ -103,17 +105,17 @@ // header .header { .book-label { - max-width: rem(200px); - margin-left: rem(12px); + max-width: rem.rem(200px); + margin-left: rem.rem(12px); - @include breakpoint(sm) { - max-width: rem(200px); + @include responsive.breakpoint(sm) { + max-width: rem.rem(200px); } - @include breakpoint(md) { - max-width: rem(420px); + @include responsive.breakpoint(md) { + max-width: rem.rem(420px); } - @include breakpoint(lg) { - max-width: rem(600px); + @include responsive.breakpoint(lg) { + max-width: rem.rem(600px); } } } diff --git a/pkg/server/assets/styles/src/_reboot.scss b/pkg/server/assets/styles/src/_reboot.scss index c38080e3..369cf5de 100644 --- a/pkg/server/assets/styles/src/_reboot.scss +++ b/pkg/server/assets/styles/src/_reboot.scss @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ /*! diff --git a/pkg/server/assets/styles/src/_rem.scss b/pkg/server/assets/styles/src/_rem.scss index dbc142fb..c2b914ff 100644 --- a/pkg/server/assets/styles/src/_rem.scss +++ b/pkg/server/assets/styles/src/_rem.scss @@ -1,20 +1,20 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ +@use "sass:list"; +@use "sass:map"; +@use "sass:meta"; /* MIT License @@ -24,6 +24,8 @@ The above copyright notice and this permission notice shall be included in all c THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +@use "sass:math"; + // assume 1 rem = 10 px // achieved by body { font-size: 62.5%; ) $rem-baseline: 10px !default; @@ -32,24 +34,24 @@ $rem-px-only: false !default; @function rem-separator($list, $separator: false) { @if $separator == "comma" or $separator == "space" { - @return append($list, null, $separator); + @return list.append($list, null, $separator); } - @if function-exists("list-separator") == true { - @return list-separator($list); + @if meta.function-exists("list-separator") == true { + @return list.separator($list); } // list-separator polyfill by Hugo Giraudel (https://sass-compatibility.github.io/#list_separator_function) $test-list: (); @each $item in $list { - $test-list: append($test-list, $item, space); + $test-list: list.append($test-list, $item, space); } @return if($test-list == $list, space, comma); } @mixin rem-baseline($zoom: 100%) { - font-size: $zoom / 16px * $rem-baseline; + font-size: math.div($zoom, 16px) * $rem-baseline; } @function rem-convert($to, $values...) { @@ -57,28 +59,28 @@ $rem-px-only: false !default; $separator: rem-separator($values); @each $value in $values { - @if type-of($value) == "number" and unit($value) == "rem" and $to == "px" { - $result: append($result, $value / 1rem * $rem-baseline, $separator); + @if meta.type-of($value) == "number" and math.unit($value) == "rem" and $to == "px" { + $result: list.append($result, math.div($value, 1rem) * $rem-baseline, $separator); } @else if - type-of($value) == + meta.type-of($value) == "number" and - unit($value) == + math.unit($value) == "px" and $to == "rem" { - $result: append($result, $value / $rem-baseline * 1rem, $separator); - } @else if type-of($value) == "list" { + $result: list.append($result, math.div($value, $rem-baseline) * 1rem, $separator); + } @else if meta.type-of($value) == "list" { $value-separator: rem-separator($value); $value: rem-convert($to, $value...); $value: rem-separator($value, $value-separator); - $result: append($result, $value, $separator); + $result: list.append($result, $value, $separator); } @else { - $result: append($result, $value, $separator); + $result: list.append($result, $value, $separator); } } - @return if(length($result) == 1, nth($result, 1), $result); + @return if(list.length($result) == 1, list.nth($result, 1), $result); } @function rem($values...) { @@ -90,9 +92,9 @@ $rem-px-only: false !default; } @mixin rem($properties, $values...) { - @if type-of($properties) == "map" { - @each $property in map-keys($properties) { - @include rem($property, map-get($properties, $property)); + @if meta.type-of($properties) == "map" { + @each $property in map.keys($properties) { + @include rem($property, map.get($properties, $property)); } } @else { @each $property in $properties { diff --git a/pkg/server/assets/styles/src/_responsive.scss b/pkg/server/assets/styles/src/_responsive.scss index 32bfe163..2ebbf0ab 100644 --- a/pkg/server/assets/styles/src/_responsive.scss +++ b/pkg/server/assets/styles/src/_responsive.scss @@ -1,54 +1,51 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -@import './variables'; +@use 'variables'; @mixin breakpoint($point) { @if $point == xl { - @media (min-width: $xl-breakpoint) { + @media (min-width: variables.$xl-breakpoint) { @content; } } @else if $point == lg { - @media (min-width: $lg-breakpoint) { + @media (min-width: variables.$lg-breakpoint) { @content; } } @else if $point == md { - @media (min-width: $md-breakpoint) { + @media (min-width: variables.$md-breakpoint) { @content; } } @else if $point == sm { - @media (min-width: $sm-breakpoint) { + @media (min-width: variables.$sm-breakpoint) { @content; } } @else if $point == smonly { - @media (min-width: $sm-breakpoint) and (max-width: $md-breakpoint - 1px) { + @media (min-width: variables.$sm-breakpoint) and (max-width: variables.$md-breakpoint - 1px) { @content; } } @else if $point == smdown { - @media (max-width: $md-breakpoint - 1px) { + @media (max-width: variables.$md-breakpoint - 1px) { @content; } } @else if $point == mdonly { - @media (min-width: $md-breakpoint) and (max-width: $lg-breakpoint - 1px) { + @media (min-width: variables.$md-breakpoint) and (max-width: variables.$lg-breakpoint - 1px) { @content; } } @else if $point == mddown { - @media (max-width: $lg-breakpoint - 1px) { + @media (max-width: variables.$lg-breakpoint - 1px) { @content; } } diff --git a/pkg/server/assets/styles/src/_select.scss b/pkg/server/assets/styles/src/_select.scss index 5507f9c8..a0dc5d8c 100644 --- a/pkg/server/assets/styles/src/_select.scss +++ b/pkg/server/assets/styles/src/_select.scss @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ /** diff --git a/pkg/server/assets/styles/src/_settings.scss b/pkg/server/assets/styles/src/_settings.scss index 535cea06..a965ecbb 100644 --- a/pkg/server/assets/styles/src/_settings.scss +++ b/pkg/server/assets/styles/src/_settings.scss @@ -1,32 +1,31 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -@import './theme'; -@import './font'; +@use 'theme'; +@use 'font'; +@use "rem"; +@use "responsive"; .settings-page { .sidebar { box-shadow: 0 1px 5px rgba(0, 0, 0, 0.2); background: white; - margin-bottom: rem(20px); - margin-top: rem(20px); + margin-bottom: rem.rem(20px); + margin-top: rem.rem(20px); - @include breakpoint(lg) { + @include responsive.breakpoint(lg) { margin-bottom: 0; margin-top: 0; } @@ -34,30 +33,30 @@ .sidebar-item { display: block; - padding: rem(12px) rem(16px); + padding: rem.rem(12px) rem.rem(16px); border-left: 4px solid transparent; - @include font-size('regular'); + @include font.font-size('regular'); &:hover { text-decoration: none; - background: $light; + background: theme.$light; } &.active { font-weight: 600; - border-left-color: $first; + border-left-color: theme.$first; } } .setting-section-wrapper { .header { - @include breakpoint(lg) { + @include responsive.breakpoint(lg) { display: none; } } .setting-section { - margin-top: rem(24px); + margin-top: rem.rem(24px); background: white; box-shadow: 0 0 8px rgba(0, 0, 0, 0.14); @@ -67,27 +66,27 @@ } .section-heading { - @include font-size('regular'); + @include font.font-size('regular'); font-weight: 600; - padding-bottom: rem(4px); - background: $light; - padding: rem(16px) rem(20px); + padding-bottom: rem.rem(4px); + background: theme.$light; + padding: rem.rem(16px) rem.rem(20px); } .section-content { - margin-top: rem(20px); + margin-top: rem.rem(20px); } .actions { - margin-top: rem(18px); + margin-top: rem.rem(18px); text-align: right; } } .setting-row { - padding: rem(16px) rem(20px); + padding: rem.rem(16px) rem.rem(20px); &:not(:last-child) { - border-bottom: 1px solid $border-color; + border-bottom: 1px solid theme.$border-color; } .setting-row-summary { @@ -95,7 +94,7 @@ flex-direction: column; // align-items: flex-start; - @include breakpoint(md) { + @include responsive.breakpoint(md) { flex-direction: row; justify-content: space-between; align-items: center; @@ -103,24 +102,24 @@ } .setting-row-main { - padding-top: rem(24px); + padding-top: rem.rem(24px); } .setting-name { font-weight: 400; - @include font-size('regular'); + @include font.font-size('regular'); margin-bottom: 0; } .setting-desc { margin-bottom: 0; - @include font-size('small'); - color: $gray; + @include font.font-size('small'); + color: theme.$gray; } .setting-action { display: flex; flex-direction: column; - @include breakpoint(md) { + @include responsive.breakpoint(md) { flex-direction: row; } } @@ -130,9 +129,9 @@ word-break: break-all; justify-content: space-between; align-items: center; - margin-top: rem(4px); + margin-top: rem.rem(4px); - @include breakpoint(md) { + @include responsive.breakpoint(md) { flex-direction: row; align-items: center; margin-top: 0; @@ -140,26 +139,26 @@ } .setting-edit { - color: $link; + color: theme.$link; padding: 0; &:hover { - color: $link-hover; + color: theme.$link-hover; } - @include breakpoint(md) { - margin-left: rem(16px); + @include responsive.breakpoint(md) { + margin-left: rem.rem(16px); } } .input-row { & ~ .input-row, .input-row { - margin-top: rem(12px); + margin-top: rem.rem(12px); } } } .email-verification-form { - margin-left: rem(12px); + margin-left: rem.rem(12px); } } diff --git a/pkg/server/assets/styles/src/_shared.scss b/pkg/server/assets/styles/src/_shared.scss index 0b1c6b41..3dcbcbe4 100644 --- a/pkg/server/assets/styles/src/_shared.scss +++ b/pkg/server/assets/styles/src/_shared.scss @@ -1,23 +1,22 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -@import './font'; -@import './responsive'; +@use 'font'; +@use 'responsive'; +@use "rem"; +@use "theme"; @keyframes holderPulse { 0% { @@ -46,7 +45,7 @@ input[type='email']:disabled, input[type='number']:disabled, input[type='password']:disabled, textarea:disabled { - background-color: $lighter-gray; + background-color: theme.$lighter-gray; cursor: not-allowed; } @@ -76,17 +75,17 @@ button { } .text-input { - border: 1px solid $border-color; - padding: rem(8px) rem(12px); + border: 1px solid theme.$border-color; + padding: rem.rem(8px) rem.rem(12px); position: relative; - border-radius: rem(4px); + border-radius: rem.rem(4px); display: block; &::placeholder { - color: $gray; + color: theme.$gray; } &:focus { - border-color: $light-blue; + border-color: theme.$light-blue; box-shadow: inset 0 1px 2px rgba(24, 31, 35, 0.075), 0 0 0 0.2em rgba(4, 100, 210, 0.3); outline: none; @@ -94,11 +93,11 @@ button { } .text-input-small { - padding: rem(4px) rem(12px); + padding: rem.rem(4px) rem.rem(12px); } .text-input-medium { - padding: rem(8px) rem(12px); + padding: rem.rem(8px) rem.rem(12px); } .text-input-stretch { @@ -110,10 +109,10 @@ button { } a { - color: $link; + color: theme.$link; &:hover { - color: $link-hover; + color: theme.$link-hover; } } @@ -129,12 +128,12 @@ h6 { // grid .container.mobile-fw { - @include breakpoint(mddown) { + @include responsive.breakpoint(mddown) { max-width: 100%; } } .container.mobile-nopadding { - @include breakpoint(mddown) { + @include responsive.breakpoint(mddown) { padding-left: 0; padding-right: 0; @@ -154,30 +153,30 @@ html body { } .page { - padding-top: rem(20px); - padding-bottom: rem(20px); + padding-top: rem.rem(20px); + padding-bottom: rem.rem(20px); &.page-mobile-full { padding-top: 0; padding-bottom: 0; - @include breakpoint(lg) { - padding-top: rem(32px); - padding-bottom: rem(32px); + @include responsive.breakpoint(lg) { + padding-top: rem.rem(32px); + padding-bottom: rem.rem(32px); } } } .page-header { - margin-top: rem(20px); + margin-top: rem.rem(20px); &.page-header-full { - margin-bottom: rem(20px); + margin-bottom: rem.rem(20px); } - @include breakpoint(lg) { + @include responsive.breakpoint(lg) { // padding: 0; - margin-bottom: rem(20px); + margin-bottom: rem.rem(20px); margin-top: 0; } } @@ -189,7 +188,7 @@ html body { background-repeat: no-repeat; background-position: right 8px center; background-size: 8px 10px; - border: 1px solid $border-color; + border: 1px solid theme.$border-color; min-height: 34px; padding: 6px 8px; padding-right: 24px; @@ -207,7 +206,7 @@ html body { &:disabled, &.form-select-disabled { background-image: url('data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAABAAAAAUCAYAAACEYr13AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAEKSURBVHgBzVTNDYIwFC4NB46OwAi4gY7gETgoE6gTGCcwTgAJ4efGCLCBjMAIXrmA3yOhQazQhJj4JQ0v7fte3/e1hbFfIk3TYxzHp6kc7dtCFEUW5/xBcdM0a9d1S1kel00mSWKCnIkkxDSnXADIMYYEU9O0zPf91WwB6L6NyB3atrUMw7hNFkCbFyROmXYYmypMDMNwo+t6ztSwtW27oEAXrXBuwu2rCht+WPgU7C8gPCBzYOBKhQS5FTwIKBYeQFeJoWyiKNYH5Co6OCuQr/0JdBuPVyElQCd7GRMb3B3HebsHHzexrmvyQvZwqjFZWsDzvCc62BFhSGYD3UMsfs6ToKOd+6EsxgtrtWLW4gUN3AAAAABJRU5ErkJggg=='); - background-color: $lighter-gray; + background-color: theme.$lighter-gray; } } @@ -215,12 +214,12 @@ html body { // width: 100%; width: auto; font-weight: 600; - margin-bottom: rem(4px); - @include font-size('small'); + margin-bottom: rem.rem(4px); + @include font.font-size('small'); } .page-heading { - @include font-size('x-large'); + @include font.font-size('x-large'); } .dropdown-caret { @@ -231,7 +230,7 @@ html body { border-right: 4px solid transparent; border-bottom: 0 solid transparent; border-left: 4px solid transparent; - margin-left: rem(8px); + margin-left: rem.rem(8px); } .divider { diff --git a/pkg/server/assets/styles/src/_theme.scss b/pkg/server/assets/styles/src/_theme.scss index 7f3712e3..5654cb7a 100644 --- a/pkg/server/assets/styles/src/_theme.scss +++ b/pkg/server/assets/styles/src/_theme.scss @@ -1,20 +1,18 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ +@use "sass:color"; // basic colors $black: #2a2a2a; @@ -26,7 +24,7 @@ $lighter-gray: #f3f3f3; $dark-gray: #637283; // primary colors -$first: #072a40; +$first: #333745; $second: #e7e7e7; $third: #0a4b73; @@ -35,7 +33,7 @@ $border-color: #d8d8d8; $border-color-light: $lighter-gray; $link: #6f53c0; -$link-hover: darken($link, 5%); +$link-hover: color.adjust($link, $lightness: -5%); $danger-text: #cb2431; $danger-background: #f8d7da; diff --git a/pkg/server/assets/styles/src/_variables.scss b/pkg/server/assets/styles/src/_variables.scss index b477f71e..490d9dbb 100644 --- a/pkg/server/assets/styles/src/_variables.scss +++ b/pkg/server/assets/styles/src/_variables.scss @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ $header-height: 60px; diff --git a/pkg/server/assets/styles/src/main.scss b/pkg/server/assets/styles/src/main.scss index 69476f5e..9afa9f6d 100644 --- a/pkg/server/assets/styles/src/main.scss +++ b/pkg/server/assets/styles/src/main.scss @@ -1,40 +1,37 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -@import './reboot'; -@import './grid'; -@import './bootstrap'; -@import './buttons'; -@import './responsive'; -@import './select'; -@import './shared'; -@import './marker'; -@import './rem'; -@import './markdown'; -@import './hljs'; +@use 'reboot'; +@use 'grid'; +@use 'bootstrap'; +@use 'buttons'; +@use 'responsive'; +@use 'select'; +@use 'shared'; +@use 'marker'; +@use 'rem'; +@use 'markdown'; +@use 'hljs'; -@import './login'; -@import './home'; -@import './note'; -@import './books'; -@import './settings'; -@import './header'; -@import './global'; +@use 'login'; +@use 'home'; +@use 'note'; +@use 'books'; +@use 'settings'; +@use 'header'; +@use 'global'; html { font-size: 62.5%; /* 1.0 rem = 10px */ @@ -74,11 +71,11 @@ img { } .container.mobile-nopadding { - @include breakpoint(mdonly) { + @include responsive.breakpoint(mdonly) { max-width: 100%; } - @include breakpoint(mddown) { + @include responsive.breakpoint(mddown) { padding-left: 0; padding-right: 0; @@ -137,7 +134,7 @@ img { } .input { - border-radius: rem(4px); + border-radius: rem.rem(4px); background-clip: padding-box; border: 1px solid #ced4da; transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out; diff --git a/pkg/server/buildinfo/info.go b/pkg/server/buildinfo/info.go index 4ac29050..4e6661f8 100644 --- a/pkg/server/buildinfo/info.go +++ b/pkg/server/buildinfo/info.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package buildinfo diff --git a/pkg/server/cmd/helpers.go b/pkg/server/cmd/helpers.go new file mode 100644 index 00000000..ba90a7ce --- /dev/null +++ b/pkg/server/cmd/helpers.go @@ -0,0 +1,134 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import ( + "flag" + "fmt" + "os" + + "github.com/dnote/dnote/pkg/clock" + "github.com/dnote/dnote/pkg/server/app" + "github.com/dnote/dnote/pkg/server/config" + "github.com/dnote/dnote/pkg/server/database" + "github.com/dnote/dnote/pkg/server/log" + "github.com/dnote/dnote/pkg/server/mailer" + "gorm.io/gorm" +) + +func initDB(dbPath string) *gorm.DB { + db := database.Open(dbPath) + database.InitSchema(db) + database.Migrate(db) + + return db +} + +func getEmailBackend() mailer.Backend { + defaultBackend, err := mailer.NewDefaultBackend() + if err != nil { + log.Debug("SMTP not configured, using StdoutBackend for emails") + return mailer.NewStdoutBackend() + } + + log.Debug("Email backend configured") + return defaultBackend +} + +func initApp(cfg config.Config) app.App { + db := initDB(cfg.DBPath) + emailBackend := getEmailBackend() + + return app.App{ + DB: db, + Clock: clock.New(), + EmailBackend: emailBackend, + HTTP500Page: cfg.HTTP500Page, + BaseURL: cfg.BaseURL, + DisableRegistration: cfg.DisableRegistration, + Port: cfg.Port, + DBPath: cfg.DBPath, + AssetBaseURL: cfg.AssetBaseURL, + } +} + +// printFlags prints flags with -- prefix for consistency with CLI +func printFlags(fs *flag.FlagSet) { + fs.VisitAll(func(f *flag.Flag) { + fmt.Printf(" --%s", f.Name) + + // Print type hint for non-boolean flags + name, usage := flag.UnquoteUsage(f) + if name != "" { + fmt.Printf(" %s", name) + } + fmt.Println() + + // Print usage description with indentation + if usage != "" { + fmt.Printf(" \t%s", usage) + if f.DefValue != "" && f.DefValue != "false" { + fmt.Printf(" (default: %s)", f.DefValue) + } + fmt.Println() + } + }) +} + +// setupFlagSet creates a FlagSet with standard usage format +func setupFlagSet(name, usageCmd string) *flag.FlagSet { + fs := flag.NewFlagSet(name, flag.ExitOnError) + fs.Usage = func() { + fmt.Printf(`Usage: + %s [flags] + +Flags: +`, usageCmd) + printFlags(fs) + } + return fs +} + +// requireString validates that a required string flag is not empty +func requireString(fs *flag.FlagSet, value, fieldName string) { + if value == "" { + fmt.Printf("Error: %s is required\n", fieldName) + fs.Usage() + os.Exit(1) + } +} + +// createApp creates config, initializes app, and returns cleanup function +func createApp(fs *flag.FlagSet, dbPath string) (*app.App, func()) { + cfg, err := config.New(config.Params{ + DBPath: dbPath, + }) + if err != nil { + fmt.Printf("Error: %s\n\n", err) + fs.Usage() + os.Exit(1) + } + + a := initApp(cfg) + cleanup := func() { + sqlDB, err := a.DB.DB() + if err == nil { + sqlDB.Close() + } + } + + return &a, cleanup +} diff --git a/pkg/server/cmd/root.go b/pkg/server/cmd/root.go new file mode 100644 index 00000000..106ab2d9 --- /dev/null +++ b/pkg/server/cmd/root.go @@ -0,0 +1,57 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import ( + "fmt" + "os" +) + +func rootCmd() { + fmt.Printf(`Dnote server - a simple command line notebook + +Usage: + dnote-server [command] [flags] + +Available commands: + start: Start the server (use 'dnote-server start --help' for flags) + user: Manage users (use 'dnote-server user' for subcommands) + version: Print the version +`) +} + +// Execute is the main entry point for the CLI +func Execute() { + if len(os.Args) < 2 { + rootCmd() + return + } + + cmd := os.Args[1] + + switch cmd { + case "start": + startCmd(os.Args[2:]) + case "user": + userCmd(os.Args[2:]) + case "version": + versionCmd() + default: + fmt.Printf("Unknown command %s\n", cmd) + rootCmd() + os.Exit(1) + } +} diff --git a/pkg/server/cmd/start.go b/pkg/server/cmd/start.go new file mode 100644 index 00000000..0a4e5292 --- /dev/null +++ b/pkg/server/cmd/start.go @@ -0,0 +1,94 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import ( + "fmt" + "net/http" + "os" + "time" + + "github.com/dnote/dnote/pkg/server/buildinfo" + "github.com/dnote/dnote/pkg/server/config" + "github.com/dnote/dnote/pkg/server/controllers" + "github.com/dnote/dnote/pkg/server/database" + "github.com/dnote/dnote/pkg/server/log" + "github.com/pkg/errors" +) + +func startCmd(args []string) { + fs := setupFlagSet("start", "dnote-server start") + + port := fs.String("port", "", "Server port (env: PORT, default: 3001)") + baseURL := fs.String("baseUrl", "", "Full URL to server without trailing slash (env: BaseURL, default: http://localhost:3001)") + dbPath := fs.String("dbPath", "", "Path to SQLite database file (env: DBPath, default: $XDG_DATA_HOME/dnote/server.db)") + disableRegistration := fs.Bool("disableRegistration", false, "Disable user registration (env: DisableRegistration, default: false)") + logLevel := fs.String("logLevel", "", "Log level: debug, info, warn, or error (env: LOG_LEVEL, default: info)") + + fs.Parse(args) + + cfg, err := config.New(config.Params{ + Port: *port, + BaseURL: *baseURL, + DBPath: *dbPath, + DisableRegistration: *disableRegistration, + LogLevel: *logLevel, + }) + if err != nil { + fmt.Printf("Error: %s\n\n", err) + fs.Usage() + os.Exit(1) + } + + // Set log level + log.SetLevel(cfg.LogLevel) + + app := initApp(cfg) + defer func() { + sqlDB, err := app.DB.DB() + if err == nil { + sqlDB.Close() + } + }() + + // Start WAL checkpointing to prevent WAL file from growing unbounded. + database.StartWALCheckpointing(app.DB, 5*time.Minute) + + // Start periodic VACUUM to reclaim space and defragment database. + database.StartPeriodicVacuum(app.DB, 24*time.Hour) + + ctl := controllers.New(&app) + rc := controllers.RouteConfig{ + WebRoutes: controllers.NewWebRoutes(&app, ctl), + APIRoutes: controllers.NewAPIRoutes(&app, ctl), + Controllers: ctl, + } + + r, err := controllers.NewRouter(&app, rc) + if err != nil { + panic(errors.Wrap(err, "initializing router")) + } + + log.WithFields(log.Fields{ + "version": buildinfo.Version, + "port": cfg.Port, + }).Info("Dnote server starting") + + if err := http.ListenAndServe(fmt.Sprintf(":%s", cfg.Port), r); err != nil { + log.ErrorWrap(err, "server failed") + os.Exit(1) + } +} diff --git a/pkg/server/cmd/user.go b/pkg/server/cmd/user.go new file mode 100644 index 00000000..7a98344d --- /dev/null +++ b/pkg/server/cmd/user.go @@ -0,0 +1,212 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import ( + "fmt" + "io" + "os" + + "github.com/dnote/dnote/pkg/prompt" + "github.com/dnote/dnote/pkg/server/app" + "github.com/dnote/dnote/pkg/server/log" + "github.com/pkg/errors" +) + +// confirm prompts for user input to confirm a choice +func confirm(r io.Reader, question string, optimistic bool) (bool, error) { + message := prompt.FormatQuestion(question, optimistic) + fmt.Print(message + " ") + + confirmed, err := prompt.ReadYesNo(r, optimistic) + if err != nil { + return false, errors.Wrap(err, "reading stdin") + } + + return confirmed, nil +} + +func userCreateCmd(args []string) { + fs := setupFlagSet("create", "dnote-server user create") + + email := fs.String("email", "", "User email address (required)") + password := fs.String("password", "", "User password (required)") + dbPath := fs.String("dbPath", "", "Path to SQLite database file (env: DBPath, default: $XDG_DATA_HOME/dnote/server.db)") + + fs.Parse(args) + + requireString(fs, *email, "email") + requireString(fs, *password, "password") + + a, cleanup := createApp(fs, *dbPath) + defer cleanup() + + _, err := a.CreateUser(*email, *password, *password) + if err != nil { + log.ErrorWrap(err, "creating user") + os.Exit(1) + } + + fmt.Printf("User created successfully\n") + fmt.Printf("Email: %s\n", *email) +} + +func userRemoveCmd(args []string, stdin io.Reader) { + fs := setupFlagSet("remove", "dnote-server user remove") + + email := fs.String("email", "", "User email address (required)") + dbPath := fs.String("dbPath", "", "Path to SQLite database file (env: DBPath, default: $XDG_DATA_HOME/dnote/server.db)") + + fs.Parse(args) + + requireString(fs, *email, "email") + + a, cleanup := createApp(fs, *dbPath) + defer cleanup() + + // Check if user exists first + _, err := a.GetUserByEmail(*email) + if err != nil { + if errors.Is(err, app.ErrNotFound) { + fmt.Printf("Error: user with email %s not found\n", *email) + } else { + log.ErrorWrap(err, "finding user") + } + os.Exit(1) + } + + // Show confirmation prompt + ok, err := confirm(stdin, fmt.Sprintf("Remove user %s?", *email), false) + if err != nil { + log.ErrorWrap(err, "getting confirmation") + os.Exit(1) + } + if !ok { + fmt.Println("Aborted by user") + os.Exit(0) + } + + // Remove the user + if err := a.RemoveUser(*email); err != nil { + if errors.Is(err, app.ErrNotFound) { + fmt.Printf("Error: user with email %s not found\n", *email) + } else if errors.Is(err, app.ErrUserHasExistingResources) { + fmt.Printf("Error: %s\n", err) + } else { + log.ErrorWrap(err, "removing user") + } + os.Exit(1) + } + + fmt.Printf("User removed successfully\n") + fmt.Printf("Email: %s\n", *email) +} + +func userResetPasswordCmd(args []string) { + fs := setupFlagSet("reset-password", "dnote-server user reset-password") + + email := fs.String("email", "", "User email address (required)") + password := fs.String("password", "", "New password (required)") + dbPath := fs.String("dbPath", "", "Path to SQLite database file (env: DBPath, default: $XDG_DATA_HOME/dnote/server.db)") + + fs.Parse(args) + + requireString(fs, *email, "email") + requireString(fs, *password, "password") + + a, cleanup := createApp(fs, *dbPath) + defer cleanup() + + // Find the user + user, err := a.GetUserByEmail(*email) + if err != nil { + if errors.Is(err, app.ErrNotFound) { + fmt.Printf("Error: user with email %s not found\n", *email) + } else { + log.ErrorWrap(err, "finding user") + } + os.Exit(1) + } + + // Update the password + if err := app.UpdateUserPassword(a.DB, user, *password); err != nil { + log.ErrorWrap(err, "updating password") + os.Exit(1) + } + + fmt.Printf("Password reset successfully\n") + fmt.Printf("Email: %s\n", *email) +} + +func userListCmd(args []string, output io.Writer) { + fs := setupFlagSet("list", "dnote-server user list") + + dbPath := fs.String("dbPath", "", "Path to SQLite database file (env: DBPath, default: $XDG_DATA_HOME/dnote/server.db)") + + fs.Parse(args) + + a, cleanup := createApp(fs, *dbPath) + defer cleanup() + + users, err := a.GetAllUsers() + if err != nil { + log.ErrorWrap(err, "listing users") + os.Exit(1) + } + + for _, user := range users { + fmt.Fprintf(output, "%s,%s,%s\n", user.UUID, user.Email.String, user.CreatedAt.UTC().Format("2006-01-02T15:04:05Z")) + } +} + +func userCmd(args []string) { + if len(args) < 1 { + fmt.Println(`Usage: + dnote-server user [command] + +Available commands: + create: Create a new user + list: List all users + remove: Remove a user + reset-password: Reset a user's password`) + os.Exit(1) + } + + subcommand := args[0] + subArgs := []string{} + if len(args) > 1 { + subArgs = args[1:] + } + + switch subcommand { + case "create": + userCreateCmd(subArgs) + case "list": + userListCmd(subArgs, os.Stdout) + case "remove": + userRemoveCmd(subArgs, os.Stdin) + case "reset-password": + userResetPasswordCmd(subArgs) + default: + fmt.Printf("Unknown subcommand: %s\n\n", subcommand) + fmt.Println(`Available commands: + create: Create a new user + list: List all users + remove: Remove a user (only if they have no notes or books) + reset-password: Reset a user's password`) + os.Exit(1) + } +} diff --git a/pkg/server/cmd/user_test.go b/pkg/server/cmd/user_test.go new file mode 100644 index 00000000..84e5f4de --- /dev/null +++ b/pkg/server/cmd/user_test.go @@ -0,0 +1,158 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import ( + "bytes" + "fmt" + "strings" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/server/database" + "github.com/dnote/dnote/pkg/server/testutils" + "golang.org/x/crypto/bcrypt" +) + +func TestUserCreateCmd(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + // Call the function directly + userCreateCmd([]string{"--dbPath", tmpDB, "--email", "test@example.com", "--password", "password123"}) + + // Verify user was created in database + db := testutils.InitDB(tmpDB) + defer func() { + sqlDB, _ := db.DB() + sqlDB.Close() + }() + + var count int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&count), "counting users") + assert.Equal(t, count, int64(1), "should have 1 user") + + var user database.User + testutils.MustExec(t, db.Where("email = ?", "test@example.com").First(&user), "finding user") + assert.Equal(t, user.Email.String, "test@example.com", "email mismatch") +} + +func TestUserRemoveCmd(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + // Create a user first + db := testutils.InitDB(tmpDB) + testutils.SetupUserData(db, "test@example.com", "password123") + sqlDB, _ := db.DB() + sqlDB.Close() + + // Remove the user with mock stdin that responds "y" + mockStdin := strings.NewReader("y\n") + userRemoveCmd([]string{"--dbPath", tmpDB, "--email", "test@example.com"}, mockStdin) + + // Verify user was removed + db2 := testutils.InitDB(tmpDB) + defer func() { + sqlDB2, _ := db2.DB() + sqlDB2.Close() + }() + + var count int64 + testutils.MustExec(t, db2.Model(&database.User{}).Count(&count), "counting users") + assert.Equal(t, count, int64(0), "should have 0 users") +} + +func TestUserResetPasswordCmd(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + // Create a user first + db := testutils.InitDB(tmpDB) + user := testutils.SetupUserData(db, "test@example.com", "oldpassword123") + oldPasswordHash := user.Password.String + sqlDB, _ := db.DB() + sqlDB.Close() + + // Reset password + userResetPasswordCmd([]string{"--dbPath", tmpDB, "--email", "test@example.com", "--password", "newpassword123"}) + + // Verify password was changed + db2 := testutils.InitDB(tmpDB) + defer func() { + sqlDB2, _ := db2.DB() + sqlDB2.Close() + }() + + var updatedUser database.User + testutils.MustExec(t, db2.Where("email = ?", "test@example.com").First(&updatedUser), "finding user") + + // Verify password hash changed + assert.Equal(t, updatedUser.Password.String != oldPasswordHash, true, "password hash should be different") + assert.Equal(t, len(updatedUser.Password.String) > 0, true, "password should be set") + + // Verify new password works + err := bcrypt.CompareHashAndPassword([]byte(updatedUser.Password.String), []byte("newpassword123")) + assert.Equal(t, err, nil, "new password should match") + + // Verify old password doesn't work + err = bcrypt.CompareHashAndPassword([]byte(updatedUser.Password.String), []byte("oldpassword123")) + assert.Equal(t, err != nil, true, "old password should not match") +} + +func TestUserListCmd(t *testing.T) { + t.Run("multiple users", func(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + // Create multiple users + db := testutils.InitDB(tmpDB) + user1 := testutils.SetupUserData(db, "alice@example.com", "password123") + user2 := testutils.SetupUserData(db, "bob@example.com", "password123") + user3 := testutils.SetupUserData(db, "charlie@example.com", "password123") + sqlDB, _ := db.DB() + sqlDB.Close() + + // Capture output + var buf bytes.Buffer + userListCmd([]string{"--dbPath", tmpDB}, &buf) + + // Verify output matches expected format + output := strings.TrimSpace(buf.String()) + lines := strings.Split(output, "\n") + + expectedLine1 := fmt.Sprintf("%s,alice@example.com,%s", user1.UUID, user1.CreatedAt.UTC().Format("2006-01-02T15:04:05Z")) + expectedLine2 := fmt.Sprintf("%s,bob@example.com,%s", user2.UUID, user2.CreatedAt.UTC().Format("2006-01-02T15:04:05Z")) + expectedLine3 := fmt.Sprintf("%s,charlie@example.com,%s", user3.UUID, user3.CreatedAt.UTC().Format("2006-01-02T15:04:05Z")) + + assert.Equal(t, lines[0], expectedLine1, "line 1 should match") + assert.Equal(t, lines[1], expectedLine2, "line 2 should match") + assert.Equal(t, lines[2], expectedLine3, "line 3 should match") + }) + + t.Run("empty database", func(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + // Initialize empty database + db := testutils.InitDB(tmpDB) + sqlDB, _ := db.DB() + sqlDB.Close() + + // Capture output + var buf bytes.Buffer + userListCmd([]string{"--dbPath", tmpDB}, &buf) + + // Verify no output + output := buf.String() + assert.Equal(t, output, "", "should have no output for empty database") + }) +} diff --git a/pkg/server/cmd/version.go b/pkg/server/cmd/version.go new file mode 100644 index 00000000..c36405d4 --- /dev/null +++ b/pkg/server/cmd/version.go @@ -0,0 +1,26 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cmd + +import ( + "fmt" + + "github.com/dnote/dnote/pkg/server/buildinfo" +) + +func versionCmd() { + fmt.Printf("dnote-server-%s\n", buildinfo.Version) +} diff --git a/pkg/server/config/config.go b/pkg/server/config/config.go index 2d92521e..19ee45f2 100644 --- a/pkg/server/config/config.go +++ b/pkg/server/config/config.go @@ -1,191 +1,117 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package config import ( - "fmt" "net/url" "os" + "path/filepath" + "github.com/dnote/dnote/pkg/dirs" "github.com/dnote/dnote/pkg/server/assets" "github.com/pkg/errors" ) const ( - // AppEnvProduction represents an app environment for production. - AppEnvProduction string = "PRODUCTION" + // DefaultDBDir is the default directory name for Dnote data + DefaultDBDir = "dnote" + // DefaultDBFilename is the default database filename + DefaultDBFilename = "server.db" ) var ( - // ErrDBMissingHost is an error for an incomplete configuration missing the host - ErrDBMissingHost = errors.New("DB Host is empty") - // ErrDBMissingPort is an error for an incomplete configuration missing the port - ErrDBMissingPort = errors.New("DB Port is empty") - // ErrDBMissingName is an error for an incomplete configuration missing the name - ErrDBMissingName = errors.New("DB Name is empty") - // ErrDBMissingUser is an error for an incomplete configuration missing the user - ErrDBMissingUser = errors.New("DB User is empty") - // ErrWebURLInvalid is an error for an incomplete configuration with invalid web url - ErrWebURLInvalid = errors.New("Invalid WebURL") + // DefaultDBPath is the default path to the database file + DefaultDBPath = filepath.Join(dirs.DataHome, DefaultDBDir, DefaultDBFilename) +) + +var ( + // ErrDBMissingPath is an error for an incomplete configuration missing the database path + ErrDBMissingPath = errors.New("DB Path is empty") + // ErrBaseURLInvalid is an error for an incomplete configuration with invalid base url + ErrBaseURLInvalid = errors.New("Invalid BaseURL") // ErrPortInvalid is an error for an incomplete configuration with invalid port ErrPortInvalid = errors.New("Invalid Port") ) -// PostgresConfig holds the postgres connection configuration. -type PostgresConfig struct { - SSLMode string - Host string - Port string - Name string - User string - Password string -} - func readBoolEnv(name string) bool { - if os.Getenv(name) == "true" { - return true - } - - return false + return os.Getenv(name) == "true" } -// checkSSLMode checks if SSL is required for the database connection -func checkSSLMode() bool { - // TODO: deprecate DB_NOSSL in favor of DBSkipSSL - if os.Getenv("DB_NOSSL") != "" { - return true +// getOrEnv returns value if non-empty, otherwise env var, otherwise default +func getOrEnv(value, envKey, defaultVal string) string { + if value != "" { + return value } - - if os.Getenv("DBSkipSSL") == "true" { - return true - } - - return os.Getenv("GO_ENV") != "PRODUCTION" -} - -func loadDBConfig() PostgresConfig { - var sslmode string - if checkSSLMode() { - sslmode = "disable" - } else { - sslmode = "require" - } - - return PostgresConfig{ - SSLMode: sslmode, - Host: os.Getenv("DBHost"), - Port: os.Getenv("DBPort"), - Name: os.Getenv("DBName"), - User: os.Getenv("DBUser"), - Password: os.Getenv("DBPassword"), + if env := os.Getenv(envKey); env != "" { + return env } + return defaultVal } // Config is an application configuration type Config struct { - AppEnv string - WebURL string - OnPremise bool + BaseURL string DisableRegistration bool Port string - DB PostgresConfig + DBPath string AssetBaseURL string HTTP500Page []byte + LogLevel string } -func getAppEnv() string { - // DEPRECATED - goEnv := os.Getenv("GO_ENV") - if goEnv != "" { - return goEnv - } - - return os.Getenv("APP_ENV") +// Params are the configuration parameters for creating a new Config +type Params struct { + Port string + BaseURL string + DBPath string + DisableRegistration bool + LogLevel string } -// Load constructs and returns a new config based on the environment variables. -func Load() Config { - port := os.Getenv("PORT") - if port == "" { - port = "3000" - } - +// New constructs and returns a new validated config. +// Empty string params will fall back to environment variables and defaults. +func New(p Params) (Config, error) { c := Config{ - AppEnv: getAppEnv(), - WebURL: os.Getenv("WebURL"), - Port: port, - OnPremise: readBoolEnv("OnPremise"), - DisableRegistration: readBoolEnv("DisableRegistration"), - DB: loadDBConfig(), - AssetBaseURL: "", + Port: getOrEnv(p.Port, "PORT", "3001"), + BaseURL: getOrEnv(p.BaseURL, "BaseURL", "http://localhost:3001"), + DBPath: getOrEnv(p.DBPath, "DBPath", DefaultDBPath), + DisableRegistration: p.DisableRegistration || readBoolEnv("DisableRegistration"), + LogLevel: getOrEnv(p.LogLevel, "LOG_LEVEL", "info"), + AssetBaseURL: "/static", HTTP500Page: assets.MustGetHTTP500ErrorPage(), } if err := validate(c); err != nil { - panic(err) + return Config{}, err } - return c -} - -// SetOnPremise sets the OnPremise value -func (c *Config) SetOnPremise(val bool) { - c.OnPremise = val -} - -// SetAssetBaseURL sets static dir for the confi -func (c *Config) SetAssetBaseURL(d string) { - c.AssetBaseURL = d -} - -// IsProd checks if the app environment is configured to be production. -func (c Config) IsProd() bool { - return c.AppEnv == AppEnvProduction + return c, nil } func validate(c Config) error { - if _, err := url.ParseRequestURI(c.WebURL); err != nil { - return errors.Wrapf(ErrWebURLInvalid, "provided: '%s'", c.WebURL) + if _, err := url.ParseRequestURI(c.BaseURL); err != nil { + return errors.Wrapf(ErrBaseURLInvalid, "'%s'", c.BaseURL) } if c.Port == "" { return ErrPortInvalid } - if c.DB.Host == "" { - return ErrDBMissingHost - } - if c.DB.Port == "" { - return ErrDBMissingPort - } - if c.DB.Name == "" { - return ErrDBMissingName - } - if c.DB.User == "" { - return ErrDBMissingUser + if c.DBPath == "" { + return ErrDBMissingPath } return nil } - -// GetConnectionStr returns a postgres connection string. -func (c PostgresConfig) GetConnectionStr() string { - return fmt.Sprintf( - "sslmode=%s host=%s port=%s dbname=%s user=%s password=%s", - c.SSLMode, c.Host, c.Port, c.Name, c.User, c.Password) -} diff --git a/pkg/server/config/config_test.go b/pkg/server/config/config_test.go index 57be4d9b..9c3c1baa 100644 --- a/pkg/server/config/config_test.go +++ b/pkg/server/config/config_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package config @@ -33,85 +30,30 @@ func TestValidate(t *testing.T) { }{ { config: Config{ - DB: PostgresConfig{ - Host: "mockHost", - Port: "5432", - Name: "mockDB", - User: "mockUser", - }, - WebURL: "http://mock.url", - Port: "3000", + DBPath: "test.db", + BaseURL: "http://mock.url", + Port: "3000", }, expectedErr: nil, }, { config: Config{ - DB: PostgresConfig{ - Port: "5432", - Name: "mockDB", - User: "mockUser", - }, - WebURL: "http://mock.url", - Port: "3000", + DBPath: "", + BaseURL: "http://mock.url", + Port: "3000", }, - expectedErr: ErrDBMissingHost, + expectedErr: ErrDBMissingPath, }, { config: Config{ - DB: PostgresConfig{ - Host: "mockHost", - Name: "mockDB", - User: "mockUser", - }, - WebURL: "http://mock.url", - Port: "3000", + DBPath: "test.db", }, - expectedErr: ErrDBMissingPort, + expectedErr: ErrBaseURLInvalid, }, { config: Config{ - DB: PostgresConfig{ - Host: "mockHost", - Port: "5432", - User: "mockUser", - }, - WebURL: "http://mock.url", - Port: "3000", - }, - expectedErr: ErrDBMissingName, - }, - { - config: Config{ - DB: PostgresConfig{ - Host: "mockHost", - Port: "5432", - Name: "mockDB", - }, - WebURL: "http://mock.url", - Port: "3000", - }, - expectedErr: ErrDBMissingUser, - }, - { - config: Config{ - DB: PostgresConfig{ - Host: "mockHost", - Port: "5432", - Name: "mockDB", - User: "mockUser", - }, - }, - expectedErr: ErrWebURLInvalid, - }, - { - config: Config{ - DB: PostgresConfig{ - Host: "mockHost", - Port: "5432", - Name: "mockDB", - User: "mockUser", - }, - WebURL: "http://mock.url", + DBPath: "test.db", + BaseURL: "http://mock.url", }, expectedErr: ErrPortInvalid, }, diff --git a/pkg/server/consts/consts.go b/pkg/server/consts/consts.go index cde14c02..1f464951 100644 --- a/pkg/server/consts/consts.go +++ b/pkg/server/consts/consts.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package consts diff --git a/pkg/server/context/user.go b/pkg/server/context/user.go index afe6c76e..b58d40e9 100644 --- a/pkg/server/context/user.go +++ b/pkg/server/context/user.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package context @@ -25,9 +22,8 @@ import ( ) const ( - userKey privateKey = "user" - accountKey privateKey = "account" - tokenKey privateKey = "token" + userKey privateKey = "user" + tokenKey privateKey = "token" ) type privateKey string @@ -37,11 +33,6 @@ func WithUser(ctx context.Context, user *database.User) context.Context { return context.WithValue(ctx, userKey, user) } -// WithAccount creates a new context with the given account -func WithAccount(ctx context.Context, account *database.Account) context.Context { - return context.WithValue(ctx, accountKey, account) -} - // WithToken creates a new context with the given user func WithToken(ctx context.Context, tok *database.Token) context.Context { return context.WithValue(ctx, tokenKey, tok) @@ -59,17 +50,6 @@ func User(ctx context.Context) *database.User { return nil } -// Account retrieves an account from the given context. -func Account(ctx context.Context) *database.Account { - if temp := ctx.Value(accountKey); temp != nil { - if account, ok := temp.(*database.Account); ok { - return account - } - } - - return nil -} - // Token retrieves a token from the given context. func Token(ctx context.Context) *database.Token { if temp := ctx.Value(tokenKey); temp != nil { diff --git a/pkg/server/controllers/books.go b/pkg/server/controllers/books.go index e5244158..c20ea679 100644 --- a/pkg/server/controllers/books.go +++ b/pkg/server/controllers/books.go @@ -1,24 +1,22 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers import ( + "errors" "fmt" "net/http" @@ -28,7 +26,8 @@ import ( "github.com/dnote/dnote/pkg/server/helpers" "github.com/dnote/dnote/pkg/server/presenters" "github.com/gorilla/mux" - "github.com/pkg/errors" + "gorm.io/gorm" + pkgErrors "github.com/pkg/errors" ) // NewBooks creates a new Books controller. @@ -54,22 +53,11 @@ func (b *Books) getBooks(r *http.Request) ([]database.Book, error) { query := r.URL.Query() name := query.Get("name") - encryptedStr := query.Get("encrypted") if name != "" { part := fmt.Sprintf("%%%s%%", name) conn = conn.Where("LOWER(label) LIKE ?", part) } - if encryptedStr != "" { - var encrypted bool - if encryptedStr == "true" { - encrypted = true - } else { - encrypted = false - } - - conn = conn.Where("encrypted = ?", encrypted) - } var books []database.Book if err := conn.Find(&books).Error; err != nil { @@ -107,13 +95,13 @@ func (b *Books) V3Show(w http.ResponseWriter, r *http.Request) { } var book database.Book - conn := b.app.DB.Where("uuid = ? AND user_id = ?", bookUUID, user.ID).First(&book) + err := b.app.DB.Where("uuid = ? AND user_id = ?", bookUUID, user.ID).First(&book).Error - if conn.RecordNotFound() { + if errors.Is(err, gorm.ErrRecordNotFound) { w.WriteHeader(http.StatusNotFound) return } - if err := conn.Error; err != nil { + if err != nil { handleJSONError(w, err, "finding the book") return } @@ -141,19 +129,19 @@ func (b *Books) create(r *http.Request) (database.Book, error) { var params createBookPayload if err := parseRequestData(r, ¶ms); err != nil { - return database.Book{}, errors.Wrap(err, "parsing request payload") + return database.Book{}, pkgErrors.Wrap(err, "parsing request payload") } if err := validateCreateBookPayload(params); err != nil { - return database.Book{}, errors.Wrap(err, "validating payload") + return database.Book{}, pkgErrors.Wrap(err, "validating payload") } - var bookCount int + var bookCount int64 err := b.app.DB.Model(database.Book{}). Where("user_id = ? AND label = ?", user.ID, params.Name). Count(&bookCount).Error if err != nil { - return database.Book{}, errors.Wrap(err, "checking duplicate") + return database.Book{}, pkgErrors.Wrap(err, "checking duplicate") } if bookCount > 0 { return database.Book{}, app.ErrDuplicateBook @@ -161,7 +149,7 @@ func (b *Books) create(r *http.Request) (database.Book, error) { book, err := b.app.CreateBook(*user, params.Name) if err != nil { - return database.Book{}, errors.Wrap(err, "inserting a book") + return database.Book{}, pkgErrors.Wrap(err, "inserting a book") } return book, nil @@ -212,18 +200,20 @@ func (b *Books) update(r *http.Request) (database.Book, error) { var book database.Book if err := tx.Where("user_id = ? AND uuid = ?", user.ID, uuid).First(&book).Error; err != nil { - return database.Book{}, errors.Wrap(err, "finding book") + tx.Rollback() + return database.Book{}, pkgErrors.Wrap(err, "finding book") } var params updateBookPayload if err := parseRequestData(r, ¶ms); err != nil { - return database.Book{}, errors.Wrap(err, "decoding payload") + tx.Rollback() + return database.Book{}, pkgErrors.Wrap(err, "decoding payload") } book, err := b.app.UpdateBook(tx, *user, book, params.Name) if err != nil { tx.Rollback() - return database.Book{}, errors.Wrap(err, "updating a book") + return database.Book{}, pkgErrors.Wrap(err, "updating a book") } tx.Commit() @@ -262,24 +252,27 @@ func (b *Books) del(r *http.Request) (database.Book, error) { var book database.Book if err := tx.Where("user_id = ? AND uuid = ?", user.ID, uuid).First(&book).Error; err != nil { - return database.Book{}, errors.Wrap(err, "finding a book") + tx.Rollback() + return database.Book{}, pkgErrors.Wrap(err, "finding a book") } var notes []database.Note if err := tx.Where("book_uuid = ? AND NOT deleted", uuid).Order("usn ASC").Find(¬es).Error; err != nil { - return database.Book{}, errors.Wrap(err, "finding notes for the book") + tx.Rollback() + return database.Book{}, pkgErrors.Wrap(err, "finding notes for the book") } for _, note := range notes { if _, err := b.app.DeleteNote(tx, *user, note); err != nil { tx.Rollback() - return database.Book{}, errors.Wrap(err, "deleting a note in the book") + return database.Book{}, pkgErrors.Wrap(err, "deleting a note in the book") } } book, err := b.app.DeleteBook(tx, *user, book) if err != nil { - return database.Book{}, errors.Wrap(err, "deleting the book") + tx.Rollback() + return database.Book{}, pkgErrors.Wrap(err, "deleting the book") } tx.Commit() diff --git a/pkg/server/controllers/books_test.go b/pkg/server/controllers/books_test.go index 89a362da..af6ba42d 100644 --- a/pkg/server/controllers/books_test.go +++ b/pkg/server/controllers/books_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers @@ -21,69 +18,76 @@ package controllers import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "testing" + "time" "github.com/dnote/dnote/pkg/assert" "github.com/dnote/dnote/pkg/clock" "github.com/dnote/dnote/pkg/server/app" - "github.com/dnote/dnote/pkg/server/config" "github.com/dnote/dnote/pkg/server/database" "github.com/dnote/dnote/pkg/server/presenters" "github.com/dnote/dnote/pkg/server/testutils" "github.com/pkg/errors" ) +// truncateMicro rounds time to microsecond precision to match SQLite storage +func truncateMicro(t time.Time) time.Time { + return t.Round(time.Microsecond) +} + func TestGetBooks(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.DB = db + a.Clock = clock.NewMock() + server := MustNewServer(t, &a) defer server.Close() - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@test.com", "pass1234") - anotherUser := testutils.SetupUserData() - testutils.SetupAccountData(anotherUser, "bob@test.com", "pass1234") + user := testutils.SetupUserData(db, "alice@test.com", "pass1234") + anotherUser := testutils.SetupUserData(db, "bob@test.com", "pass1234") b1 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "js", USN: 1123, Deleted: false, } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1") + testutils.MustExec(t, db.Save(&b1), "preparing b1") b2 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "css", USN: 1125, Deleted: false, } - testutils.MustExec(t, testutils.DB.Save(&b2), "preparing b2") + testutils.MustExec(t, db.Save(&b2), "preparing b2") b3 := database.Book{ + UUID: testutils.MustUUID(t), UserID: anotherUser.ID, Label: "css", USN: 1128, Deleted: false, } - testutils.MustExec(t, testutils.DB.Save(&b3), "preparing b3") + testutils.MustExec(t, db.Save(&b3), "preparing b3") b4 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "", USN: 1129, Deleted: true, } - testutils.MustExec(t, testutils.DB.Save(&b4), "preparing b4") + testutils.MustExec(t, db.Save(&b4), "preparing b4") // Execute endpoint := "/api/v3/books" req := testutils.MakeReq(server.URL, "GET", endpoint, "") - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusOK, "") @@ -94,66 +98,73 @@ func TestGetBooks(t *testing.T) { } var b1Record, b2Record database.Book - testutils.MustExec(t, testutils.DB.Where("id = ?", b1.ID).First(&b1Record), "finding b1") - testutils.MustExec(t, testutils.DB.Where("id = ?", b2.ID).First(&b2Record), "finding b2") - testutils.MustExec(t, testutils.DB.Where("id = ?", b2.ID).First(&b2Record), "finding b2") + testutils.MustExec(t, db.Where("id = ?", b1.ID).First(&b1Record), "finding b1") + testutils.MustExec(t, db.Where("id = ?", b2.ID).First(&b2Record), "finding b2") + testutils.MustExec(t, db.Where("id = ?", b2.ID).First(&b2Record), "finding b2") expected := []presenters.Book{ { UUID: b2Record.UUID, - CreatedAt: b2Record.CreatedAt, - UpdatedAt: b2Record.UpdatedAt, + CreatedAt: truncateMicro(b2Record.CreatedAt), + UpdatedAt: truncateMicro(b2Record.UpdatedAt), Label: b2Record.Label, USN: b2Record.USN, }, { UUID: b1Record.UUID, - CreatedAt: b1Record.CreatedAt, - UpdatedAt: b1Record.UpdatedAt, + CreatedAt: truncateMicro(b1Record.CreatedAt), + UpdatedAt: truncateMicro(b1Record.UpdatedAt), Label: b1Record.Label, USN: b1Record.USN, }, } + // Truncate payload timestamps to match SQLite precision + for i := range payload { + payload[i].CreatedAt = truncateMicro(payload[i].CreatedAt) + payload[i].UpdatedAt = truncateMicro(payload[i].UpdatedAt) + } + assert.DeepEqual(t, payload, expected, "payload mismatch") } func TestGetBooksByName(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.DB = db + a.Clock = clock.NewMock() + server := MustNewServer(t, &a) defer server.Close() - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@test.com", "pass1234") - anotherUser := testutils.SetupUserData() - testutils.SetupAccountData(anotherUser, "bob@test.com", "pass1234") + user := testutils.SetupUserData(db, "alice@test.com", "pass1234") + anotherUser := testutils.SetupUserData(db, "bob@test.com", "pass1234") b1 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "js", } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1") + testutils.MustExec(t, db.Save(&b1), "preparing b1") b2 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "css", } - testutils.MustExec(t, testutils.DB.Save(&b2), "preparing b2") + testutils.MustExec(t, db.Save(&b2), "preparing b2") b3 := database.Book{ + UUID: testutils.MustUUID(t), UserID: anotherUser.ID, Label: "js", } - testutils.MustExec(t, testutils.DB.Save(&b3), "preparing b3") + testutils.MustExec(t, db.Save(&b3), "preparing b3") // Execute endpoint := "/api/v3/books?name=js" req := testutils.MakeReq(server.URL, "GET", endpoint, "") - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusOK, "") @@ -164,56 +175,62 @@ func TestGetBooksByName(t *testing.T) { } var b1Record database.Book - testutils.MustExec(t, testutils.DB.Where("id = ?", b1.ID).First(&b1Record), "finding b1") + testutils.MustExec(t, db.Where("id = ?", b1.ID).First(&b1Record), "finding b1") expected := []presenters.Book{ { UUID: b1Record.UUID, - CreatedAt: b1Record.CreatedAt, - UpdatedAt: b1Record.UpdatedAt, + CreatedAt: truncateMicro(b1Record.CreatedAt), + UpdatedAt: truncateMicro(b1Record.UpdatedAt), Label: b1Record.Label, USN: b1Record.USN, }, } + for i := range payload { + payload[i].CreatedAt = truncateMicro(payload[i].CreatedAt) + payload[i].UpdatedAt = truncateMicro(payload[i].UpdatedAt) + } + assert.DeepEqual(t, payload, expected, "payload mismatch") } func TestGetBook(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.DB = db + a.Clock = clock.NewMock() + server := MustNewServer(t, &a) defer server.Close() - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@test.com", "pass1234") - anotherUser := testutils.SetupUserData() - testutils.SetupAccountData(anotherUser, "bob@test.com", "pass1234") + user := testutils.SetupUserData(db, "alice@test.com", "pass1234") + anotherUser := testutils.SetupUserData(db, "bob@test.com", "pass1234") b1 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "js", } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1") + testutils.MustExec(t, db.Save(&b1), "preparing b1") b2 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "css", } - testutils.MustExec(t, testutils.DB.Save(&b2), "preparing b2") + testutils.MustExec(t, db.Save(&b2), "preparing b2") b3 := database.Book{ + UUID: testutils.MustUUID(t), UserID: anotherUser.ID, Label: "js", } - testutils.MustExec(t, testutils.DB.Save(&b3), "preparing b3") + testutils.MustExec(t, db.Save(&b3), "preparing b3") // Execute endpoint := fmt.Sprintf("/api/v3/books/%s", b1.UUID) req := testutils.MakeReq(server.URL, "GET", endpoint, "") - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusOK, "") @@ -224,49 +241,51 @@ func TestGetBook(t *testing.T) { } var b1Record database.Book - testutils.MustExec(t, testutils.DB.Where("id = ?", b1.ID).First(&b1Record), "finding b1") + testutils.MustExec(t, db.Where("id = ?", b1.ID).First(&b1Record), "finding b1") expected := presenters.Book{ UUID: b1Record.UUID, - CreatedAt: b1Record.CreatedAt, - UpdatedAt: b1Record.UpdatedAt, + CreatedAt: truncateMicro(b1Record.CreatedAt), + UpdatedAt: truncateMicro(b1Record.UpdatedAt), Label: b1Record.Label, USN: b1Record.USN, } + payload.CreatedAt = truncateMicro(payload.CreatedAt) + payload.UpdatedAt = truncateMicro(payload.UpdatedAt) + assert.DeepEqual(t, payload, expected, "payload mismatch") } func TestGetBookNonOwner(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.DB = db + a.Clock = clock.NewMock() + server := MustNewServer(t, &a) defer server.Close() - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@test.com", "pass1234") - nonOwner := testutils.SetupUserData() - testutils.SetupAccountData(nonOwner, "bob@test.com", "pass1234") + user := testutils.SetupUserData(db, "alice@test.com", "pass1234") + nonOwner := testutils.SetupUserData(db, "bob@test.com", "pass1234") b1 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "js", } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1") + testutils.MustExec(t, db.Save(&b1), "preparing b1") // Execute endpoint := fmt.Sprintf("/api/v3/books/%s", b1.UUID) req := testutils.MakeReq(server.URL, "GET", endpoint, "") - res := testutils.HTTPAuthDo(t, req, nonOwner) + res := testutils.HTTPAuthDo(t, db, req, nonOwner) // Test assert.StatusCodeEquals(t, res, http.StatusNotFound, "") - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { t.Fatal(errors.Wrap(err, "reading body")) } @@ -275,39 +294,38 @@ func TestGetBookNonOwner(t *testing.T) { func TestCreateBook(t *testing.T) { t.Run("success", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.DB = db + a.Clock = clock.NewMock() + server := MustNewServer(t, &a) defer server.Close() - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@test.com", "pass1234") - testutils.MustExec(t, testutils.DB.Model(&user).Update("max_usn", 101), "preparing user max_usn") + user := testutils.SetupUserData(db, "alice@test.com", "pass1234") + testutils.MustExec(t, db.Model(&user).Update("max_usn", 101), "preparing user max_usn") req := testutils.MakeReq(server.URL, "POST", "/api/v3/books", `{"name": "js"}`) // Execute - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusCreated, "") var bookRecord database.Book var userRecord database.User - var bookCount, noteCount int - testutils.MustExec(t, testutils.DB.Model(&database.Book{}).Count(&bookCount), "counting books") - testutils.MustExec(t, testutils.DB.Model(&database.Note{}).Count(¬eCount), "counting notes") - testutils.MustExec(t, testutils.DB.First(&bookRecord), "finding book") - testutils.MustExec(t, testutils.DB.Where("id = ?", user.ID).First(&userRecord), "finding user record") + var bookCount, noteCount int64 + testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), "counting books") + testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), "counting notes") + testutils.MustExec(t, db.First(&bookRecord), "finding book") + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), "finding user record") maxUSN := 102 - assert.Equalf(t, bookCount, 1, "book count mismatch") - assert.Equalf(t, noteCount, 0, "note count mismatch") + assert.Equalf(t, bookCount, int64(1), "book count mismatch") + assert.Equalf(t, noteCount, int64(0), "note count mismatch") assert.NotEqual(t, bookRecord.UUID, "", "book uuid should have been generated") assert.Equal(t, bookRecord.Label, "js", "book name mismatch") @@ -323,53 +341,56 @@ func TestCreateBook(t *testing.T) { Book: presenters.Book{ UUID: bookRecord.UUID, USN: bookRecord.USN, - CreatedAt: bookRecord.CreatedAt, - UpdatedAt: bookRecord.UpdatedAt, + CreatedAt: truncateMicro(bookRecord.CreatedAt), + UpdatedAt: truncateMicro(bookRecord.UpdatedAt), Label: "js", }, } + got.Book.CreatedAt = truncateMicro(got.Book.CreatedAt) + got.Book.UpdatedAt = truncateMicro(got.Book.UpdatedAt) + assert.DeepEqual(t, got, expected, "payload mismatch") }) t.Run("duplicate", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.DB = db + a.Clock = clock.NewMock() + server := MustNewServer(t, &a) defer server.Close() - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@test.com", "pass1234") - testutils.MustExec(t, testutils.DB.Model(&user).Update("max_usn", 101), "preparing user max_usn") + user := testutils.SetupUserData(db, "alice@test.com", "pass1234") + testutils.MustExec(t, db.Model(&user).Update("max_usn", 101), "preparing user max_usn") b1 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "js", USN: 58, } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing book data") + testutils.MustExec(t, db.Save(&b1), "preparing book data") // Execute req := testutils.MakeReq(server.URL, "POST", "/api/v3/books", `{"name": "js"}`) - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusConflict, "") var bookRecord database.Book - var bookCount, noteCount int + var bookCount, noteCount int64 var userRecord database.User - testutils.MustExec(t, testutils.DB.Model(&database.Book{}).Count(&bookCount), "counting books") - testutils.MustExec(t, testutils.DB.Model(&database.Note{}).Count(¬eCount), "counting notes") - testutils.MustExec(t, testutils.DB.First(&bookRecord), "finding book") - testutils.MustExec(t, testutils.DB.Where("id = ?", user.ID).First(&userRecord), "finding user record") + testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), "counting books") + testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), "counting notes") + testutils.MustExec(t, db.First(&bookRecord), "finding book") + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), "finding user record") - assert.Equalf(t, bookCount, 1, "book count mismatch") - assert.Equalf(t, noteCount, 0, "note count mismatch") + assert.Equalf(t, bookCount, int64(1), "book count mismatch") + assert.Equalf(t, noteCount, int64(0), "note count mismatch") assert.Equal(t, bookRecord.Label, "js", "book name mismatch") assert.Equal(t, bookRecord.UserID, user.ID, "book user_id mismatch") @@ -422,18 +443,17 @@ func TestUpdateBook(t *testing.T) { for idx, tc := range testCases { t.Run(fmt.Sprintf("test case %d", idx), func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.DB = db + a.Clock = clock.NewMock() + server := MustNewServer(t, &a) defer server.Close() - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@test.com", "pass1234") - testutils.MustExec(t, testutils.DB.Model(&user).Update("max_usn", 101), "preparing user max_usn") + user := testutils.SetupUserData(db, "alice@test.com", "pass1234") + testutils.MustExec(t, db.Model(&user).Update("max_usn", 101), "preparing user max_usn") b1 := database.Book{ UUID: tc.bookUUID, @@ -441,32 +461,32 @@ func TestUpdateBook(t *testing.T) { Label: tc.bookLabel, Deleted: tc.bookDeleted, } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1") + testutils.MustExec(t, db.Save(&b1), "preparing b1") b2 := database.Book{ UUID: b2UUID, UserID: user.ID, Label: "js", } - testutils.MustExec(t, testutils.DB.Save(&b2), "preparing b2") + testutils.MustExec(t, db.Save(&b2), "preparing b2") // Execute endpoint := fmt.Sprintf("/api/v3/books/%s", tc.bookUUID) req := testutils.MakeReq(server.URL, "PATCH", endpoint, tc.payload.ToJSON(t)) - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusOK, fmt.Sprintf("status code mismatch for test case %d", idx)) var bookRecord database.Book var userRecord database.User - var noteCount, bookCount int - testutils.MustExec(t, testutils.DB.Model(&database.Book{}).Count(&bookCount), "counting books") - testutils.MustExec(t, testutils.DB.Model(&database.Note{}).Count(¬eCount), "counting notes") - testutils.MustExec(t, testutils.DB.Where("id = ?", b1.ID).First(&bookRecord), "finding book") - testutils.MustExec(t, testutils.DB.Where("id = ?", user.ID).First(&userRecord), "finding user record") + var noteCount, bookCount int64 + testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), "counting books") + testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), "counting notes") + testutils.MustExec(t, db.Where("id = ?", b1.ID).First(&bookRecord), "finding book") + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), "finding user record") - assert.Equalf(t, bookCount, 2, "book count mismatch") - assert.Equalf(t, noteCount, 0, "note count mismatch") + assert.Equalf(t, bookCount, int64(2), "book count mismatch") + assert.Equalf(t, noteCount, int64(0), "note count mismatch") assert.Equalf(t, bookRecord.UUID, tc.bookUUID, "book uuid mismatch") assert.Equalf(t, bookRecord.Label, tc.expectedBookLabel, "book label mismatch") @@ -507,41 +527,42 @@ func TestDeleteBook(t *testing.T) { for _, tc := range testCases { t.Run(fmt.Sprintf("originally deleted %t", tc.deleted), func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.DB = db + a.Clock = clock.NewMock() + server := MustNewServer(t, &a) defer server.Close() - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@test.com", "pass1234") - testutils.MustExec(t, testutils.DB.Model(&user).Update("max_usn", 58), "preparing user max_usn") - anotherUser := testutils.SetupUserData() - testutils.SetupAccountData(anotherUser, "bob@test.com", "pass1234") - testutils.MustExec(t, testutils.DB.Model(&anotherUser).Update("max_usn", 109), "preparing another user max_usn") + user := testutils.SetupUserData(db, "alice@test.com", "pass1234") + testutils.MustExec(t, db.Model(&user).Update("max_usn", 58), "preparing user max_usn") + anotherUser := testutils.SetupUserData(db, "bob@test.com", "pass1234") + testutils.MustExec(t, db.Model(&anotherUser).Update("max_usn", 109), "preparing another user max_usn") b1 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "js", USN: 1, } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing a book data") + testutils.MustExec(t, db.Save(&b1), "preparing a book data") b2 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: tc.label, USN: 2, Deleted: tc.deleted, } - testutils.MustExec(t, testutils.DB.Save(&b2), "preparing a book data") + testutils.MustExec(t, db.Save(&b2), "preparing a book data") b3 := database.Book{ + UUID: testutils.MustUUID(t), UserID: anotherUser.ID, Label: "linux", USN: 3, } - testutils.MustExec(t, testutils.DB.Save(&b3), "preparing a book data") + testutils.MustExec(t, db.Save(&b3), "preparing a book data") var n2Body string if !tc.deleted { @@ -553,49 +574,54 @@ func TestDeleteBook(t *testing.T) { } n1 := database.Note{ + UUID: testutils.MustUUID(t), UserID: user.ID, BookUUID: b1.UUID, Body: "n1 content", USN: 4, } - testutils.MustExec(t, testutils.DB.Save(&n1), "preparing a note data") + testutils.MustExec(t, db.Save(&n1), "preparing a note data") n2 := database.Note{ + UUID: testutils.MustUUID(t), UserID: user.ID, BookUUID: b2.UUID, Body: n2Body, USN: 5, Deleted: tc.deleted, } - testutils.MustExec(t, testutils.DB.Save(&n2), "preparing a note data") + testutils.MustExec(t, db.Save(&n2), "preparing a note data") n3 := database.Note{ + UUID: testutils.MustUUID(t), UserID: user.ID, BookUUID: b2.UUID, Body: n3Body, USN: 6, Deleted: tc.deleted, } - testutils.MustExec(t, testutils.DB.Save(&n3), "preparing a note data") + testutils.MustExec(t, db.Save(&n3), "preparing a note data") n4 := database.Note{ + UUID: testutils.MustUUID(t), UserID: user.ID, BookUUID: b2.UUID, Body: "", USN: 7, Deleted: true, } - testutils.MustExec(t, testutils.DB.Save(&n4), "preparing a note data") + testutils.MustExec(t, db.Save(&n4), "preparing a note data") n5 := database.Note{ + UUID: testutils.MustUUID(t), UserID: anotherUser.ID, BookUUID: b3.UUID, Body: "n5 content", USN: 8, } - testutils.MustExec(t, testutils.DB.Save(&n5), "preparing a note data") + testutils.MustExec(t, db.Save(&n5), "preparing a note data") // Execute endpoint := fmt.Sprintf("/api/v3/books/%s", b2.UUID) req := testutils.MakeReq(server.URL, "DELETE", endpoint, "") - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusOK, "") @@ -603,22 +629,22 @@ func TestDeleteBook(t *testing.T) { var b1Record, b2Record, b3Record database.Book var n1Record, n2Record, n3Record, n4Record, n5Record database.Note var userRecord database.User - var bookCount, noteCount int + var bookCount, noteCount int64 - testutils.MustExec(t, testutils.DB.Model(&database.Book{}).Count(&bookCount), "counting books") - testutils.MustExec(t, testutils.DB.Model(&database.Note{}).Count(¬eCount), "counting notes") - testutils.MustExec(t, testutils.DB.Where("id = ?", b1.ID).First(&b1Record), "finding b1") - testutils.MustExec(t, testutils.DB.Where("id = ?", b2.ID).First(&b2Record), "finding b2") - testutils.MustExec(t, testutils.DB.Where("id = ?", b3.ID).First(&b3Record), "finding b3") - testutils.MustExec(t, testutils.DB.Where("id = ?", n1.ID).First(&n1Record), "finding n1") - testutils.MustExec(t, testutils.DB.Where("id = ?", n2.ID).First(&n2Record), "finding n2") - testutils.MustExec(t, testutils.DB.Where("id = ?", n3.ID).First(&n3Record), "finding n3") - testutils.MustExec(t, testutils.DB.Where("id = ?", n4.ID).First(&n4Record), "finding n4") - testutils.MustExec(t, testutils.DB.Where("id = ?", n5.ID).First(&n5Record), "finding n5") - testutils.MustExec(t, testutils.DB.Where("id = ?", user.ID).First(&userRecord), "finding user record") + testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), "counting books") + testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), "counting notes") + testutils.MustExec(t, db.Where("id = ?", b1.ID).First(&b1Record), "finding b1") + testutils.MustExec(t, db.Where("id = ?", b2.ID).First(&b2Record), "finding b2") + testutils.MustExec(t, db.Where("id = ?", b3.ID).First(&b3Record), "finding b3") + testutils.MustExec(t, db.Where("id = ?", n1.ID).First(&n1Record), "finding n1") + testutils.MustExec(t, db.Where("id = ?", n2.ID).First(&n2Record), "finding n2") + testutils.MustExec(t, db.Where("id = ?", n3.ID).First(&n3Record), "finding n3") + testutils.MustExec(t, db.Where("id = ?", n4.ID).First(&n4Record), "finding n4") + testutils.MustExec(t, db.Where("id = ?", n5.ID).First(&n5Record), "finding n5") + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), "finding user record") - assert.Equal(t, bookCount, 3, "book count mismatch") - assert.Equal(t, noteCount, 5, "note count mismatch") + assert.Equal(t, bookCount, int64(3), "book count mismatch") + assert.Equal(t, noteCount, int64(5), "note count mismatch") assert.Equal(t, userRecord.MaxUSN, tc.expectedMaxUSN, "user max_usn mismatch") diff --git a/pkg/server/controllers/controllers.go b/pkg/server/controllers/controllers.go index 161dfe21..4ab9980d 100644 --- a/pkg/server/controllers/controllers.go +++ b/pkg/server/controllers/controllers.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers diff --git a/pkg/server/controllers/health.go b/pkg/server/controllers/health.go index 8d28c1bf..85d8e048 100644 --- a/pkg/server/controllers/health.go +++ b/pkg/server/controllers/health.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers diff --git a/pkg/server/controllers/health_test.go b/pkg/server/controllers/health_test.go index a3b1d230..56fe9778 100644 --- a/pkg/server/controllers/health_test.go +++ b/pkg/server/controllers/health_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers @@ -24,16 +21,15 @@ import ( "github.com/dnote/dnote/pkg/assert" "github.com/dnote/dnote/pkg/server/app" - "github.com/dnote/dnote/pkg/server/config" "github.com/dnote/dnote/pkg/server/testutils" ) func TestHealth(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) - server := MustNewServer(t, &app.App{ - Config: config.Config{}, - }) + a := app.NewTest() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() // Execute diff --git a/pkg/server/controllers/helpers.go b/pkg/server/controllers/helpers.go index 9a2e6d14..c17e83a1 100644 --- a/pkg/server/controllers/helpers.go +++ b/pkg/server/controllers/helpers.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers @@ -61,13 +58,6 @@ func parseForm(r *http.Request, dst interface{}) error { return parseValues(r.PostForm, dst) } -func parseURLParams(r *http.Request, dst interface{}) error { - if err := r.ParseForm(); err != nil { - return err - } - return parseValues(r.Form, dst) -} - func parseValues(values url.Values, dst interface{}) error { dec := schema.NewDecoder() @@ -239,8 +229,6 @@ func getStatusCode(err error) int { return http.StatusUnauthorized case app.ErrEmailTooLong: return http.StatusBadRequest - case app.ErrEmailAlreadyVerified: - return http.StatusConflict case app.ErrMissingToken: return http.StatusBadRequest case app.ErrExpiredToken: diff --git a/pkg/server/controllers/main_test.go b/pkg/server/controllers/main_test.go index d85c9ed8..cc0383b3 100644 --- a/pkg/server/controllers/main_test.go +++ b/pkg/server/controllers/main_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers @@ -21,15 +18,13 @@ package controllers import ( "os" "testing" - - "github.com/dnote/dnote/pkg/server/testutils" + "time" ) func TestMain(m *testing.M) { - testutils.InitTestDB() + // Set timezone to UTC to match database timestamps + time.Local = time.UTC code := m.Run() - testutils.ClearData(testutils.DB) - os.Exit(code) } diff --git a/pkg/server/controllers/notes.go b/pkg/server/controllers/notes.go index 50f5368a..964a608f 100644 --- a/pkg/server/controllers/notes.go +++ b/pkg/server/controllers/notes.go @@ -1,31 +1,25 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers import ( - "math" "net/http" "net/url" - "sort" "strconv" "strings" - "time" "github.com/dnote/dnote/pkg/server/app" "github.com/dnote/dnote/pkg/server/context" @@ -76,7 +70,6 @@ func parseGetNotesQuery(q url.Values) (app.GetNotesParams, error) { yearStr := q.Get("year") monthStr := q.Get("month") books := q["book"] - encryptedStr := q.Get("encrypted") pageStr := q.Get("page") page, err := parsePageQuery(q) @@ -110,21 +103,13 @@ func parseGetNotesQuery(q url.Values) (app.GetNotesParams, error) { month = m } - var encrypted bool - if strings.ToLower(encryptedStr) == "true" { - encrypted = true - } else { - encrypted = false - } - ret := app.GetNotesParams{ - Year: year, - Month: month, - Page: page, - Search: parseSearchQuery(q), - Books: books, - Encrypted: encrypted, - PerPage: notesPerPage, + Year: year, + Month: month, + Page: page, + Search: parseSearchQuery(q), + Books: books, + PerPage: notesPerPage, } return ret, nil @@ -150,73 +135,10 @@ func (n *Notes) getNotes(r *http.Request) (app.GetNotesResult, app.GetNotesParam return res, p, nil } -type noteGroup struct { - Year int - Month int - Data []database.Note -} - -type bucketKey struct { - year int - month time.Month -} - -func groupNotes(notes []database.Note) []noteGroup { - ret := []noteGroup{} - - buckets := map[bucketKey][]database.Note{} - - for _, note := range notes { - year := note.UpdatedAt.Year() - month := note.UpdatedAt.Month() - key := bucketKey{year, month} - - if _, ok := buckets[key]; !ok { - buckets[key] = []database.Note{} - } - - buckets[key] = append(buckets[key], note) - } - - keys := []bucketKey{} - for key := range buckets { - keys = append(keys, key) - } - - sort.Slice(keys, func(i, j int) bool { - yearI := keys[i].year - yearJ := keys[j].year - monthI := keys[i].month - monthJ := keys[j].month - - if yearI == yearJ { - return monthI < monthJ - } - - return yearI < yearJ - }) - - for _, key := range keys { - group := noteGroup{ - Year: key.year, - Month: int(key.month), - Data: buckets[key], - } - ret = append(ret, group) - } - - return ret -} - -func getMaxPage(page, total int) int { - tmp := float64(total) / float64(notesPerPage) - return int(math.Ceil(tmp)) -} - // GetNotesResponse is a reponse by getNotesHandler type GetNotesResponse struct { Notes []presenters.Note `json:"notes"` - Total int `json:"total"` + Total int64 `json:"total"` } // V3Index is a v3 handler for getting notes @@ -293,11 +215,11 @@ func (n *Notes) create(r *http.Request) (database.Note, error) { var book database.Book if err := n.app.DB.Where("uuid = ? AND user_id = ?", params.BookUUID, user.ID).First(&book).Error; err != nil { - return database.Note{}, errors.Wrap(err, "finding book") + return database.Note{}, errors.Wrapf(err, "finding book %s", params.BookUUID) } client := getClientType(r) - note, err := n.app.CreateNote(*user, params.BookUUID, params.Content, params.AddedOn, params.EditedOn, false, client) + note, err := n.app.CreateNote(*user, params.BookUUID, params.Content, params.AddedOn, params.EditedOn, client) if err != nil { return database.Note{}, errors.Wrap(err, "creating note") } @@ -376,11 +298,10 @@ func (n *Notes) V3Delete(w http.ResponseWriter, r *http.Request) { type updateNotePayload struct { BookUUID *string `schema:"book_uuid" json:"book_uuid"` Content *string `schema:"content" json:"content"` - Public *bool `schema:"public" json:"public"` } func validateUpdateNotePayload(p updateNotePayload) error { - if p.BookUUID == nil && p.Content == nil && p.Public == nil { + if p.BookUUID == nil && p.Content == nil { return app.ErrEmptyUpdate } @@ -416,7 +337,6 @@ func (n *Notes) update(r *http.Request) (database.Note, error) { note, err = n.app.UpdateNote(tx, *user, note, &app.UpdateNoteParams{ BookUUID: params.BookUUID, Content: params.Content, - Public: params.Public, }) if err != nil { tx.Rollback() diff --git a/pkg/server/controllers/notes_test.go b/pkg/server/controllers/notes_test.go index 56227333..afbca59b 100644 --- a/pkg/server/controllers/notes_test.go +++ b/pkg/server/controllers/notes_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers @@ -21,7 +18,7 @@ package controllers import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "testing" "time" @@ -29,7 +26,6 @@ import ( "github.com/dnote/dnote/pkg/assert" "github.com/dnote/dnote/pkg/clock" "github.com/dnote/dnote/pkg/server/app" - "github.com/dnote/dnote/pkg/server/config" "github.com/dnote/dnote/pkg/server/database" "github.com/dnote/dnote/pkg/server/presenters" "github.com/dnote/dnote/pkg/server/testutils" @@ -39,11 +35,10 @@ import ( func getExpectedNotePayload(n database.Note, b database.Book, u database.User) presenters.Note { return presenters.Note{ UUID: n.UUID, - CreatedAt: n.CreatedAt, - UpdatedAt: n.UpdatedAt, + CreatedAt: truncateMicro(n.CreatedAt), + UpdatedAt: truncateMicro(n.UpdatedAt), Body: n.Body, AddedOn: n.AddedOn, - Public: n.Public, USN: n.USN, Book: presenters.NoteBook{ UUID: b.UUID, @@ -56,37 +51,39 @@ func getExpectedNotePayload(n database.Note, b database.Book, u database.User) p } func TestGetNotes(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.DB = db + a.Clock = clock.NewMock() + server := MustNewServer(t, &a) defer server.Close() - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@test.com", "pass1234") - anotherUser := testutils.SetupUserData() - testutils.SetupAccountData(anotherUser, "bob@test.com", "pass1234") + user := testutils.SetupUserData(db, "alice@test.com", "pass1234") + anotherUser := testutils.SetupUserData(db, "bob@test.com", "pass1234") b1 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "js", } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1") + testutils.MustExec(t, db.Save(&b1), "preparing b1") b2 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "css", } - testutils.MustExec(t, testutils.DB.Save(&b2), "preparing b2") + testutils.MustExec(t, db.Save(&b2), "preparing b2") b3 := database.Book{ + UUID: testutils.MustUUID(t), UserID: anotherUser.ID, Label: "css", } - testutils.MustExec(t, testutils.DB.Save(&b3), "preparing b3") + testutils.MustExec(t, db.Save(&b3), "preparing b3") n1 := database.Note{ + UUID: testutils.MustUUID(t), UserID: user.ID, BookUUID: b1.UUID, Body: "n1 content", @@ -94,8 +91,9 @@ func TestGetNotes(t *testing.T) { Deleted: false, AddedOn: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC).UnixNano(), } - testutils.MustExec(t, testutils.DB.Save(&n1), "preparing n1") + testutils.MustExec(t, db.Save(&n1), "preparing n1") n2 := database.Note{ + UUID: testutils.MustUUID(t), UserID: user.ID, BookUUID: b1.UUID, Body: "n2 content", @@ -103,8 +101,9 @@ func TestGetNotes(t *testing.T) { Deleted: false, AddedOn: time.Date(2018, time.August, 11, 22, 0, 0, 0, time.UTC).UnixNano(), } - testutils.MustExec(t, testutils.DB.Save(&n2), "preparing n2") + testutils.MustExec(t, db.Save(&n2), "preparing n2") n3 := database.Note{ + UUID: testutils.MustUUID(t), UserID: user.ID, BookUUID: b1.UUID, Body: "n3 content", @@ -112,8 +111,9 @@ func TestGetNotes(t *testing.T) { Deleted: false, AddedOn: time.Date(2017, time.January, 10, 23, 0, 0, 0, time.UTC).UnixNano(), } - testutils.MustExec(t, testutils.DB.Save(&n3), "preparing n3") + testutils.MustExec(t, db.Save(&n3), "preparing n3") n4 := database.Note{ + UUID: testutils.MustUUID(t), UserID: user.ID, BookUUID: b2.UUID, Body: "n4 content", @@ -121,8 +121,9 @@ func TestGetNotes(t *testing.T) { Deleted: false, AddedOn: time.Date(2018, time.September, 10, 23, 0, 0, 0, time.UTC).UnixNano(), } - testutils.MustExec(t, testutils.DB.Save(&n4), "preparing n4") + testutils.MustExec(t, db.Save(&n4), "preparing n4") n5 := database.Note{ + UUID: testutils.MustUUID(t), UserID: anotherUser.ID, BookUUID: b3.UUID, Body: "n5 content", @@ -130,8 +131,9 @@ func TestGetNotes(t *testing.T) { Deleted: false, AddedOn: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC).UnixNano(), } - testutils.MustExec(t, testutils.DB.Save(&n5), "preparing n5") + testutils.MustExec(t, db.Save(&n5), "preparing n5") n6 := database.Note{ + UUID: testutils.MustUUID(t), UserID: user.ID, BookUUID: b1.UUID, Body: "", @@ -139,13 +141,13 @@ func TestGetNotes(t *testing.T) { Deleted: true, AddedOn: time.Date(2018, time.August, 10, 23, 0, 0, 0, time.UTC).UnixNano(), } - testutils.MustExec(t, testutils.DB.Save(&n6), "preparing n6") + testutils.MustExec(t, db.Save(&n6), "preparing n6") // Execute endpoint := "/api/v3/notes" req := testutils.MakeReq(server.URL, "GET", fmt.Sprintf("%s?year=2018&month=8", endpoint), "") - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusOK, "") @@ -156,8 +158,8 @@ func TestGetNotes(t *testing.T) { } var n2Record, n1Record database.Note - testutils.MustExec(t, testutils.DB.Where("uuid = ?", n2.UUID).First(&n2Record), "finding n2Record") - testutils.MustExec(t, testutils.DB.Where("uuid = ?", n1.UUID).First(&n1Record), "finding n1Record") + testutils.MustExec(t, db.Where("uuid = ?", n2.UUID).First(&n2Record), "finding n2Record") + testutils.MustExec(t, db.Where("uuid = ?", n1.UUID).First(&n1Record), "finding n1Record") expected := GetNotesResponse{ Notes: []presenters.Note{ @@ -171,54 +173,49 @@ func TestGetNotes(t *testing.T) { } func TestGetNote(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.DB = db + a.Clock = clock.NewMock() + server := MustNewServer(t, &a) defer server.Close() - user := testutils.SetupUserData() - anotherUser := testutils.SetupUserData() + user := testutils.SetupUserData(db, "user@test.com", "pass1234") + anotherUser := testutils.SetupUserData(db, "another@test.com", "pass1234") b1 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "js", } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1") + testutils.MustExec(t, db.Save(&b1), "preparing b1") - privateNote := database.Note{ + note := database.Note{ + UUID: testutils.MustUUID(t), UserID: user.ID, BookUUID: b1.UUID, - Body: "privateNote content", - Public: false, + Body: "note content", } - testutils.MustExec(t, testutils.DB.Save(&privateNote), "preparing privateNote") - publicNote := database.Note{ - UserID: user.ID, - BookUUID: b1.UUID, - Body: "publicNote content", - Public: true, - } - testutils.MustExec(t, testutils.DB.Save(&publicNote), "preparing publicNote") + testutils.MustExec(t, db.Save(¬e), "preparing note") deletedNote := database.Note{ + UUID: testutils.MustUUID(t), UserID: user.ID, BookUUID: b1.UUID, Deleted: true, } - testutils.MustExec(t, testutils.DB.Save(&deletedNote), "preparing deletedNote") + testutils.MustExec(t, db.Save(&deletedNote), "preparing deletedNote") getURL := func(noteUUID string) string { return fmt.Sprintf("/api/v3/notes/%s", noteUUID) } - t.Run("owner accessing private note", func(t *testing.T) { + t.Run("owner accessing note", func(t *testing.T) { // Execute - url := getURL(publicNote.UUID) + url := getURL(note.UUID) req := testutils.MakeReq(server.URL, "GET", url, "") - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusOK, "") @@ -228,65 +225,23 @@ func TestGetNote(t *testing.T) { t.Fatal(errors.Wrap(err, "decoding payload")) } - var n2Record database.Note - testutils.MustExec(t, testutils.DB.Where("uuid = ?", publicNote.UUID).First(&n2Record), "finding n2Record") + var noteRecord database.Note + testutils.MustExec(t, db.Where("uuid = ?", note.UUID).First(¬eRecord), "finding noteRecord") - expected := getExpectedNotePayload(n2Record, b1, user) + expected := getExpectedNotePayload(noteRecord, b1, user) assert.DeepEqual(t, payload, expected, "payload mismatch") }) - t.Run("owner accessing public note", func(t *testing.T) { + t.Run("non-owner accessing note", func(t *testing.T) { // Execute - url := getURL(publicNote.UUID) + url := getURL(note.UUID) req := testutils.MakeReq(server.URL, "GET", url, "") - res := testutils.HTTPAuthDo(t, req, user) - - // Test - assert.StatusCodeEquals(t, res, http.StatusOK, "") - - var payload presenters.Note - if err := json.NewDecoder(res.Body).Decode(&payload); err != nil { - t.Fatal(errors.Wrap(err, "decoding payload")) - } - - var n2Record database.Note - testutils.MustExec(t, testutils.DB.Where("uuid = ?", publicNote.UUID).First(&n2Record), "finding n2Record") - - expected := getExpectedNotePayload(n2Record, b1, user) - assert.DeepEqual(t, payload, expected, "payload mismatch") - }) - - t.Run("non-owner accessing public note", func(t *testing.T) { - // Execute - url := getURL(publicNote.UUID) - req := testutils.MakeReq(server.URL, "GET", url, "") - res := testutils.HTTPAuthDo(t, req, anotherUser) - - // Test - assert.StatusCodeEquals(t, res, http.StatusOK, "") - - var payload presenters.Note - if err := json.NewDecoder(res.Body).Decode(&payload); err != nil { - t.Fatal(errors.Wrap(err, "decoding payload")) - } - - var n2Record database.Note - testutils.MustExec(t, testutils.DB.Where("uuid = ?", publicNote.UUID).First(&n2Record), "finding n2Record") - - expected := getExpectedNotePayload(n2Record, b1, user) - assert.DeepEqual(t, payload, expected, "payload mismatch") - }) - - t.Run("non-owner accessing private note", func(t *testing.T) { - // Execute - url := getURL(privateNote.UUID) - req := testutils.MakeReq(server.URL, "GET", url, "") - res := testutils.HTTPAuthDo(t, req, anotherUser) + res := testutils.HTTPAuthDo(t, db, req, anotherUser) // Test assert.StatusCodeEquals(t, res, http.StatusNotFound, "") - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { t.Fatal(errors.Wrap(err, "reading body")) } @@ -294,54 +249,33 @@ func TestGetNote(t *testing.T) { assert.DeepEqual(t, string(body), "not found\n", "payload mismatch") }) - t.Run("guest accessing public note", func(t *testing.T) { + t.Run("guest accessing note", func(t *testing.T) { // Execute - url := getURL(publicNote.UUID) + url := getURL(note.UUID) req := testutils.MakeReq(server.URL, "GET", url, "") res := testutils.HTTPDo(t, req) // Test - assert.StatusCodeEquals(t, res, http.StatusOK, "") + assert.StatusCodeEquals(t, res, http.StatusUnauthorized, "") - var payload presenters.Note - if err := json.NewDecoder(res.Body).Decode(&payload); err != nil { - t.Fatal(errors.Wrap(err, "decoding payload")) - } - - var n2Record database.Note - testutils.MustExec(t, testutils.DB.Where("uuid = ?", publicNote.UUID).First(&n2Record), "finding n2Record") - - expected := getExpectedNotePayload(n2Record, b1, user) - assert.DeepEqual(t, payload, expected, "payload mismatch") - }) - - t.Run("guest accessing private note", func(t *testing.T) { - // Execute - url := getURL(privateNote.UUID) - req := testutils.MakeReq(server.URL, "GET", url, "") - res := testutils.HTTPDo(t, req) - - // Test - assert.StatusCodeEquals(t, res, http.StatusNotFound, "") - - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { t.Fatal(errors.Wrap(err, "reading body")) } - assert.DeepEqual(t, string(body), "not found\n", "payload mismatch") + assert.DeepEqual(t, string(body), "unauthorized\n", "payload mismatch") }) t.Run("nonexistent", func(t *testing.T) { // Execute url := getURL("somerandomstring") req := testutils.MakeReq(server.URL, "GET", url, "") - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusNotFound, "") - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { t.Fatal(errors.Wrap(err, "reading body")) } @@ -353,12 +287,12 @@ func TestGetNote(t *testing.T) { // Execute url := getURL(deletedNote.UUID) req := testutils.MakeReq(server.URL, "GET", url, "") - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusNotFound, "") - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { t.Fatal(errors.Wrap(err, "reading body")) } @@ -368,31 +302,31 @@ func TestGetNote(t *testing.T) { } func TestCreateNote(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.DB = db + a.Clock = clock.NewMock() + server := MustNewServer(t, &a) defer server.Close() - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@test.com", "pass1234") - testutils.MustExec(t, testutils.DB.Model(&user).Update("max_usn", 101), "preparing user max_usn") + user := testutils.SetupUserData(db, "alice@test.com", "pass1234") + testutils.MustExec(t, db.Model(&user).Update("max_usn", 101), "preparing user max_usn") b1 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "js", USN: 58, } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1") + testutils.MustExec(t, db.Save(&b1), "preparing b1") // Execute dat := fmt.Sprintf(`{"book_uuid": "%s", "content": "note content"}`, b1.UUID) req := testutils.MakeReq(server.URL, "POST", "/api/v3/notes", dat) - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusCreated, "") @@ -400,15 +334,15 @@ func TestCreateNote(t *testing.T) { var noteRecord database.Note var bookRecord database.Book var userRecord database.User - var bookCount, noteCount int - testutils.MustExec(t, testutils.DB.Model(&database.Book{}).Count(&bookCount), "counting books") - testutils.MustExec(t, testutils.DB.Model(&database.Note{}).Count(¬eCount), "counting notes") - testutils.MustExec(t, testutils.DB.First(¬eRecord), "finding note") - testutils.MustExec(t, testutils.DB.Where("id = ?", b1.ID).First(&bookRecord), "finding book") - testutils.MustExec(t, testutils.DB.Where("id = ?", user.ID).First(&userRecord), "finding user record") + var bookCount, noteCount int64 + testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), "counting books") + testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), "counting notes") + testutils.MustExec(t, db.First(¬eRecord), "finding note") + testutils.MustExec(t, db.Where("id = ?", b1.ID).First(&bookRecord), "finding book") + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), "finding user record") - assert.Equalf(t, bookCount, 1, "book count mismatch") - assert.Equalf(t, noteCount, 1, "note count mismatch") + assert.Equalf(t, bookCount, int64(1), "book count mismatch") + assert.Equalf(t, noteCount, int64(1), "note count mismatch") assert.Equal(t, bookRecord.Label, b1.Label, "book name mismatch") assert.Equal(t, bookRecord.UUID, b1.UUID, "book uuid mismatch") @@ -449,38 +383,38 @@ func TestDeleteNote(t *testing.T) { for idx, tc := range testCases { t.Run(fmt.Sprintf("test case %d", idx), func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.DB = db + a.Clock = clock.NewMock() + server := MustNewServer(t, &a) defer server.Close() - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@test.com", "pass1234") - testutils.MustExec(t, testutils.DB.Model(&user).Update("max_usn", 981), "preparing user max_usn") + user := testutils.SetupUserData(db, "alice@test.com", "pass1234") + testutils.MustExec(t, db.Model(&user).Update("max_usn", 981), "preparing user max_usn") b1 := database.Book{ UUID: b1UUID, UserID: user.ID, Label: "js", } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1") + testutils.MustExec(t, db.Save(&b1), "preparing b1") note := database.Note{ + UUID: testutils.MustUUID(t), UserID: user.ID, BookUUID: b1.UUID, Body: tc.content, Deleted: tc.deleted, USN: tc.originalUSN, } - testutils.MustExec(t, testutils.DB.Save(¬e), "preparing note") + testutils.MustExec(t, db.Save(¬e), "preparing note") // Execute endpoint := fmt.Sprintf("/api/v3/notes/%s", note.UUID) req := testutils.MakeReq(server.URL, "DELETE", endpoint, "") - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusOK, "") @@ -488,15 +422,15 @@ func TestDeleteNote(t *testing.T) { var bookRecord database.Book var noteRecord database.Note var userRecord database.User - var bookCount, noteCount int - testutils.MustExec(t, testutils.DB.Model(&database.Book{}).Count(&bookCount), "counting books") - testutils.MustExec(t, testutils.DB.Model(&database.Note{}).Count(¬eCount), "counting notes") - testutils.MustExec(t, testutils.DB.Where("uuid = ?", note.UUID).First(¬eRecord), "finding note") - testutils.MustExec(t, testutils.DB.Where("id = ?", b1.ID).First(&bookRecord), "finding book") - testutils.MustExec(t, testutils.DB.Where("id = ?", user.ID).First(&userRecord), "finding user record") + var bookCount, noteCount int64 + testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), "counting books") + testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), "counting notes") + testutils.MustExec(t, db.Where("uuid = ?", note.UUID).First(¬eRecord), "finding note") + testutils.MustExec(t, db.Where("id = ?", b1.ID).First(&bookRecord), "finding book") + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), "finding user record") - assert.Equalf(t, bookCount, 1, "book count mismatch") - assert.Equalf(t, noteCount, 1, "note count mismatch") + assert.Equalf(t, bookCount, int64(1), "book count mismatch") + assert.Equalf(t, noteCount, int64(1), "note count mismatch") assert.Equal(t, noteRecord.UUID, note.UUID, "note uuid mismatch for test case") assert.Equal(t, noteRecord.Body, "", "note content mismatch for test case") @@ -519,7 +453,6 @@ func TestUpdateNote(t *testing.T) { type payloadData struct { Content *string `schema:"content" json:"content,omitempty"` BookUUID *string `schema:"book_uuid" json:"book_uuid,omitempty"` - Public *bool `schema:"public" json:"public,omitempty"` } testCases := []struct { @@ -527,12 +460,10 @@ func TestUpdateNote(t *testing.T) { noteUUID string noteBookUUID string noteBody string - notePublic bool noteDeleted bool expectedNoteBody string expectedNoteBookName string expectedNoteBookUUID string - expectedNotePublic bool }{ { payload: testutils.PayloadWrapper{ @@ -542,13 +473,11 @@ func TestUpdateNote(t *testing.T) { }, noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053", noteBookUUID: b1UUID, - notePublic: false, noteBody: "original content", noteDeleted: false, expectedNoteBookUUID: b1UUID, expectedNoteBody: "some updated content", expectedNoteBookName: "css", - expectedNotePublic: false, }, { payload: testutils.PayloadWrapper{ @@ -558,13 +487,11 @@ func TestUpdateNote(t *testing.T) { }, noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053", noteBookUUID: b1UUID, - notePublic: false, noteBody: "original content", noteDeleted: false, expectedNoteBookUUID: b1UUID, expectedNoteBody: "original content", expectedNoteBookName: "css", - expectedNotePublic: false, }, { payload: testutils.PayloadWrapper{ @@ -574,13 +501,11 @@ func TestUpdateNote(t *testing.T) { }, noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053", noteBookUUID: b1UUID, - notePublic: false, noteBody: "original content", noteDeleted: false, expectedNoteBookUUID: b2UUID, expectedNoteBody: "original content", expectedNoteBookName: "js", - expectedNotePublic: false, }, { payload: testutils.PayloadWrapper{ @@ -591,13 +516,11 @@ func TestUpdateNote(t *testing.T) { }, noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053", noteBookUUID: b1UUID, - notePublic: false, noteBody: "original content", noteDeleted: false, expectedNoteBookUUID: b2UUID, expectedNoteBody: "some updated content", expectedNoteBookName: "js", - expectedNotePublic: false, }, { payload: testutils.PayloadWrapper{ @@ -608,121 +531,50 @@ func TestUpdateNote(t *testing.T) { }, noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053", noteBookUUID: b1UUID, - notePublic: false, noteBody: "", noteDeleted: true, expectedNoteBookUUID: b1UUID, expectedNoteBody: updatedBody, expectedNoteBookName: "js", - expectedNotePublic: false, - }, - { - payload: testutils.PayloadWrapper{ - Data: payloadData{ - Public: &testutils.TrueVal, - }, - }, - noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053", - noteBookUUID: b1UUID, - notePublic: false, - noteBody: "original content", - noteDeleted: false, - expectedNoteBookUUID: b1UUID, - expectedNoteBody: "original content", - expectedNoteBookName: "css", - expectedNotePublic: true, - }, - { - payload: testutils.PayloadWrapper{ - Data: payloadData{ - Public: &testutils.FalseVal, - }, - }, - noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053", - noteBookUUID: b1UUID, - notePublic: true, - noteBody: "original content", - noteDeleted: false, - expectedNoteBookUUID: b1UUID, - expectedNoteBody: "original content", - expectedNoteBookName: "css", - expectedNotePublic: false, - }, - { - payload: testutils.PayloadWrapper{ - Data: payloadData{ - Content: &updatedBody, - Public: &testutils.FalseVal, - }, - }, - noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053", - noteBookUUID: b1UUID, - notePublic: true, - noteBody: "original content", - noteDeleted: false, - expectedNoteBookUUID: b1UUID, - expectedNoteBody: updatedBody, - expectedNoteBookName: "css", - expectedNotePublic: false, - }, - { - payload: testutils.PayloadWrapper{ - Data: payloadData{ - BookUUID: &b2UUID, - Content: &updatedBody, - Public: &testutils.TrueVal, - }, - }, - noteUUID: "ab50aa32-b232-40d8-b10f-10a7f9134053", - noteBookUUID: b1UUID, - notePublic: false, - noteBody: "original content", - noteDeleted: false, - expectedNoteBookUUID: b2UUID, - expectedNoteBody: updatedBody, - expectedNoteBookName: "js", - expectedNotePublic: true, }, } for idx, tc := range testCases { t.Run(fmt.Sprintf("test case %d", idx), func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.DB = db + a.Clock = clock.NewMock() + server := MustNewServer(t, &a) defer server.Close() - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@test.com", "pass1234") + user := testutils.SetupUserData(db, "alice@test.com", "pass1234") - testutils.MustExec(t, testutils.DB.Model(&user).Update("max_usn", 101), "preparing user max_usn") + testutils.MustExec(t, db.Model(&user).Update("max_usn", 101), "preparing user max_usn") b1 := database.Book{ UUID: b1UUID, UserID: user.ID, Label: "css", } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1") + testutils.MustExec(t, db.Save(&b1), "preparing b1") b2 := database.Book{ UUID: b2UUID, UserID: user.ID, Label: "js", } - testutils.MustExec(t, testutils.DB.Save(&b2), "preparing b2") + testutils.MustExec(t, db.Save(&b2), "preparing b2") note := database.Note{ - UserID: user.ID, UUID: tc.noteUUID, + UserID: user.ID, BookUUID: tc.noteBookUUID, Body: tc.noteBody, Deleted: tc.noteDeleted, - Public: tc.notePublic, } - testutils.MustExec(t, testutils.DB.Save(¬e), "preparing note") + testutils.MustExec(t, db.Save(¬e), "preparing note") // Execute var req *http.Request @@ -730,7 +582,7 @@ func TestUpdateNote(t *testing.T) { endpoint := fmt.Sprintf("/api/v3/notes/%s", note.UUID) req = testutils.MakeReq(server.URL, "PATCH", endpoint, tc.payload.ToJSON(t)) - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusOK, "status code mismatch for test case") @@ -738,20 +590,19 @@ func TestUpdateNote(t *testing.T) { var bookRecord database.Book var noteRecord database.Note var userRecord database.User - var noteCount, bookCount int - testutils.MustExec(t, testutils.DB.Model(&database.Book{}).Count(&bookCount), "counting books") - testutils.MustExec(t, testutils.DB.Model(&database.Note{}).Count(¬eCount), "counting notes") - testutils.MustExec(t, testutils.DB.Where("uuid = ?", note.UUID).First(¬eRecord), "finding note") - testutils.MustExec(t, testutils.DB.Where("id = ?", b1.ID).First(&bookRecord), "finding book") - testutils.MustExec(t, testutils.DB.Where("id = ?", user.ID).First(&userRecord), "finding user record") + var noteCount, bookCount int64 + testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), "counting books") + testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), "counting notes") + testutils.MustExec(t, db.Where("uuid = ?", note.UUID).First(¬eRecord), "finding note") + testutils.MustExec(t, db.Where("id = ?", b1.ID).First(&bookRecord), "finding book") + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), "finding user record") - assert.Equalf(t, bookCount, 2, "book count mismatch") - assert.Equalf(t, noteCount, 1, "note count mismatch") + assert.Equalf(t, bookCount, int64(2), "book count mismatch") + assert.Equalf(t, noteCount, int64(1), "note count mismatch") assert.Equal(t, noteRecord.UUID, tc.noteUUID, "note uuid mismatch for test case") assert.Equal(t, noteRecord.Body, tc.expectedNoteBody, "note content mismatch for test case") assert.Equal(t, noteRecord.BookUUID, tc.expectedNoteBookUUID, "note book_uuid mismatch for test case") - assert.Equal(t, noteRecord.Public, tc.expectedNotePublic, "note public mismatch for test case") assert.Equal(t, noteRecord.USN, 102, "note usn mismatch for test case") assert.Equal(t, userRecord.MaxUSN, 102, "user max_usn mismatch for test case") diff --git a/pkg/server/controllers/routes.go b/pkg/server/controllers/routes.go index af40cb02..3552d081 100644 --- a/pkg/server/controllers/routes.go +++ b/pkg/server/controllers/routes.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers @@ -48,25 +45,23 @@ func NewWebRoutes(a *app.App, c *Controllers) []Route { redirectGuest := &mw.AuthParams{RedirectGuestsToLogin: true} ret := []Route{ - {"GET", "/", mw.Auth(a, c.Users.Settings, redirectGuest), true}, - {"GET", "/about", mw.Auth(a, c.Users.About, redirectGuest), true}, - {"GET", "/login", mw.GuestOnly(a, c.Users.NewLogin), true}, - {"POST", "/login", mw.GuestOnly(a, c.Users.Login), true}, + {"GET", "/", mw.Auth(a.DB, c.Users.Settings, redirectGuest), true}, + {"GET", "/about", mw.Auth(a.DB, c.Users.About, redirectGuest), true}, + {"GET", "/login", mw.GuestOnly(a.DB, c.Users.NewLogin), true}, + {"POST", "/login", mw.GuestOnly(a.DB, c.Users.Login), true}, {"POST", "/logout", c.Users.Logout, true}, {"GET", "/password-reset", c.Users.PasswordResetView.ServeHTTP, true}, {"PATCH", "/password-reset", c.Users.PasswordReset, true}, {"GET", "/password-reset/{token}", c.Users.PasswordResetConfirm, true}, {"POST", "/reset-token", c.Users.CreateResetToken, true}, - {"POST", "/verification-token", mw.Auth(a, c.Users.CreateEmailVerificationToken, redirectGuest), true}, - {"GET", "/verify-email/{token}", mw.Auth(a, c.Users.VerifyEmail, redirectGuest), true}, - {"PATCH", "/account/profile", mw.Auth(a, c.Users.ProfileUpdate, nil), true}, - {"PATCH", "/account/password", mw.Auth(a, c.Users.PasswordUpdate, nil), true}, + {"PATCH", "/account/profile", mw.Auth(a.DB, c.Users.ProfileUpdate, nil), true}, + {"PATCH", "/account/password", mw.Auth(a.DB, c.Users.PasswordUpdate, nil), true}, {"GET", "/health", c.Health.Index, true}, } - if !a.Config.DisableRegistration { + if !a.DisableRegistration { ret = append(ret, Route{"GET", "/join", c.Users.New, true}) ret = append(ret, Route{"POST", "/join", c.Users.Create, true}) } @@ -76,28 +71,25 @@ func NewWebRoutes(a *app.App, c *Controllers) []Route { // NewAPIRoutes returns a new api routes func NewAPIRoutes(a *app.App, c *Controllers) []Route { - - proOnly := mw.AuthParams{ProOnly: true} - return []Route{ // v3 - {"GET", "/v3/sync/fragment", mw.Cors(mw.Auth(a, c.Sync.GetSyncFragment, &proOnly)), false}, - {"GET", "/v3/sync/state", mw.Cors(mw.Auth(a, c.Sync.GetSyncState, &proOnly)), false}, - {"POST", "/v3/signin", mw.Cors(c.Users.V3Login), true}, - {"POST", "/v3/signout", mw.Cors(c.Users.V3Logout), true}, - {"OPTIONS", "/v3/signout", mw.Cors(c.Users.logoutOptions), true}, - {"GET", "/v3/notes", mw.Cors(mw.Auth(a, c.Notes.V3Index, nil)), true}, - {"GET", "/v3/notes/{noteUUID}", c.Notes.V3Show, true}, - {"POST", "/v3/notes", mw.Cors(mw.Auth(a, c.Notes.V3Create, nil)), true}, - {"DELETE", "/v3/notes/{noteUUID}", mw.Cors(mw.Auth(a, c.Notes.V3Delete, nil)), true}, - {"PATCH", "/v3/notes/{noteUUID}", mw.Cors(mw.Auth(a, c.Notes.V3Update, nil)), true}, - {"OPTIONS", "/v3/notes", mw.Cors(c.Notes.IndexOptions), true}, - {"GET", "/v3/books", mw.Cors(mw.Auth(a, c.Books.V3Index, nil)), true}, - {"GET", "/v3/books/{bookUUID}", mw.Cors(mw.Auth(a, c.Books.V3Show, nil)), true}, - {"POST", "/v3/books", mw.Cors(mw.Auth(a, c.Books.V3Create, nil)), true}, - {"PATCH", "/v3/books/{bookUUID}", mw.Cors(mw.Auth(a, c.Books.V3Update, nil)), true}, - {"DELETE", "/v3/books/{bookUUID}", mw.Cors(mw.Auth(a, c.Books.V3Delete, nil)), true}, - {"OPTIONS", "/v3/books", mw.Cors(c.Books.IndexOptions), true}, + {"GET", "/v3/sync/fragment", mw.Auth(a.DB, c.Sync.GetSyncFragment, nil), false}, + {"GET", "/v3/sync/state", mw.Auth(a.DB, c.Sync.GetSyncState, nil), false}, + {"POST", "/v3/signin", c.Users.V3Login, true}, + {"POST", "/v3/signout", c.Users.V3Logout, true}, + {"OPTIONS", "/v3/signout", c.Users.logoutOptions, true}, + {"GET", "/v3/notes", mw.Auth(a.DB, c.Notes.V3Index, nil), true}, + {"GET", "/v3/notes/{noteUUID}", mw.Auth(a.DB, c.Notes.V3Show, nil), true}, + {"POST", "/v3/notes", mw.Auth(a.DB, c.Notes.V3Create, nil), true}, + {"DELETE", "/v3/notes/{noteUUID}", mw.Auth(a.DB, c.Notes.V3Delete, nil), true}, + {"PATCH", "/v3/notes/{noteUUID}", mw.Auth(a.DB, c.Notes.V3Update, nil), true}, + {"OPTIONS", "/v3/notes", c.Notes.IndexOptions, true}, + {"GET", "/v3/books", mw.Auth(a.DB, c.Books.V3Index, nil), true}, + {"GET", "/v3/books/{bookUUID}", mw.Auth(a.DB, c.Books.V3Show, nil), true}, + {"POST", "/v3/books", mw.Auth(a.DB, c.Books.V3Create, nil), true}, + {"PATCH", "/v3/books/{bookUUID}", mw.Auth(a.DB, c.Books.V3Update, nil), true}, + {"DELETE", "/v3/books/{bookUUID}", mw.Auth(a.DB, c.Books.V3Delete, nil), true}, + {"OPTIONS", "/v3/books", c.Books.IndexOptions, true}, } } diff --git a/pkg/server/controllers/routes_test.go b/pkg/server/controllers/routes_test.go index 1146d1c9..0d146fe6 100644 --- a/pkg/server/controllers/routes_test.go +++ b/pkg/server/controllers/routes_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers @@ -25,7 +22,6 @@ import ( "github.com/dnote/dnote/pkg/assert" "github.com/dnote/dnote/pkg/clock" "github.com/dnote/dnote/pkg/server/app" - "github.com/dnote/dnote/pkg/server/config" "github.com/dnote/dnote/pkg/server/testutils" ) @@ -56,10 +52,11 @@ func TestNotSupportedVersions(t *testing.T) { } // setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + db := testutils.InitMemoryDB(t) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() for _, tc := range testCases { diff --git a/pkg/server/controllers/static.go b/pkg/server/controllers/static.go index f24fa6ee..42872438 100644 --- a/pkg/server/controllers/static.go +++ b/pkg/server/controllers/static.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers diff --git a/pkg/server/controllers/sync.go b/pkg/server/controllers/sync.go index d9ea3f2e..7cda4316 100644 --- a/pkg/server/controllers/sync.go +++ b/pkg/server/controllers/sync.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers @@ -75,7 +72,6 @@ type SyncFragNote struct { AddedOn int64 `json:"added_on"` EditedOn int64 `json:"edited_on"` Body string `json:"content"` - Public bool `json:"public"` Deleted bool `json:"deleted"` } @@ -89,7 +85,6 @@ func NewFragNote(note database.Note) SyncFragNote { AddedOn: note.AddedOn, EditedOn: note.EditedOn, Body: note.Body, - Public: note.Public, Deleted: note.Deleted, BookUUID: note.BookUUID, } @@ -303,7 +298,7 @@ func (s *Sync) GetSyncState(w http.ResponseWriter, r *http.Request) { } response := GetSyncStateResp{ - FullSyncBefore: fullSyncBefore, + FullSyncBefore: int(user.FullSyncBefore), MaxUSN: user.MaxUSN, // TODO: exposing server time means we probably shouldn't seed random generator with time? CurrentTime: s.app.Clock.Now().Unix(), diff --git a/pkg/server/controllers/sync_test.go b/pkg/server/controllers/sync_test.go index 0ff45a12..e97ecb50 100644 --- a/pkg/server/controllers/sync_test.go +++ b/pkg/server/controllers/sync_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers diff --git a/pkg/server/controllers/testutils.go b/pkg/server/controllers/testutils.go index 7a931208..5da11a41 100644 --- a/pkg/server/controllers/testutils.go +++ b/pkg/server/controllers/testutils.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers @@ -27,9 +24,9 @@ import ( ) // MustNewServer is a test utility function to initialize a new server -// with the given app paratmers -func MustNewServer(t *testing.T, appParams *app.App) *httptest.Server { - server, err := NewServer(appParams) +// with the given app +func MustNewServer(t *testing.T, a *app.App) *httptest.Server { + server, err := NewServer(a) if err != nil { t.Fatal(errors.Wrap(err, "initializing router")) } @@ -37,16 +34,14 @@ func MustNewServer(t *testing.T, appParams *app.App) *httptest.Server { return server } -func NewServer(appParams *app.App) (*httptest.Server, error) { - a := app.NewTest(appParams) - - ctl := New(&a) +func NewServer(a *app.App) (*httptest.Server, error) { + ctl := New(a) rc := RouteConfig{ - WebRoutes: NewWebRoutes(&a, ctl), - APIRoutes: NewAPIRoutes(&a, ctl), + WebRoutes: NewWebRoutes(a, ctl), + APIRoutes: NewAPIRoutes(a, ctl), Controllers: ctl, } - r, err := NewRouter(&a, rc) + r, err := NewRouter(a, rc) if err != nil { return nil, errors.Wrap(err, "initializing router") } diff --git a/pkg/server/controllers/users.go b/pkg/server/controllers/users.go index 372c26fe..26881668 100644 --- a/pkg/server/controllers/users.go +++ b/pkg/server/controllers/users.go @@ -1,24 +1,22 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers import ( + "errors" "net/http" "net/url" "time" @@ -29,11 +27,11 @@ import ( "github.com/dnote/dnote/pkg/server/database" "github.com/dnote/dnote/pkg/server/helpers" "github.com/dnote/dnote/pkg/server/log" - "github.com/dnote/dnote/pkg/server/mailer" "github.com/dnote/dnote/pkg/server/token" "github.com/dnote/dnote/pkg/server/views" "github.com/gorilla/mux" - "github.com/pkg/errors" + "gorm.io/gorm" + pkgErrors "github.com/pkg/errors" "golang.org/x/crypto/bcrypt" ) @@ -78,10 +76,6 @@ func NewUsers(app *app.App, viewEngine *views.Engine) *Users { views.Config{Title: "About", Layout: "base", HelperFuncs: commonHelpers, HeaderTemplate: "navbar"}, "users/settings_about", ), - EmailVerificationView: viewEngine.NewView(app, - views.Config{Layout: "base", HelperFuncs: commonHelpers, HeaderTemplate: "navbar"}, - "users/email_verification", - ), app: app, } } @@ -94,7 +88,6 @@ type Users struct { AboutView *views.View PasswordResetView *views.View PasswordResetConfirmView *views.View - EmailVerificationView *views.View app *app.App } @@ -247,7 +240,7 @@ func (u *Users) V3Login(w http.ResponseWriter, r *http.Request) { func (u *Users) logout(r *http.Request) (bool, error) { key, err := GetCredential(r) if err != nil { - return false, errors.Wrap(err, "getting credentials") + return false, pkgErrors.Wrap(err, "getting credentials") } if key == "" { @@ -255,7 +248,7 @@ func (u *Users) logout(r *http.Request) (bool, error) { } if err = u.app.DeleteSession(key); err != nil { - return false, errors.Wrap(err, "deleting session") + return false, pkgErrors.Wrap(err, "deleting session") } return true, nil @@ -311,23 +304,23 @@ func (u *Users) CreateResetToken(w http.ResponseWriter, r *http.Request) { return } - var account database.Account - conn := u.app.DB.Where("email = ?", form.Email).First(&account) - if conn.RecordNotFound() { + var user database.User + err := u.app.DB.Where("email = ?", form.Email).First(&user).Error + if errors.Is(err, gorm.ErrRecordNotFound) { return } - if err := conn.Error; err != nil { - handleHTMLError(w, r, err, "finding account", u.PasswordResetView, vd) + if err != nil { + handleHTMLError(w, r, err, "finding user", u.PasswordResetView, vd) return } - resetToken, err := token.Create(u.app.DB, account.UserID, database.TokenTypeResetPassword) + resetToken, err := token.Create(u.app.DB, user.ID, database.TokenTypeResetPassword) if err != nil { handleHTMLError(w, r, err, "generating token", u.PasswordResetView, vd) return } - if err := u.app.SendPasswordResetEmail(account.Email.String, resetToken.Value); err != nil { + if err := u.app.SendPasswordResetEmail(user.Email.String, resetToken.Value); err != nil { handleHTMLError(w, r, err, "sending password reset email", u.PasswordResetView, vd) return } @@ -379,12 +372,12 @@ func (u *Users) PasswordReset(w http.ResponseWriter, r *http.Request) { } var token database.Token - conn := u.app.DB.Where("value = ? AND type =? AND used_at IS NULL", params.Token, database.TokenTypeResetPassword).First(&token) - if conn.RecordNotFound() { + err := u.app.DB.Where("value = ? AND type =? AND used_at IS NULL", params.Token, database.TokenTypeResetPassword).First(&token).Error + if errors.Is(err, gorm.ErrRecordNotFound) { handleHTMLError(w, r, app.ErrInvalidToken, "invalid token", u.PasswordResetConfirmView, vd) return } - if err := conn.Error; err != nil { + if err != nil { handleHTMLError(w, r, err, "finding token", u.PasswordResetConfirmView, vd) return } @@ -400,34 +393,28 @@ func (u *Users) PasswordReset(w http.ResponseWriter, r *http.Request) { return } - tx := u.app.DB.Begin() - - hashedPassword, err := bcrypt.GenerateFromPassword([]byte(params.Password), bcrypt.DefaultCost) - if err != nil { - tx.Rollback() - handleHTMLError(w, r, err, "hashing password", u.PasswordResetConfirmView, vd) - return - } - - var account database.Account - if err := u.app.DB.Where("user_id = ?", token.UserID).First(&account).Error; err != nil { - tx.Rollback() + var user database.User + if err := u.app.DB.Where("id = ?", token.UserID).First(&user).Error; err != nil { handleHTMLError(w, r, err, "finding user", u.PasswordResetConfirmView, vd) return } - if err := tx.Model(&account).Update("password", string(hashedPassword)).Error; err != nil { + tx := u.app.DB.Begin() + + // Update the password + if err := app.UpdateUserPassword(tx, &user, params.Password); err != nil { tx.Rollback() handleHTMLError(w, r, err, "updating password", u.PasswordResetConfirmView, vd) return } + if err := tx.Model(&token).Update("used_at", time.Now()).Error; err != nil { tx.Rollback() handleHTMLError(w, r, err, "updating password reset token", u.PasswordResetConfirmView, vd) return } - if err := u.app.DeleteUserSessions(tx, account.UserID); err != nil { + if err := u.app.DeleteUserSessions(tx, user.ID); err != nil { tx.Rollback() handleHTMLError(w, r, err, "deleting user sessions", u.PasswordResetConfirmView, vd) return @@ -435,19 +422,13 @@ func (u *Users) PasswordReset(w http.ResponseWriter, r *http.Request) { tx.Commit() - var user database.User - if err := u.app.DB.Where("id = ?", account.UserID).First(&user).Error; err != nil { - handleHTMLError(w, r, err, "finding user", u.PasswordResetConfirmView, vd) - return - } - alert := views.Alert{ Level: views.AlertLvlSuccess, Message: "Password reset successful", } views.RedirectAlert(w, r, "/login", http.StatusFound, alert) - if err := u.app.SendPasswordResetAlertEmail(account.Email.String); err != nil { + if err := u.app.SendPasswordResetAlertEmail(user.Email.String); err != nil { log.ErrorWrap(err, "sending password reset email") } } @@ -503,14 +484,8 @@ func (u *Users) PasswordUpdate(w http.ResponseWriter, r *http.Request) { return } - var account database.Account - if err := u.app.DB.Where("user_id = ?", user.ID).First(&account).Error; err != nil { - handleHTMLError(w, r, err, "getting account", u.SettingView, vd) - return - } - password := []byte(form.OldPassword) - if err := bcrypt.CompareHashAndPassword([]byte(account.Password.String), password); err != nil { + if err := bcrypt.CompareHashAndPassword([]byte(user.Password.String), password); err != nil { log.WithFields(log.Fields{ "user_id": user.ID, }).Warn("invalid password update attempt") @@ -518,18 +493,7 @@ func (u *Users) PasswordUpdate(w http.ResponseWriter, r *http.Request) { return } - if err := validatePassword(form.NewPassword); err != nil { - handleHTMLError(w, r, err, "invalid password", u.SettingView, vd) - return - } - - hashedNewPassword, err := bcrypt.GenerateFromPassword([]byte(form.NewPassword), bcrypt.DefaultCost) - if err != nil { - handleHTMLError(w, r, err, "hashing password", u.SettingView, vd) - return - } - - if err := u.app.DB.Model(&account).Update("password", string(hashedNewPassword)).Error; err != nil { + if err := app.UpdateUserPassword(u.app.DB, user, form.NewPassword); err != nil { handleHTMLError(w, r, err, "updating password", u.SettingView, vd) return } @@ -541,14 +505,6 @@ func (u *Users) PasswordUpdate(w http.ResponseWriter, r *http.Request) { views.RedirectAlert(w, r, "/", http.StatusFound, alert) } -func validatePassword(password string) error { - if len(password) < 8 { - return app.ErrPasswordTooShort - } - - return nil -} - type updateProfileForm struct { Email string `schema:"email"` Password string `schema:"password"` @@ -563,12 +519,6 @@ func (u *Users) ProfileUpdate(w http.ResponseWriter, r *http.Request) { return } - var account database.Account - if err := u.app.DB.Where("user_id = ?", user.ID).First(&account).Error; err != nil { - handleHTMLError(w, r, err, "getting account", u.SettingView, vd) - return - } - var form updateProfileForm if err := parseRequestData(r, &form); err != nil { handleHTMLError(w, r, err, "parsing payload", u.SettingView, vd) @@ -576,7 +526,7 @@ func (u *Users) ProfileUpdate(w http.ResponseWriter, r *http.Request) { } password := []byte(form.Password) - if err := bcrypt.CompareHashAndPassword([]byte(account.Password.String), password); err != nil { + if err := bcrypt.CompareHashAndPassword([]byte(user.Password.String), password); err != nil { log.WithFields(log.Fields{ "user_id": user.ID, }).Warn("invalid email update attempt") @@ -590,27 +540,13 @@ func (u *Users) ProfileUpdate(w http.ResponseWriter, r *http.Request) { return } - tx := u.app.DB.Begin() - if err := tx.Save(&user).Error; err != nil { - tx.Rollback() + user.Email.String = form.Email + + if err := u.app.DB.Save(&user).Error; err != nil { handleHTMLError(w, r, err, "saving user", u.SettingView, vd) return } - // check if email was changed - if form.Email != account.Email.String { - account.EmailVerified = false - } - account.Email.String = form.Email - - if err := tx.Save(&account).Error; err != nil { - tx.Rollback() - handleHTMLError(w, r, err, "saving account", u.SettingView, vd) - return - } - - tx.Commit() - alert := views.Alert{ Level: views.AlertLvlSuccess, Message: "Email change successful", @@ -618,119 +554,3 @@ func (u *Users) ProfileUpdate(w http.ResponseWriter, r *http.Request) { views.RedirectAlert(w, r, "/", http.StatusFound, alert) } -func (u *Users) VerifyEmail(w http.ResponseWriter, r *http.Request) { - vd := views.Data{} - - vars := mux.Vars(r) - tokenValue := vars["token"] - - if tokenValue == "" { - handleHTMLError(w, r, app.ErrMissingToken, "Missing email verification token", u.EmailVerificationView, vd) - return - } - - var token database.Token - if err := u.app.DB. - Where("value = ? AND type = ?", tokenValue, database.TokenTypeEmailVerification). - First(&token).Error; err != nil { - handleHTMLError(w, r, app.ErrInvalidToken, "Finding token", u.EmailVerificationView, vd) - return - } - - if token.UsedAt != nil { - handleHTMLError(w, r, app.ErrInvalidToken, "Token has already been used.", u.EmailVerificationView, vd) - return - } - - // Expire after ttl - if time.Since(token.CreatedAt).Minutes() > 30 { - handleHTMLError(w, r, app.ErrExpiredToken, "Token has expired.", u.EmailVerificationView, vd) - return - } - - var account database.Account - if err := u.app.DB.Where("user_id = ?", token.UserID).First(&account).Error; err != nil { - handleHTMLError(w, r, err, "finding account", u.EmailVerificationView, vd) - return - } - if account.EmailVerified { - handleHTMLError(w, r, app.ErrEmailAlreadyVerified, "Already verified", u.EmailVerificationView, vd) - return - } - - tx := u.app.DB.Begin() - account.EmailVerified = true - if err := tx.Save(&account).Error; err != nil { - tx.Rollback() - handleHTMLError(w, r, err, "updating email_verified", u.EmailVerificationView, vd) - return - } - if err := tx.Model(&token).Update("used_at", time.Now()).Error; err != nil { - tx.Rollback() - handleHTMLError(w, r, err, "updating reset token", u.EmailVerificationView, vd) - return - } - tx.Commit() - - var user database.User - if err := u.app.DB.Where("id = ?", token.UserID).First(&user).Error; err != nil { - handleHTMLError(w, r, err, "finding user", u.EmailVerificationView, vd) - return - } - - session, err := u.app.SignIn(&user) - if err != nil { - handleHTMLError(w, r, err, "Creating session", u.EmailVerificationView, vd) - } - - setSessionCookie(w, session.Key, session.ExpiresAt) - http.Redirect(w, r, "/", http.StatusFound) -} - -func (u *Users) CreateEmailVerificationToken(w http.ResponseWriter, r *http.Request) { - vd := views.Data{} - - user := context.User(r.Context()) - if user == nil { - handleHTMLError(w, r, app.ErrLoginRequired, "No authenticated user found", u.SettingView, vd) - return - } - - var account database.Account - err := u.app.DB.Where("user_id = ?", user.ID).First(&account).Error - if err != nil { - handleHTMLError(w, r, err, "finding account", u.SettingView, vd) - return - } - - if account.EmailVerified { - handleHTMLError(w, r, app.ErrEmailAlreadyVerified, "email is already verified.", u.SettingView, vd) - return - } - if account.Email.String == "" { - handleHTMLError(w, r, app.ErrEmailRequired, "email is empty.", u.SettingView, vd) - return - } - - tok, err := token.Create(u.app.DB, account.UserID, database.TokenTypeEmailVerification) - if err != nil { - handleHTMLError(w, r, err, "saving token", u.SettingView, vd) - return - } - - if err := u.app.SendVerificationEmail(account.Email.String, tok.Value); err != nil { - if errors.Cause(err) == mailer.ErrSMTPNotConfigured { - handleHTMLError(w, r, app.ErrInvalidSMTPConfig, "SMTP config is not configured correctly.", u.SettingView, vd) - } else { - handleHTMLError(w, r, err, "sending verification email", u.SettingView, vd) - } - - return - } - - alert := views.Alert{ - Level: views.AlertLvlSuccess, - Message: "Please check your email for the verification", - } - views.RedirectAlert(w, r, "/", http.StatusFound, alert) -} diff --git a/pkg/server/controllers/users_test.go b/pkg/server/controllers/users_test.go index 7e014c99..f5ddf5b5 100644 --- a/pkg/server/controllers/users_test.go +++ b/pkg/server/controllers/users_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package controllers @@ -30,18 +27,18 @@ import ( "github.com/dnote/dnote/pkg/assert" "github.com/dnote/dnote/pkg/clock" "github.com/dnote/dnote/pkg/server/app" - "github.com/dnote/dnote/pkg/server/config" "github.com/dnote/dnote/pkg/server/database" "github.com/dnote/dnote/pkg/server/testutils" "github.com/pkg/errors" "golang.org/x/crypto/bcrypt" + "gorm.io/gorm" ) -func assertResponseSessionCookie(t *testing.T, res *http.Response) { - var sessionCount int +func assertResponseSessionCookie(t *testing.T, db *gorm.DB, res *http.Response) { + var sessionCount int64 var session database.Session - testutils.MustExec(t, testutils.DB.Model(&database.Session{}).Count(&sessionCount), "counting session") - testutils.MustExec(t, testutils.DB.First(&session), "getting session") + testutils.MustExec(t, db.Model(&database.Session{}).Count(&sessionCount), "counting session") + testutils.MustExec(t, db.First(&session), "getting session") c := testutils.GetCookieByName(res.Cookies(), "id") assert.Equal(t, c.Value, session.Key, "session key mismatch") @@ -55,53 +52,35 @@ func TestJoin(t *testing.T) { email string password string passwordConfirmation string - onPremise bool - expectedPro bool }{ { email: "alice@example.com", password: "pass1234", passwordConfirmation: "pass1234", - onPremise: false, - expectedPro: false, }, { email: "bob@example.com", password: "Y9EwmjH@Jq6y5a64MSACUoM4w7SAhzvY", passwordConfirmation: "Y9EwmjH@Jq6y5a64MSACUoM4w7SAhzvY", - onPremise: false, - expectedPro: false, }, { email: "chuck@example.com", password: "e*H@kJi^vXbWEcD9T5^Am!Y@7#Po2@PC", passwordConfirmation: "e*H@kJi^vXbWEcD9T5^Am!Y@7#Po2@PC", - onPremise: false, - expectedPro: false, - }, - // on premise - { - email: "dan@example.com", - password: "e*H@kJi^vXbWEcD9T5^Am!Y@7#Po2@PC", - passwordConfirmation: "e*H@kJi^vXbWEcD9T5^Am!Y@7#Po2@PC", - onPremise: true, - expectedPro: true, }, } for _, tc := range testCases { t.Run(fmt.Sprintf("register %s %s", tc.email, tc.password), func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup emailBackend := testutils.MockEmailbackendImplementation{} - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - EmailBackend: &emailBackend, - Config: config.Config{ - OnPremise: tc.onPremise, - }, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.EmailBackend = &emailBackend + a.DB = db + server := MustNewServer(t, &a) defer server.Close() dat := url.Values{} @@ -116,16 +95,14 @@ func TestJoin(t *testing.T) { // Test assert.StatusCodeEquals(t, res, http.StatusFound, "") - var account database.Account - testutils.MustExec(t, testutils.DB.Where("email = ?", tc.email).First(&account), "finding account") - assert.Equal(t, account.Email.String, tc.email, "Email mismatch") - assert.NotEqual(t, account.UserID, 0, "UserID mismatch") - passwordErr := bcrypt.CompareHashAndPassword([]byte(account.Password.String), []byte(tc.password)) + var user database.User + testutils.MustExec(t, db.Where("email = ?", tc.email).First(&user), "finding account") + assert.Equal(t, user.Email.String, tc.email, "Email mismatch") + assert.NotEqual(t, user.ID, 0, "UserID mismatch") + passwordErr := bcrypt.CompareHashAndPassword([]byte(user.Password.String), []byte(tc.password)) assert.Equal(t, passwordErr, nil, "Password mismatch") - var user database.User - testutils.MustExec(t, testutils.DB.Where("id = ?", account.UserID).First(&user), "finding user") - assert.Equal(t, user.Cloud, tc.expectedPro, "Cloud mismatch") + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&user), "finding user") assert.Equal(t, user.MaxUSN, 0, "MaxUSN mismatch") // welcome email @@ -133,20 +110,20 @@ func TestJoin(t *testing.T) { assert.DeepEqual(t, emailBackend.Emails[0].To, []string{tc.email}, "email to mismatch") // after register, should sign in user - assertResponseSessionCookie(t, res) + assertResponseSessionCookie(t, db, res) }) } } func TestJoinError(t *testing.T) { t.Run("missing email", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() dat := url.Values{} @@ -159,22 +136,20 @@ func TestJoinError(t *testing.T) { // Test assert.StatusCodeEquals(t, res, http.StatusBadRequest, "Status mismatch") - var accountCount, userCount int - testutils.MustExec(t, testutils.DB.Model(&database.Account{}).Count(&accountCount), "counting account") - testutils.MustExec(t, testutils.DB.Model(&database.User{}).Count(&userCount), "counting user") + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting user") - assert.Equal(t, accountCount, 0, "accountCount mismatch") - assert.Equal(t, userCount, 0, "userCount mismatch") + assert.Equal(t, userCount, int64(0), "userCount mismatch") }) t.Run("missing password", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() dat := url.Values{} @@ -187,22 +162,20 @@ func TestJoinError(t *testing.T) { // Test assert.StatusCodeEquals(t, res, http.StatusBadRequest, "Status mismatch") - var accountCount, userCount int - testutils.MustExec(t, testutils.DB.Model(&database.Account{}).Count(&accountCount), "counting account") - testutils.MustExec(t, testutils.DB.Model(&database.User{}).Count(&userCount), "counting user") + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting user") - assert.Equal(t, accountCount, 0, "accountCount mismatch") - assert.Equal(t, userCount, 0, "userCount mismatch") + assert.Equal(t, userCount, int64(0), "userCount mismatch") }) t.Run("password confirmation mismatch", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() dat := url.Values{} @@ -217,27 +190,24 @@ func TestJoinError(t *testing.T) { // Test assert.StatusCodeEquals(t, res, http.StatusBadRequest, "Status mismatch") - var accountCount, userCount int - testutils.MustExec(t, testutils.DB.Model(&database.Account{}).Count(&accountCount), "counting account") - testutils.MustExec(t, testutils.DB.Model(&database.User{}).Count(&userCount), "counting user") + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting user") - assert.Equal(t, accountCount, 0, "accountCount mismatch") - assert.Equal(t, userCount, 0, "userCount mismatch") + assert.Equal(t, userCount, int64(0), "userCount mismatch") }) } func TestJoinDuplicateEmail(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() - u := testutils.SetupUserData() - testutils.SetupAccountData(u, "alice@example.com", "somepassword") + u := testutils.SetupUserData(db, "alice@example.com", "somepassword") dat := url.Values{} dat.Set("email", "alice@example.com") @@ -251,30 +221,27 @@ func TestJoinDuplicateEmail(t *testing.T) { // Test assert.StatusCodeEquals(t, res, http.StatusBadRequest, "status code mismatch") - var accountCount, userCount, verificationTokenCount int - testutils.MustExec(t, testutils.DB.Model(&database.Account{}).Count(&accountCount), "counting account") - testutils.MustExec(t, testutils.DB.Model(&database.User{}).Count(&userCount), "counting user") - testutils.MustExec(t, testutils.DB.Model(&database.Token{}).Count(&verificationTokenCount), "counting verification token") + var userCount, verificationTokenCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting user") + testutils.MustExec(t, db.Model(&database.Token{}).Count(&verificationTokenCount), "counting verification token") var user database.User - testutils.MustExec(t, testutils.DB.Where("id = ?", u.ID).First(&user), "finding user") + testutils.MustExec(t, db.Where("id = ?", u.ID).First(&user), "finding user") - assert.Equal(t, accountCount, 1, "account count mismatch") - assert.Equal(t, userCount, 1, "user count mismatch") - assert.Equal(t, verificationTokenCount, 0, "verification_token should not have been created") + assert.Equal(t, userCount, int64(1), "user count mismatch") + assert.Equal(t, verificationTokenCount, int64(0), "verification_token should not have been created") assert.Equal(t, user.LastLoginAt, (*time.Time)(nil), "LastLoginAt mismatch") } func TestJoinDisabled(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{ - DisableRegistration: true, - }, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + a.DisableRegistration = true + server := MustNewServer(t, &a) defer server.Close() dat := url.Values{} @@ -288,26 +255,23 @@ func TestJoinDisabled(t *testing.T) { // Test assert.StatusCodeEquals(t, res, http.StatusNotFound, "status code mismatch") - var accountCount, userCount int - testutils.MustExec(t, testutils.DB.Model(&database.Account{}).Count(&accountCount), "counting account") - testutils.MustExec(t, testutils.DB.Model(&database.User{}).Count(&userCount), "counting user") + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting user") - assert.Equal(t, accountCount, 0, "account count mismatch") - assert.Equal(t, userCount, 0, "user count mismatch") + assert.Equal(t, userCount, int64(0), "user count mismatch") } func TestLogin(t *testing.T) { testutils.RunForWebAndAPI(t, "success", func(t *testing.T, target testutils.EndpointType) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) - u := testutils.SetupUserData() - testutils.SetupAccountData(u, "alice@example.com", "pass1234") + _ = testutils.SetupUserData(db, "alice@example.com", "pass1234") defer server.Close() // Execute @@ -332,11 +296,11 @@ func TestLogin(t *testing.T) { } var user database.User - testutils.MustExec(t, testutils.DB.Model(&database.User{}).First(&user), "finding user") + testutils.MustExec(t, db.Model(&database.User{}).First(&user), "finding user") assert.NotEqual(t, user.LastLoginAt, nil, "LastLoginAt mismatch") if target == testutils.EndpointWeb { - assertResponseSessionCookie(t, res) + assertResponseSessionCookie(t, db, res) } else { // after register, should sign in user var got SessionResponse @@ -344,30 +308,29 @@ func TestLogin(t *testing.T) { t.Fatal(errors.Wrap(err, "decoding payload")) } - var sessionCount int + var sessionCount int64 var session database.Session - testutils.MustExec(t, testutils.DB.Model(&database.Session{}).Count(&sessionCount), "counting session") - testutils.MustExec(t, testutils.DB.First(&session), "getting session") + testutils.MustExec(t, db.Model(&database.Session{}).Count(&sessionCount), "counting session") + testutils.MustExec(t, db.First(&session), "getting session") - assert.Equal(t, sessionCount, 1, "sessionCount mismatch") + assert.Equal(t, sessionCount, int64(1), "sessionCount mismatch") assert.Equal(t, got.Key, session.Key, "session Key mismatch") assert.Equal(t, got.ExpiresAt, session.ExpiresAt.Unix(), "session ExpiresAt mismatch") - assertResponseSessionCookie(t, res) + assertResponseSessionCookie(t, db, res) } }) testutils.RunForWebAndAPI(t, "wrong password", func(t *testing.T, target testutils.EndpointType) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) - u := testutils.SetupUserData() - testutils.SetupAccountData(u, "alice@example.com", "pass1234") + _ = testutils.SetupUserData(db, "alice@example.com", "pass1234") defer server.Close() var req *http.Request @@ -388,26 +351,25 @@ func TestLogin(t *testing.T) { assert.StatusCodeEquals(t, res, http.StatusUnauthorized, "") var user database.User - testutils.MustExec(t, testutils.DB.Model(&database.User{}).First(&user), "finding user") + testutils.MustExec(t, db.Model(&database.User{}).First(&user), "finding user") assert.Equal(t, user.LastLoginAt, (*time.Time)(nil), "LastLoginAt mismatch") - var sessionCount int - testutils.MustExec(t, testutils.DB.Model(&database.Session{}).Count(&sessionCount), "counting session") - assert.Equal(t, sessionCount, 0, "sessionCount mismatch") + var sessionCount int64 + testutils.MustExec(t, db.Model(&database.Session{}).Count(&sessionCount), "counting session") + assert.Equal(t, sessionCount, int64(0), "sessionCount mismatch") }) testutils.RunForWebAndAPI(t, "wrong email", func(t *testing.T, target testutils.EndpointType) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() - u := testutils.SetupUserData() - testutils.SetupAccountData(u, "alice@example.com", "pass1234") + _ = testutils.SetupUserData(db, "alice@example.com", "pass1234") var req *http.Request if target == testutils.EndpointWeb { @@ -427,22 +389,22 @@ func TestLogin(t *testing.T) { assert.StatusCodeEquals(t, res, http.StatusUnauthorized, "") var user database.User - testutils.MustExec(t, testutils.DB.Model(&database.User{}).First(&user), "finding user") + testutils.MustExec(t, db.Model(&database.User{}).First(&user), "finding user") assert.DeepEqual(t, user.LastLoginAt, (*time.Time)(nil), "LastLoginAt mismatch") - var sessionCount int - testutils.MustExec(t, testutils.DB.Model(&database.Session{}).Count(&sessionCount), "counting session") - assert.Equal(t, sessionCount, 0, "sessionCount mismatch") + var sessionCount int64 + testutils.MustExec(t, db.Model(&database.Session{}).Count(&sessionCount), "counting session") + assert.Equal(t, sessionCount, int64(0), "sessionCount mismatch") }) testutils.RunForWebAndAPI(t, "nonexistent email", func(t *testing.T, target testutils.EndpointType) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() var req *http.Request @@ -462,23 +424,22 @@ func TestLogin(t *testing.T) { // Test assert.StatusCodeEquals(t, res, http.StatusUnauthorized, "") - var sessionCount int - testutils.MustExec(t, testutils.DB.Model(&database.Session{}).Count(&sessionCount), "counting session") - assert.Equal(t, sessionCount, 0, "sessionCount mismatch") + var sessionCount int64 + testutils.MustExec(t, db.Model(&database.Session{}).Count(&sessionCount), "counting session") + assert.Equal(t, sessionCount, int64(0), "sessionCount mismatch") }) } func TestLogout(t *testing.T) { - setupLogoutTest := func(t *testing.T) (*httptest.Server, *database.Session, *database.Session) { + setupLogoutTest := func(t *testing.T, db *gorm.DB) (*httptest.Server, *database.Session, *database.Session) { // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) - aliceUser := testutils.SetupUserData() - testutils.SetupAccountData(aliceUser, "alice@example.com", "pass1234") - anotherUser := testutils.SetupUserData() + aliceUser := testutils.SetupUserData(db, "alice@example.com", "pass1234") + anotherUser := testutils.SetupUserData(db, "bob@example.com", "password123") session1ExpiresAt := time.Now().Add(time.Hour * 24) session1 := database.Session{ @@ -486,21 +447,21 @@ func TestLogout(t *testing.T) { UserID: aliceUser.ID, ExpiresAt: session1ExpiresAt, } - testutils.MustExec(t, testutils.DB.Save(&session1), "preparing session1") + testutils.MustExec(t, db.Save(&session1), "preparing session1") session2 := database.Session{ Key: "MDCpbvCRg7W2sH6S870wqLqZDZTObYeVd0PzOekfo/A=", UserID: anotherUser.ID, ExpiresAt: time.Now().Add(time.Hour * 24), } - testutils.MustExec(t, testutils.DB.Save(&session2), "preparing session2") + testutils.MustExec(t, db.Save(&session2), "preparing session2") return server, &session1, &session2 } testutils.RunForWebAndAPI(t, "authenticated", func(t *testing.T, target testutils.EndpointType) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) - server, session1, _ := setupLogoutTest(t) + server, session1, _ := setupLogoutTest(t, db) defer server.Close() // Execute @@ -523,12 +484,12 @@ func TestLogout(t *testing.T) { assert.StatusCodeEquals(t, res, http.StatusNoContent, "Status mismatch") } - var sessionCount int + var sessionCount int64 var s2 database.Session - testutils.MustExec(t, testutils.DB.Model(&database.Session{}).Count(&sessionCount), "counting session") - testutils.MustExec(t, testutils.DB.Where("key = ?", "MDCpbvCRg7W2sH6S870wqLqZDZTObYeVd0PzOekfo/A=").First(&s2), "getting s2") + testutils.MustExec(t, db.Model(&database.Session{}).Count(&sessionCount), "counting session") + testutils.MustExec(t, db.Where("key = ?", "MDCpbvCRg7W2sH6S870wqLqZDZTObYeVd0PzOekfo/A=").First(&s2), "getting s2") - assert.Equal(t, sessionCount, 1, "sessionCount mismatch") + assert.Equal(t, sessionCount, int64(1), "sessionCount mismatch") if target == testutils.EndpointWeb { c := testutils.GetCookieByName(res.Cookies(), "id") @@ -542,9 +503,9 @@ func TestLogout(t *testing.T) { }) testutils.RunForWebAndAPI(t, "unauthenticated", func(t *testing.T, target testutils.EndpointType) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) - server, _, _ := setupLogoutTest(t) + server, _, _ := setupLogoutTest(t, db) defer server.Close() // Execute @@ -565,14 +526,14 @@ func TestLogout(t *testing.T) { assert.StatusCodeEquals(t, res, http.StatusNoContent, "Status mismatch") } - var sessionCount int + var sessionCount int64 var postSession1, postSession2 database.Session - testutils.MustExec(t, testutils.DB.Model(&database.Session{}).Count(&sessionCount), "counting session") - testutils.MustExec(t, testutils.DB.Where("key = ?", "A9xgggqzTHETy++GDi1NpDNe0iyqosPm9bitdeNGkJU=").First(&postSession1), "getting postSession1") - testutils.MustExec(t, testutils.DB.Where("key = ?", "MDCpbvCRg7W2sH6S870wqLqZDZTObYeVd0PzOekfo/A=").First(&postSession2), "getting postSession2") + testutils.MustExec(t, db.Model(&database.Session{}).Count(&sessionCount), "counting session") + testutils.MustExec(t, db.Where("key = ?", "A9xgggqzTHETy++GDi1NpDNe0iyqosPm9bitdeNGkJU=").First(&postSession1), "getting postSession1") + testutils.MustExec(t, db.Where("key = ?", "MDCpbvCRg7W2sH6S870wqLqZDZTObYeVd0PzOekfo/A=").First(&postSession2), "getting postSession2") // two existing sessions should remain - assert.Equal(t, sessionCount, 2, "sessionCount mismatch") + assert.Equal(t, sessionCount, int64(2), "sessionCount mismatch") c := testutils.GetCookieByName(res.Cookies(), "id") assert.Equal(t, c, (*http.Cookie)(nil), "id cookie should have not been set") @@ -581,46 +542,39 @@ func TestLogout(t *testing.T) { func TestResetPassword(t *testing.T) { t.Run("success", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() - u := testutils.SetupUserData() - a := testutils.SetupAccountData(u, "alice@example.com", "oldpassword") + u := testutils.SetupUserData(db, "alice@example.com", "oldpassword") tok := database.Token{ UserID: u.ID, Value: "MivFxYiSMMA4An9dP24DNQ==", Type: database.TokenTypeResetPassword, } - testutils.MustExec(t, testutils.DB.Save(&tok), "preparing token") - otherTok := database.Token{ - UserID: u.ID, - Value: "somerandomvalue", - Type: database.TokenTypeEmailVerification, - } - testutils.MustExec(t, testutils.DB.Save(&otherTok), "preparing another token") + testutils.MustExec(t, db.Save(&tok), "preparing token") s1 := database.Session{ Key: "some-session-key-1", UserID: u.ID, ExpiresAt: time.Now().Add(time.Hour * 10 * 24), } - testutils.MustExec(t, testutils.DB.Save(&s1), "preparing user session 1") + testutils.MustExec(t, db.Save(&s1), "preparing user session 1") s2 := &database.Session{ Key: "some-session-key-2", UserID: u.ID, ExpiresAt: time.Now().Add(time.Hour * 10 * 24), } - testutils.MustExec(t, testutils.DB.Save(&s2), "preparing user session 2") + testutils.MustExec(t, db.Save(&s2), "preparing user session 2") - anotherUser := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Save(&database.Session{ + anotherUser := testutils.SetupUserData(db, "bob@example.com", "password123") + testutils.MustExec(t, db.Save(&database.Session{ Key: "some-session-key-3", UserID: anotherUser.ID, ExpiresAt: time.Now().Add(time.Hour * 10 * 24), @@ -638,50 +592,47 @@ func TestResetPassword(t *testing.T) { // Test assert.StatusCodeEquals(t, res, http.StatusFound, "Status code mismatch") - var resetToken, verificationToken database.Token - var account database.Account - testutils.MustExec(t, testutils.DB.Where("value = ?", "MivFxYiSMMA4An9dP24DNQ==").First(&resetToken), "finding reset token") - testutils.MustExec(t, testutils.DB.Where("value = ?", "somerandomvalue").First(&verificationToken), "finding reset token") - testutils.MustExec(t, testutils.DB.Where("id = ?", a.ID).First(&account), "finding account") + var resetToken database.Token + var user database.User + testutils.MustExec(t, db.Where("value = ?", "MivFxYiSMMA4An9dP24DNQ==").First(&resetToken), "finding reset token") + testutils.MustExec(t, db.Where("id = ?", u.ID).First(&user), "finding account") assert.NotEqual(t, resetToken.UsedAt, nil, "reset_token UsedAt mismatch") - passwordErr := bcrypt.CompareHashAndPassword([]byte(account.Password.String), []byte("newpassword")) + passwordErr := bcrypt.CompareHashAndPassword([]byte(user.Password.String), []byte("newpassword")) assert.Equal(t, passwordErr, nil, "Password mismatch") - assert.Equal(t, verificationToken.UsedAt, (*time.Time)(nil), "verificationToken UsedAt mismatch") - var s1Count, s2Count int - testutils.MustExec(t, testutils.DB.Model(&database.Session{}).Where("id = ?", s1.ID).Count(&s1Count), "counting s1") - testutils.MustExec(t, testutils.DB.Model(&database.Session{}).Where("id = ?", s2.ID).Count(&s2Count), "counting s2") + var s1Count, s2Count int64 + testutils.MustExec(t, db.Model(&database.Session{}).Where("id = ?", s1.ID).Count(&s1Count), "counting s1") + testutils.MustExec(t, db.Model(&database.Session{}).Where("id = ?", s2.ID).Count(&s2Count), "counting s2") - assert.Equal(t, s1Count, 0, "s1 should have been deleted") - assert.Equal(t, s2Count, 0, "s2 should have been deleted") + assert.Equal(t, s1Count, int64(0), "s1 should have been deleted") + assert.Equal(t, s2Count, int64(0), "s2 should have been deleted") - var userSessionCount, anotherUserSessionCount int - testutils.MustExec(t, testutils.DB.Model(&database.Session{}).Where("user_id = ?", u.ID).Count(&userSessionCount), "counting user session") - testutils.MustExec(t, testutils.DB.Model(&database.Session{}).Where("user_id = ?", anotherUser.ID).Count(&anotherUserSessionCount), "counting anotherUser session") + var userSessionCount, anotherUserSessionCount int64 + testutils.MustExec(t, db.Model(&database.Session{}).Where("user_id = ?", u.ID).Count(&userSessionCount), "counting user session") + testutils.MustExec(t, db.Model(&database.Session{}).Where("user_id = ?", anotherUser.ID).Count(&anotherUserSessionCount), "counting anotherUser session") - assert.Equal(t, userSessionCount, 0, "should have deleted a user session") - assert.Equal(t, anotherUserSessionCount, 1, "anotherUser session count mismatch") + assert.Equal(t, userSessionCount, int64(0), "should have deleted a user session") + assert.Equal(t, anotherUserSessionCount, int64(1), "anotherUser session count mismatch") }) t.Run("nonexistent token", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() - u := testutils.SetupUserData() - a := testutils.SetupAccountData(u, "alice@example.com", "somepassword") + u := testutils.SetupUserData(db, "alice@example.com", "somepassword") tok := database.Token{ UserID: u.ID, Value: "MivFxYiSMMA4An9dP24DNQ==", Type: database.TokenTypeResetPassword, } - testutils.MustExec(t, testutils.DB.Save(&tok), "preparing token") + testutils.MustExec(t, db.Save(&tok), "preparing token") dat := url.Values{} dat.Set("token", "-ApMnyvpg59uOU5b-Kf5uQ==") @@ -696,34 +647,33 @@ func TestResetPassword(t *testing.T) { assert.StatusCodeEquals(t, res, http.StatusBadRequest, "Status code mismatch") var resetToken database.Token - var account database.Account - testutils.MustExec(t, testutils.DB.Where("value = ?", "MivFxYiSMMA4An9dP24DNQ==").First(&resetToken), "finding reset token") - testutils.MustExec(t, testutils.DB.Where("id = ?", a.ID).First(&account), "finding account") + var user database.User + testutils.MustExec(t, db.Where("value = ?", "MivFxYiSMMA4An9dP24DNQ==").First(&resetToken), "finding reset token") + testutils.MustExec(t, db.Where("id = ?", u.ID).First(&user), "finding account") - assert.Equal(t, a.Password, account.Password, "password should not have been updated") - assert.Equal(t, a.Password, account.Password, "password should not have been updated") + assert.Equal(t, u.Password, user.Password, "password should not have been updated") + assert.Equal(t, u.Password, user.Password, "password should not have been updated") assert.Equal(t, resetToken.UsedAt, (*time.Time)(nil), "used_at should be nil") }) t.Run("expired token", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() - u := testutils.SetupUserData() - a := testutils.SetupAccountData(u, "alice@example.com", "somepassword") + u := testutils.SetupUserData(db, "alice@example.com", "somepassword") tok := database.Token{ UserID: u.ID, Value: "MivFxYiSMMA4An9dP24DNQ==", Type: database.TokenTypeResetPassword, } - testutils.MustExec(t, testutils.DB.Save(&tok), "preparing token") - testutils.MustExec(t, testutils.DB.Model(&tok).Update("created_at", time.Now().Add(time.Minute*-11)), "Failed to prepare reset_token created_at") + testutils.MustExec(t, db.Save(&tok), "preparing token") + testutils.MustExec(t, db.Model(&tok).Update("created_at", time.Now().Add(time.Minute*-11)), "Failed to prepare reset_token created_at") dat := url.Values{} dat.Set("token", "MivFxYiSMMA4An9dP24DNQ==") @@ -738,25 +688,24 @@ func TestResetPassword(t *testing.T) { assert.StatusCodeEquals(t, res, http.StatusGone, "Status code mismatch") var resetToken database.Token - var account database.Account - testutils.MustExec(t, testutils.DB.Where("value = ?", "MivFxYiSMMA4An9dP24DNQ==").First(&resetToken), "failed to find reset_token") - testutils.MustExec(t, testutils.DB.Where("id = ?", a.ID).First(&account), "failed to find account") - assert.Equal(t, a.Password, account.Password, "password should not have been updated") + var user database.User + testutils.MustExec(t, db.Where("value = ?", "MivFxYiSMMA4An9dP24DNQ==").First(&resetToken), "failed to find reset_token") + testutils.MustExec(t, db.Where("id = ?", u.ID).First(&user), "failed to find account") + assert.Equal(t, u.Password, user.Password, "password should not have been updated") assert.Equal(t, resetToken.UsedAt, (*time.Time)(nil), "used_at should be nil") }) t.Run("used token", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() - u := testutils.SetupUserData() - a := testutils.SetupAccountData(u, "alice@example.com", "somepassword") + u := testutils.SetupUserData(db, "alice@example.com", "somepassword") usedAt := time.Now().Add(time.Hour * -11).UTC() tok := database.Token{ @@ -765,8 +714,8 @@ func TestResetPassword(t *testing.T) { Type: database.TokenTypeResetPassword, UsedAt: &usedAt, } - testutils.MustExec(t, testutils.DB.Save(&tok), "preparing token") - testutils.MustExec(t, testutils.DB.Model(&tok).Update("created_at", time.Now().Add(time.Minute*-11)), "Failed to prepare reset_token created_at") + testutils.MustExec(t, db.Save(&tok), "preparing token") + testutils.MustExec(t, db.Model(&tok).Update("created_at", time.Now().Add(time.Minute*-11)), "Failed to prepare reset_token created_at") dat := url.Values{} dat.Set("token", "MivFxYiSMMA4An9dP24DNQ==") @@ -781,76 +730,36 @@ func TestResetPassword(t *testing.T) { assert.StatusCodeEquals(t, res, http.StatusBadRequest, "Status code mismatch") var resetToken database.Token - var account database.Account - testutils.MustExec(t, testutils.DB.Where("value = ?", "MivFxYiSMMA4An9dP24DNQ==").First(&resetToken), "failed to find reset_token") - testutils.MustExec(t, testutils.DB.Where("id = ?", a.ID).First(&account), "failed to find account") - assert.Equal(t, a.Password, account.Password, "password should not have been updated") + var user database.User + testutils.MustExec(t, db.Where("value = ?", "MivFxYiSMMA4An9dP24DNQ==").First(&resetToken), "failed to find reset_token") + testutils.MustExec(t, db.Where("id = ?", u.ID).First(&user), "failed to find account") + assert.Equal(t, u.Password, user.Password, "password should not have been updated") - if resetToken.UsedAt.Year() != usedAt.Year() || - resetToken.UsedAt.Month() != usedAt.Month() || - resetToken.UsedAt.Day() != usedAt.Day() || - resetToken.UsedAt.Hour() != usedAt.Hour() || - resetToken.UsedAt.Minute() != usedAt.Minute() || - resetToken.UsedAt.Second() != usedAt.Second() { + resetTokenUsedAtUTC := resetToken.UsedAt.UTC() + if resetTokenUsedAtUTC.Year() != usedAt.Year() || + resetTokenUsedAtUTC.Month() != usedAt.Month() || + resetTokenUsedAtUTC.Day() != usedAt.Day() || + resetTokenUsedAtUTC.Hour() != usedAt.Hour() || + resetTokenUsedAtUTC.Minute() != usedAt.Minute() || + resetTokenUsedAtUTC.Second() != usedAt.Second() { t.Errorf("used_at should be %+v but got: %+v", usedAt, resetToken.UsedAt) } }) - t.Run("using wrong type token: email_verification", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) - - // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) - defer server.Close() - - u := testutils.SetupUserData() - a := testutils.SetupAccountData(u, "alice@example.com", "somepassword") - tok := database.Token{ - UserID: u.ID, - Value: "MivFxYiSMMA4An9dP24DNQ==", - Type: database.TokenTypeEmailVerification, - } - testutils.MustExec(t, testutils.DB.Save(&tok), "Failed to prepare reset_token") - testutils.MustExec(t, testutils.DB.Model(&tok).Update("created_at", time.Now().Add(time.Minute*-11)), "Failed to prepare reset_token created_at") - - dat := url.Values{} - dat.Set("token", "MivFxYiSMMA4An9dP24DNQ==") - dat.Set("password", "oldpassword") - dat.Set("password_confirmation", "oldpassword") - req := testutils.MakeFormReq(server.URL, "PATCH", "/password-reset", dat) - - // Execute - res := testutils.HTTPDo(t, req) - - // Test - assert.StatusCodeEquals(t, res, http.StatusBadRequest, "Status code mismatch") - - var resetToken database.Token - var account database.Account - testutils.MustExec(t, testutils.DB.Where("value = ?", "MivFxYiSMMA4An9dP24DNQ==").First(&resetToken), "failed to find reset_token") - testutils.MustExec(t, testutils.DB.Where("id = ?", a.ID).First(&account), "failed to find account") - - assert.Equal(t, a.Password, account.Password, "password should not have been updated") - assert.Equal(t, resetToken.UsedAt, (*time.Time)(nil), "used_at should be nil") - }) } func TestCreateResetToken(t *testing.T) { t.Run("success", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() - u := testutils.SetupUserData() - testutils.SetupAccountData(u, "alice@example.com", "somepassword") + u := testutils.SetupUserData(db, "alice@example.com", "somepassword") // Execute dat := url.Values{} @@ -862,29 +771,28 @@ func TestCreateResetToken(t *testing.T) { // Test assert.StatusCodeEquals(t, res, http.StatusFound, "Status code mismtach") - var tokenCount int - testutils.MustExec(t, testutils.DB.Model(&database.Token{}).Count(&tokenCount), "counting tokens") + var tokenCount int64 + testutils.MustExec(t, db.Model(&database.Token{}).Count(&tokenCount), "counting tokens") var resetToken database.Token - testutils.MustExec(t, testutils.DB.Where("user_id = ? AND type = ?", u.ID, database.TokenTypeResetPassword).First(&resetToken), "finding reset token") + testutils.MustExec(t, db.Where("user_id = ? AND type = ?", u.ID, database.TokenTypeResetPassword).First(&resetToken), "finding reset token") - assert.Equal(t, tokenCount, 1, "reset_token count mismatch") + assert.Equal(t, tokenCount, int64(1), "reset_token count mismatch") assert.NotEqual(t, resetToken.Value, nil, "reset_token value mismatch") assert.Equal(t, resetToken.UsedAt, (*time.Time)(nil), "reset_token UsedAt mismatch") }) t.Run("nonexistent email", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() - u := testutils.SetupUserData() - testutils.SetupAccountData(u, "alice@example.com", "somepassword") + _ = testutils.SetupUserData(db, "alice@example.com", "somepassword") // Execute dat := url.Values{} @@ -896,25 +804,24 @@ func TestCreateResetToken(t *testing.T) { // Test assert.StatusCodeEquals(t, res, http.StatusOK, "Status code mismtach") - var tokenCount int - testutils.MustExec(t, testutils.DB.Model(&database.Token{}).Count(&tokenCount), "counting tokens") - assert.Equal(t, tokenCount, 0, "reset_token count mismatch") + var tokenCount int64 + testutils.MustExec(t, db.Model(&database.Token{}).Count(&tokenCount), "counting tokens") + assert.Equal(t, tokenCount, int64(0), "reset_token count mismatch") }) } func TestUpdatePassword(t *testing.T) { t.Run("success", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@example.com", "oldpassword") + user := testutils.SetupUserData(db, "alice@example.com", "oldpassword") // Execute dat := url.Values{} @@ -923,29 +830,27 @@ func TestUpdatePassword(t *testing.T) { dat.Set("new_password_confirmation", "newpassword") req := testutils.MakeFormReq(server.URL, "PATCH", "/account/password", dat) - res := testutils.HTTPAuthDo(t, req, user) + res := testutils.HTTPAuthDo(t, db, req, user) // Test assert.StatusCodeEquals(t, res, http.StatusFound, "Status code mismsatch") - var account database.Account - testutils.MustExec(t, testutils.DB.Where("user_id = ?", user.ID).First(&account), "finding account") + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&user), "finding account") - passwordErr := bcrypt.CompareHashAndPassword([]byte(account.Password.String), []byte("newpassword")) + passwordErr := bcrypt.CompareHashAndPassword([]byte(user.Password.String), []byte("newpassword")) assert.Equal(t, passwordErr, nil, "Password mismatch") }) t.Run("old password mismatch", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() - u := testutils.SetupUserData() - a := testutils.SetupAccountData(u, "alice@example.com", "oldpassword") + u := testutils.SetupUserData(db, "alice@example.com", "oldpassword") // Execute dat := url.Values{} @@ -954,28 +859,27 @@ func TestUpdatePassword(t *testing.T) { dat.Set("new_password_confirmation", "newpassword") req := testutils.MakeFormReq(server.URL, "PATCH", "/account/password", dat) - res := testutils.HTTPAuthDo(t, req, u) + res := testutils.HTTPAuthDo(t, db, req, u) // Test assert.StatusCodeEquals(t, res, http.StatusUnauthorized, "Status code mismsatch") - var account database.Account - testutils.MustExec(t, testutils.DB.Where("user_id = ?", u.ID).First(&account), "finding account") - assert.Equal(t, a.Password.String, account.Password.String, "password should not have been updated") + var user database.User + testutils.MustExec(t, db.Where("id = ?", u.ID).First(&user), "finding account") + assert.Equal(t, u.Password.String, user.Password.String, "password should not have been updated") }) t.Run("password too short", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() - u := testutils.SetupUserData() - a := testutils.SetupAccountData(u, "alice@example.com", "oldpassword") + u := testutils.SetupUserData(db, "alice@example.com", "oldpassword") // Execute dat := url.Values{} @@ -984,28 +888,27 @@ func TestUpdatePassword(t *testing.T) { dat.Set("new_password_confirmation", "a") req := testutils.MakeFormReq(server.URL, "PATCH", "/account/password", dat) - res := testutils.HTTPAuthDo(t, req, u) + res := testutils.HTTPAuthDo(t, db, req, u) // Test assert.StatusCodeEquals(t, res, http.StatusBadRequest, "Status code mismsatch") - var account database.Account - testutils.MustExec(t, testutils.DB.Where("user_id = ?", u.ID).First(&account), "finding account") - assert.Equal(t, a.Password.String, account.Password.String, "password should not have been updated") + var user database.User + testutils.MustExec(t, db.Where("id = ?", u.ID).First(&user), "finding account") + assert.Equal(t, u.Password.String, user.Password.String, "password should not have been updated") }) t.Run("password confirmation mismatch", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() - u := testutils.SetupUserData() - a := testutils.SetupAccountData(u, "alice@example.com", "oldpassword") + u := testutils.SetupUserData(db, "alice@example.com", "oldpassword") // Execute dat := url.Values{} @@ -1014,32 +917,29 @@ func TestUpdatePassword(t *testing.T) { dat.Set("new_password_confirmation", "newpassword2") req := testutils.MakeFormReq(server.URL, "PATCH", "/account/password", dat) - res := testutils.HTTPAuthDo(t, req, u) + res := testutils.HTTPAuthDo(t, db, req, u) // Test assert.StatusCodeEquals(t, res, http.StatusBadRequest, "Status code mismsatch") - var account database.Account - testutils.MustExec(t, testutils.DB.Where("user_id = ?", u.ID).First(&account), "finding account") - assert.Equal(t, a.Password.String, account.Password.String, "password should not have been updated") + var user database.User + testutils.MustExec(t, db.Where("id = ?", u.ID).First(&user), "finding account") + assert.Equal(t, u.Password.String, user.Password.String, "password should not have been updated") }) } func TestUpdateEmail(t *testing.T) { t.Run("success", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() - u := testutils.SetupUserData() - a := testutils.SetupAccountData(u, "alice@example.com", "pass1234") - a.EmailVerified = true - testutils.MustExec(t, testutils.DB.Save(&a), "updating email_verified") + u := testutils.SetupUserData(db, "alice@example.com", "pass1234") // Execute dat := url.Values{} @@ -1047,34 +947,29 @@ func TestUpdateEmail(t *testing.T) { dat.Set("password", "pass1234") req := testutils.MakeFormReq(server.URL, "PATCH", "/account/profile", dat) - res := testutils.HTTPAuthDo(t, req, u) + res := testutils.HTTPAuthDo(t, db, req, u) // Test assert.StatusCodeEquals(t, res, http.StatusFound, "Status code mismatch") var user database.User - var account database.Account - testutils.MustExec(t, testutils.DB.Where("id = ?", u.ID).First(&user), "finding user") - testutils.MustExec(t, testutils.DB.Where("user_id = ?", u.ID).First(&account), "finding account") + testutils.MustExec(t, db.Where("id = ?", u.ID).First(&user), "finding user") + testutils.MustExec(t, db.Where("id = ?", u.ID).First(&user), "finding account") - assert.Equal(t, account.Email.String, "alice-new@example.com", "email mismatch") - assert.Equal(t, account.EmailVerified, false, "EmailVerified mismatch") + assert.Equal(t, user.Email.String, "alice-new@example.com", "email mismatch") }) t.Run("password mismatch", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) + a := app.NewTest() + a.Clock = clock.NewMock() + a.DB = db + server := MustNewServer(t, &a) defer server.Close() - u := testutils.SetupUserData() - a := testutils.SetupAccountData(u, "alice@example.com", "pass1234") - a.EmailVerified = true - testutils.MustExec(t, testutils.DB.Save(&a), "updating email_verified") + u := testutils.SetupUserData(db, "alice@example.com", "pass1234") // Execute dat := url.Values{} @@ -1082,249 +977,15 @@ func TestUpdateEmail(t *testing.T) { dat.Set("password", "wrongpassword") req := testutils.MakeFormReq(server.URL, "PATCH", "/account/profile", dat) - res := testutils.HTTPAuthDo(t, req, u) + res := testutils.HTTPAuthDo(t, db, req, u) // Test assert.StatusCodeEquals(t, res, http.StatusUnauthorized, "Status code mismsatch") var user database.User - var account database.Account - testutils.MustExec(t, testutils.DB.Where("id = ?", u.ID).First(&user), "finding user") - testutils.MustExec(t, testutils.DB.Where("user_id = ?", u.ID).First(&account), "finding account") + testutils.MustExec(t, db.Where("id = ?", u.ID).First(&user), "finding user") - assert.Equal(t, account.Email.String, "alice@example.com", "email mismatch") - assert.Equal(t, account.EmailVerified, true, "EmailVerified mismatch") + assert.Equal(t, user.Email.String, "alice@example.com", "email mismatch") }) } -func TestVerifyEmail(t *testing.T) { - t.Run("success", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) - - // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) - defer server.Close() - - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@example.com", "pass1234") - tok := database.Token{ - UserID: user.ID, - Type: database.TokenTypeEmailVerification, - Value: "someTokenValue", - } - testutils.MustExec(t, testutils.DB.Save(&tok), "preparing token") - - // Execute - req := testutils.MakeReq(server.URL, "GET", fmt.Sprintf("/verify-email/%s", "someTokenValue"), "") - res := testutils.HTTPAuthDo(t, req, user) - - // Test - assert.StatusCodeEquals(t, res, http.StatusFound, "Status code mismatch") - - var account database.Account - var token database.Token - var tokenCount int - testutils.MustExec(t, testutils.DB.Where("user_id = ?", user.ID).First(&account), "finding account") - testutils.MustExec(t, testutils.DB.Where("user_id = ? AND type = ?", user.ID, database.TokenTypeEmailVerification).First(&token), "finding token") - testutils.MustExec(t, testutils.DB.Model(&database.Token{}).Count(&tokenCount), "counting token") - - assert.Equal(t, account.EmailVerified, true, "email_verified mismatch") - assert.NotEqual(t, token.Value, "", "token value should not have been updated") - assert.Equal(t, tokenCount, 1, "token count mismatch") - assert.NotEqual(t, token.UsedAt, (*time.Time)(nil), "token should have been used") - }) - - t.Run("used token", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) - - // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) - defer server.Close() - - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@example.com", "pass1234") - - usedAt := time.Now().Add(time.Hour * -11).UTC() - tok := database.Token{ - UserID: user.ID, - Type: database.TokenTypeEmailVerification, - Value: "someTokenValue", - UsedAt: &usedAt, - } - testutils.MustExec(t, testutils.DB.Save(&tok), "preparing token") - - // Execute - req := testutils.MakeReq(server.URL, "GET", fmt.Sprintf("/verify-email/%s", "someTokenValue"), "") - res := testutils.HTTPAuthDo(t, req, user) - - // Test - assert.StatusCodeEquals(t, res, http.StatusBadRequest, "") - - var account database.Account - var token database.Token - var tokenCount int - testutils.MustExec(t, testutils.DB.Where("user_id = ?", user.ID).First(&account), "finding account") - testutils.MustExec(t, testutils.DB.Where("user_id = ? AND type = ?", user.ID, database.TokenTypeEmailVerification).First(&token), "finding token") - testutils.MustExec(t, testutils.DB.Model(&database.Token{}).Count(&tokenCount), "counting token") - - assert.Equal(t, account.EmailVerified, false, "email_verified mismatch") - assert.NotEqual(t, token.UsedAt, nil, "token used_at mismatch") - assert.Equal(t, tokenCount, 1, "token count mismatch") - assert.NotEqual(t, token.UsedAt, (*time.Time)(nil), "token should have been used") - }) - - t.Run("expired token", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) - - // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) - defer server.Close() - - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@example.com", "pass1234") - - tok := database.Token{ - UserID: user.ID, - Type: database.TokenTypeEmailVerification, - Value: "someTokenValue", - } - testutils.MustExec(t, testutils.DB.Save(&tok), "preparing token") - testutils.MustExec(t, testutils.DB.Model(&tok).Update("created_at", time.Now().Add(time.Minute*-31)), "Failed to prepare token created_at") - - // Execute - req := testutils.MakeReq(server.URL, "GET", fmt.Sprintf("/verify-email/%s", "someTokenValue"), "") - res := testutils.HTTPAuthDo(t, req, user) - - // Test - assert.StatusCodeEquals(t, res, http.StatusGone, "") - - var account database.Account - var token database.Token - var tokenCount int - testutils.MustExec(t, testutils.DB.Where("user_id = ?", user.ID).First(&account), "finding account") - testutils.MustExec(t, testutils.DB.Where("user_id = ? AND type = ?", user.ID, database.TokenTypeEmailVerification).First(&token), "finding token") - testutils.MustExec(t, testutils.DB.Model(&database.Token{}).Count(&tokenCount), "counting token") - - assert.Equal(t, account.EmailVerified, false, "email_verified mismatch") - assert.Equal(t, tokenCount, 1, "token count mismatch") - assert.Equal(t, token.UsedAt, (*time.Time)(nil), "token should have not been used") - }) - - t.Run("already verified", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) - - // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) - defer server.Close() - - user := testutils.SetupUserData() - a := testutils.SetupAccountData(user, "alice@example.com", "oldpass1234") - a.EmailVerified = true - testutils.MustExec(t, testutils.DB.Save(&a), "preparing account") - - tok := database.Token{ - UserID: user.ID, - Type: database.TokenTypeEmailVerification, - Value: "someTokenValue", - } - testutils.MustExec(t, testutils.DB.Save(&tok), "preparing token") - - // Execute - req := testutils.MakeReq(server.URL, "GET", fmt.Sprintf("/verify-email/%s", "someTokenValue"), "") - res := testutils.HTTPAuthDo(t, req, user) - - // Test - assert.StatusCodeEquals(t, res, http.StatusConflict, "") - - var account database.Account - var token database.Token - var tokenCount int - testutils.MustExec(t, testutils.DB.Where("user_id = ?", user.ID).First(&account), "finding account") - testutils.MustExec(t, testutils.DB.Where("user_id = ? AND type = ?", user.ID, database.TokenTypeEmailVerification).First(&token), "finding token") - testutils.MustExec(t, testutils.DB.Model(&database.Token{}).Count(&tokenCount), "counting token") - - assert.Equal(t, account.EmailVerified, true, "email_verified mismatch") - assert.Equal(t, tokenCount, 1, "token count mismatch") - assert.Equal(t, token.UsedAt, (*time.Time)(nil), "token should have not been used") - }) -} - -func TestCreateVerificationToken(t *testing.T) { - t.Run("success", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) - - // Setup - emailBackend := testutils.MockEmailbackendImplementation{} - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - EmailBackend: &emailBackend, - }) - defer server.Close() - - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@example.com", "pass1234") - - // Execute - req := testutils.MakeReq(server.URL, "POST", "/verification-token", "") - res := testutils.HTTPAuthDo(t, req, user) - - // Test - assert.StatusCodeEquals(t, res, http.StatusFound, "status code mismatch") - - var account database.Account - var token database.Token - var tokenCount int - testutils.MustExec(t, testutils.DB.Where("user_id = ?", user.ID).First(&account), "finding account") - testutils.MustExec(t, testutils.DB.Where("user_id = ? AND type = ?", user.ID, database.TokenTypeEmailVerification).First(&token), "finding token") - testutils.MustExec(t, testutils.DB.Model(&database.Token{}).Count(&tokenCount), "counting token") - - assert.Equal(t, account.EmailVerified, false, "email_verified should not have been updated") - assert.NotEqual(t, token.Value, "", "token Value mismatch") - assert.Equal(t, tokenCount, 1, "token count mismatch") - assert.Equal(t, token.UsedAt, (*time.Time)(nil), "token UsedAt mismatch") - assert.Equal(t, len(emailBackend.Emails), 1, "email queue count mismatch") - }) - - t.Run("already verified", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) - // Setup - server := MustNewServer(t, &app.App{ - Clock: clock.NewMock(), - Config: config.Config{}, - }) - defer server.Close() - - user := testutils.SetupUserData() - a := testutils.SetupAccountData(user, "alice@example.com", "pass1234") - a.EmailVerified = true - testutils.MustExec(t, testutils.DB.Save(&a), "preparing account") - - // Execute - req := testutils.MakeReq(server.URL, "POST", "/verification-token", "") - res := testutils.HTTPAuthDo(t, req, user) - - // Test - assert.StatusCodeEquals(t, res, http.StatusConflict, "Status code mismatch") - - var account database.Account - var tokenCount int - testutils.MustExec(t, testutils.DB.Where("user_id = ?", user.ID).First(&account), "finding account") - testutils.MustExec(t, testutils.DB.Model(&database.Token{}).Count(&tokenCount), "counting token") - - assert.Equal(t, account.EmailVerified, true, "email_verified should not have been updated") - assert.Equal(t, tokenCount, 0, "token count mismatch") - }) -} diff --git a/pkg/server/crypt/crypt.go b/pkg/server/crypt/crypt.go index 1c960db6..fb0edd26 100644 --- a/pkg/server/crypt/crypt.go +++ b/pkg/server/crypt/crypt.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package crypt diff --git a/pkg/server/database/consts.go b/pkg/server/database/consts.go index 6bc0e15c..4406ee5c 100644 --- a/pkg/server/database/consts.go +++ b/pkg/server/database/consts.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package database @@ -21,10 +18,6 @@ package database const ( // TokenTypeResetPassword is a type of a token for reseting password TokenTypeResetPassword = "reset_password" - // TokenTypeEmailVerification is a type of a token for verifying email - TokenTypeEmailVerification = "email_verification" - // TokenTypeEmailPreference is a type of a token for updating email preference - TokenTypeEmailPreference = "email_preference" ) const ( diff --git a/pkg/server/database/database.go b/pkg/server/database/database.go index 2a7c01d1..bd7869bc 100644 --- a/pkg/server/database/database.go +++ b/pkg/server/database/database.go @@ -1,30 +1,30 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package database import ( - "github.com/dnote/dnote/pkg/server/config" - "github.com/jinzhu/gorm" - "github.com/pkg/errors" + "os" + "path/filepath" + "time" - // Use postgres - _ "github.com/lib/pq" + "github.com/dnote/dnote/pkg/server/log" + "github.com/pkg/errors" + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "gorm.io/gorm/logger" ) var ( @@ -32,32 +32,107 @@ var ( MigrationTableName = "migrations" ) +// getDBLogLevel converts application log level to GORM log level +func getDBLogLevel(level string) logger.LogLevel { + switch level { + case log.LevelDebug: + return logger.Info + case log.LevelInfo: + return logger.Silent + case log.LevelWarn: + return logger.Warn + case log.LevelError: + return logger.Error + default: + return logger.Silent + } +} + // InitSchema migrates database schema to reflect the latest model definition func InitSchema(db *gorm.DB) { - if err := db.Exec(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`).Error; err != nil { - panic(err) - } - if err := db.AutoMigrate( - Note{}, - Book{}, - User{}, - Account{}, - Notification{}, - Token{}, - EmailPreference{}, - Session{}, - ).Error; err != nil { + &User{}, + &Book{}, + &Note{}, + &Token{}, + &Session{}, + ); err != nil { panic(err) } } // Open initializes the database connection -func Open(c config.Config) *gorm.DB { - db, err := gorm.Open("postgres", c.DB.GetConnectionStr()) +func Open(dbPath string) *gorm.DB { + // Create directory if it doesn't exist + dir := filepath.Dir(dbPath) + if err := os.MkdirAll(dir, 0755); err != nil { + panic(errors.Wrapf(err, "creating database directory at %s", dir)) + } + + db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{ + Logger: logger.Default.LogMode(getDBLogLevel(log.GetLevel())), + }) if err != nil { panic(errors.Wrap(err, "opening database conection")) } + // Get underlying *sql.DB to configure connection pool + sqlDB, err := db.DB() + if err != nil { + panic(errors.Wrap(err, "getting underlying database connection")) + } + + // Configure connection pool for SQLite with WAL mode + sqlDB.SetMaxOpenConns(25) + sqlDB.SetMaxIdleConns(5) + sqlDB.SetConnMaxLifetime(0) // Doesn't expire. + + // Apply performance PRAGMAs + pragmas := []string{ + "PRAGMA journal_mode=WAL", // Enable WAL mode for better concurrency + "PRAGMA synchronous=NORMAL", // Balance between safety and speed + "PRAGMA cache_size=-64000", // 64MB cache (negative = KB) + "PRAGMA busy_timeout=5000", // Wait up to 5s for locks + "PRAGMA foreign_keys=ON", // Enforce foreign key constraints + "PRAGMA temp_store=MEMORY", // Store temp tables in memory + } + + for _, pragma := range pragmas { + if err := db.Exec(pragma).Error; err != nil { + panic(errors.Wrapf(err, "executing pragma: %s", pragma)) + } + } + return db } + +// StartWALCheckpointing starts a background goroutine that periodically +// checkpoints the WAL file to prevent it from growing unbounded +func StartWALCheckpointing(db *gorm.DB, interval time.Duration) { + go func() { + ticker := time.NewTicker(interval) + defer ticker.Stop() + + for range ticker.C { + // TRUNCATE mode removes the WAL file after checkpointing + if err := db.Exec("PRAGMA wal_checkpoint(TRUNCATE)").Error; err != nil { + log.ErrorWrap(err, "WAL checkpoint failed") + } + } + }() +} + +// StartPeriodicVacuum runs full VACUUM on a schedule to reclaim space and defragment. +// VACUUM acquires an exclusive lock and blocks all database operations briefly. +func StartPeriodicVacuum(db *gorm.DB, interval time.Duration) { + go func() { + ticker := time.NewTicker(interval) + defer ticker.Stop() + + for range ticker.C { + if err := db.Exec("VACUUM").Error; err != nil { + log.ErrorWrap(err, "VACUUM failed") + } + } + }() +} diff --git a/pkg/server/database/database_test.go b/pkg/server/database/database_test.go new file mode 100644 index 00000000..3d3f5b92 --- /dev/null +++ b/pkg/server/database/database_test.go @@ -0,0 +1,70 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package database + +import ( + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/server/log" + "gorm.io/gorm/logger" +) + +func TestGetDBLogLevel(t *testing.T) { + testCases := []struct { + name string + level string + expected logger.LogLevel + }{ + { + name: "debug level maps to Info", + level: log.LevelDebug, + expected: logger.Info, + }, + { + name: "info level maps to Silent", + level: log.LevelInfo, + expected: logger.Silent, + }, + { + name: "warn level maps to Warn", + level: log.LevelWarn, + expected: logger.Warn, + }, + { + name: "error level maps to Error", + level: log.LevelError, + expected: logger.Error, + }, + { + name: "unknown level maps to Silent", + level: "unknown", + expected: logger.Silent, + }, + { + name: "empty string maps to Silent", + level: "", + expected: logger.Silent, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := getDBLogLevel(tc.level) + assert.Equal(t, result, tc.expected, "log level mismatch") + }) + } +} diff --git a/pkg/server/database/errors.go b/pkg/server/database/errors.go index fee3016f..ada00a6d 100644 --- a/pkg/server/database/errors.go +++ b/pkg/server/database/errors.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package database diff --git a/pkg/server/database/migrate.go b/pkg/server/database/migrate.go index e5d6de2f..59e4c820 100644 --- a/pkg/server/database/migrate.go +++ b/pkg/server/database/migrate.go @@ -1,47 +1,182 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package database import ( - "log" - "net/http" + "fmt" + "io/fs" + "sort" + "strings" "github.com/dnote/dnote/pkg/server/database/migrations" - "github.com/jinzhu/gorm" + "github.com/dnote/dnote/pkg/server/log" "github.com/pkg/errors" - "github.com/rubenv/sql-migrate" + "gorm.io/gorm" ) -// Migrate runs the migrations -func Migrate(db *gorm.DB) error { - migrations := &migrate.HttpFileSystemMigrationSource{ - FileSystem: http.FileSystem(http.FS(migrations.Files)), +type migrationFile struct { + filename string + version int +} + +// validateMigrationFilename checks if filename follows format: NNN-description.sql +func validateMigrationFilename(name string) error { + // Check .sql extension + if !strings.HasSuffix(name, ".sql") { + return errors.Errorf("invalid migration filename: must end with .sql") } - migrate.SetTable(MigrationTableName) - - n, err := migrate.Exec(db.DB(), "postgres", migrations, migrate.Up) - if err != nil { - return errors.Wrap(err, "running migrations") + name = strings.TrimSuffix(name, ".sql") + parts := strings.SplitN(name, "-", 2) + if len(parts) != 2 { + return errors.Errorf("invalid migration filename: must be NNN-description.sql") } - log.Printf("Performed %d migrations", n) + version, description := parts[0], parts[1] + + // Validate version is 3 digits + if len(version) != 3 { + return errors.Errorf("invalid migration filename: version must be 3 digits, got %s", version) + } + for _, c := range version { + if c < '0' || c > '9' { + return errors.Errorf("invalid migration filename: version must be numeric, got %s", version) + } + } + + // Validate description is not empty + if description == "" { + return errors.Errorf("invalid migration filename: description is required") + } + + return nil +} + +// Migrate runs the migrations using the embedded migration files +func Migrate(db *gorm.DB) error { + return migrate(db, migrations.Files) +} + +// getMigrationFiles reads, validates, and sorts migration files +func getMigrationFiles(fsys fs.FS) ([]migrationFile, error) { + entries, err := fs.ReadDir(fsys, ".") + if err != nil { + return nil, errors.Wrap(err, "reading migration directory") + } + + var migrations []migrationFile + seen := make(map[int]string) + for _, e := range entries { + name := e.Name() + + if err := validateMigrationFilename(name); err != nil { + return nil, err + } + + // Parse version + var v int + fmt.Sscanf(name, "%d", &v) + + // Check for duplicate version numbers + if existing, found := seen[v]; found { + return nil, errors.Errorf("duplicate migration version %d: %s and %s", v, existing, name) + } + seen[v] = name + + migrations = append(migrations, migrationFile{ + filename: name, + version: v, + }) + } + + // Sort by version + sort.Slice(migrations, func(i, j int) bool { + return migrations[i].version < migrations[j].version + }) + + return migrations, nil +} + +// migrate runs migrations from the provided filesystem +func migrate(db *gorm.DB, fsys fs.FS) error { + if err := db.Exec(` + CREATE TABLE IF NOT EXISTS schema_migrations ( + version INTEGER PRIMARY KEY, + applied_at DATETIME DEFAULT CURRENT_TIMESTAMP + ) + `).Error; err != nil { + return errors.Wrap(err, "initializing migration table") + } + + // Get current version + var version int + if err := db.Raw("SELECT COALESCE(MAX(version), 0) FROM schema_migrations").Scan(&version).Error; err != nil { + return errors.Wrap(err, "reading current version") + } + + // Read and validate migration files + migrations, err := getMigrationFiles(fsys) + if err != nil { + return err + } + + var filenames []string + for _, m := range migrations { + filenames = append(filenames, m.filename) + } + + log.WithFields(log.Fields{ + "version": version, + }).Info("Database schema version.") + + log.WithFields(log.Fields{ + "files": filenames, + }).Debug("Database migration files.") + + // Apply pending migrations + for _, m := range migrations { + if m.version <= version { + continue + } + + log.WithFields(log.Fields{ + "file": m.filename, + }).Info("Applying migration.") + + sql, err := fs.ReadFile(fsys, m.filename) + if err != nil { + return errors.Wrapf(err, "reading migration file %s", m.filename) + } + + if len(strings.TrimSpace(string(sql))) == 0 { + return errors.Errorf("migration file %s is empty", m.filename) + } + + if err := db.Exec(string(sql)).Error; err != nil { + return fmt.Errorf("migration %s failed: %w", m.filename, err) + } + + if err := db.Exec("INSERT INTO schema_migrations (version) VALUES (?)", m.version).Error; err != nil { + return errors.Wrapf(err, "recording migration %s", m.filename) + } + + log.WithFields(log.Fields{ + "file": m.filename, + }).Info("Migrate success.") + } return nil } diff --git a/pkg/server/database/migrate/main.go b/pkg/server/database/migrate/main.go deleted file mode 100644 index 16b97cf5..00000000 --- a/pkg/server/database/migrate/main.go +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package main - -import ( - "flag" - "fmt" - "os" - - "github.com/dnote/dnote/pkg/server/config" - "github.com/dnote/dnote/pkg/server/database" - "github.com/joho/godotenv" - "github.com/pkg/errors" - "github.com/rubenv/sql-migrate" -) - -var ( - migrationDir = flag.String("migrationDir", "../migrations", "the path to the directory with migraiton files") -) - -func init() { - fmt.Println("Migrating Dnote database...") - - // Load env - if os.Getenv("GO_ENV") != "PRODUCTION" { - if err := godotenv.Load("../../.env.dev"); err != nil { - panic(err) - } - } - -} - -func main() { - flag.Parse() - - c := config.Load() - db := database.Open(c) - - migrations := &migrate.FileMigrationSource{ - Dir: *migrationDir, - } - - migrate.SetTable("migrations") - - n, err := migrate.Exec(db.DB(), "postgres", migrations, migrate.Up) - if err != nil { - panic(errors.Wrap(err, "executing migrations")) - } - - fmt.Printf("Applied %d migrations\n", n) -} diff --git a/pkg/server/database/migrate_test.go b/pkg/server/database/migrate_test.go new file mode 100644 index 00000000..4ee93627 --- /dev/null +++ b/pkg/server/database/migrate_test.go @@ -0,0 +1,310 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package database + +import ( + "io/fs" + "testing" + "testing/fstest" + + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +// unsortedFS wraps fstest.MapFS to return entries in reverse order +type unsortedFS struct { + fstest.MapFS +} + +func (u unsortedFS) ReadDir(name string) ([]fs.DirEntry, error) { + entries, err := u.MapFS.ReadDir(name) + if err != nil { + return nil, err + } + // Reverse the entries to ensure they're not in sorted order + for i, j := 0, len(entries)-1; i < j; i, j = i+1, j-1 { + entries[i], entries[j] = entries[j], entries[i] + } + return entries, nil +} + +// errorFS returns an error on ReadDir +type errorFS struct{} + +func (e errorFS) Open(name string) (fs.File, error) { + return nil, fs.ErrNotExist +} + +func (e errorFS) ReadDir(name string) ([]fs.DirEntry, error) { + return nil, fs.ErrPermission +} + +func TestMigrate_createsSchemaTable(t *testing.T) { + db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open database: %v", err) + } + + migrationsFs := fstest.MapFS{} + migrate(db, migrationsFs) + + // Verify schema_migrations table exists + var count int64 + if err := db.Raw("SELECT COUNT(*) FROM schema_migrations").Scan(&count).Error; err != nil { + t.Fatalf("schema_migrations table not found: %v", err) + } +} + +func TestMigrate_idempotency(t *testing.T) { + db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open database: %v", err) + } + + // Set up table before migration + if err := db.Exec("CREATE TABLE counter (value INTEGER)").Error; err != nil { + t.Fatalf("failed to create table: %v", err) + } + + // Create migration that inserts a row + migrationsFs := fstest.MapFS{ + "001-insert-data.sql": &fstest.MapFile{ + Data: []byte("INSERT INTO counter (value) VALUES (100);"), + }, + } + + // Run migration first time + if err := migrate(db, migrationsFs); err != nil { + t.Fatalf("first migration failed: %v", err) + } + var count int64 + if err := db.Raw("SELECT COUNT(*) FROM counter").Scan(&count).Error; err != nil { + t.Fatalf("failed to count rows: %v", err) + } + if count != 1 { + t.Errorf("expected 1 row, got %d", count) + } + + // Run migration second time - it should not run the SQL again + if err := migrate(db, migrationsFs); err != nil { + t.Fatalf("second migration failed: %v", err) + } + if err := db.Raw("SELECT COUNT(*) FROM counter").Scan(&count).Error; err != nil { + t.Fatalf("failed to count rows: %v", err) + } + if count != 1 { + t.Errorf("migration ran twice: expected 1 row, got %d", count) + } +} + +func TestMigrate_ordering(t *testing.T) { + db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open database: %v", err) + } + + // Create table before migrations + if err := db.Exec("CREATE TABLE log (value INTEGER)").Error; err != nil { + t.Fatalf("failed to create table: %v", err) + } + + // Create migrations with unsorted filesystem + migrationsFs := unsortedFS{ + MapFS: fstest.MapFS{ + "010-tenth.sql": &fstest.MapFile{ + Data: []byte("INSERT INTO log (value) VALUES (3);"), + }, + "001-first.sql": &fstest.MapFile{ + Data: []byte("INSERT INTO log (value) VALUES (1);"), + }, + "002-second.sql": &fstest.MapFile{ + Data: []byte("INSERT INTO log (value) VALUES (2);"), + }, + }, + } + + // Run migrations + if err := migrate(db, migrationsFs); err != nil { + t.Fatalf("migration failed: %v", err) + } + + // Verify migrations ran in correct order (1, 2, 3) + var values []int + if err := db.Raw("SELECT value FROM log ORDER BY rowid").Scan(&values).Error; err != nil { + t.Fatalf("failed to query log: %v", err) + } + + expected := []int{1, 2, 3} + if len(values) != len(expected) { + t.Fatalf("expected %d rows, got %d", len(expected), len(values)) + } + + for i, v := range values { + if v != expected[i] { + t.Errorf("row %d: expected value %d, got %d", i, expected[i], v) + } + } +} + +func TestMigrate_duplicateVersion(t *testing.T) { + db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open database: %v", err) + } + + // Create migrations with duplicate version numbers + migrationsFs := fstest.MapFS{ + "001-first.sql": &fstest.MapFile{ + Data: []byte("SELECT 1;"), + }, + "001-second.sql": &fstest.MapFile{ + Data: []byte("SELECT 2;"), + }, + } + + // Should return error for duplicate version + err = migrate(db, migrationsFs) + if err == nil { + t.Fatal("expected error for duplicate version numbers, got nil") + } +} + +func TestMigrate_initTableError(t *testing.T) { + db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open database: %v", err) + } + + // Close the database connection to cause exec to fail + sqlDB, _ := db.DB() + sqlDB.Close() + + migrationsFs := fstest.MapFS{ + "001-init.sql": &fstest.MapFile{ + Data: []byte("SELECT 1;"), + }, + } + + // Should return error for table initialization failure + err = migrate(db, migrationsFs) + if err == nil { + t.Fatal("expected error for table initialization failure, got nil") + } +} + +func TestMigrate_readDirError(t *testing.T) { + db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open database: %v", err) + } + + // Use filesystem that fails on ReadDir + err = migrate(db, errorFS{}) + if err == nil { + t.Fatal("expected error for ReadDir failure, got nil") + } +} + +func TestMigrate_sqlError(t *testing.T) { + db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open database: %v", err) + } + + // Create migration with invalid SQL + migrationsFs := fstest.MapFS{ + "001-bad-sql.sql": &fstest.MapFile{ + Data: []byte("INVALID SQL SYNTAX HERE;"), + }, + } + + // Should return error for SQL execution failure + err = migrate(db, migrationsFs) + if err == nil { + t.Fatal("expected error for invalid SQL, got nil") + } +} + +func TestMigrate_emptyFile(t *testing.T) { + db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open database: %v", err) + } + + tests := []struct { + name string + data string + wantErr bool + }{ + {"completely empty", "", true}, + {"only whitespace", " \n\t ", true}, + {"only comments", "-- just a comment", false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + migrationsFs := fstest.MapFS{ + "001-empty.sql": &fstest.MapFile{ + Data: []byte(tt.data), + }, + } + + err = migrate(db, migrationsFs) + if (err != nil) != tt.wantErr { + t.Errorf("error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func TestMigrate_invalidFilename(t *testing.T) { + db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open database: %v", err) + } + + tests := []struct { + name string + filename string + wantErr bool + }{ + {"valid format", "001-init.sql", false}, + {"no leading zeros", "1-init.sql", true}, + {"two digits", "01-init.sql", true}, + {"no dash", "001init.sql", true}, + {"no description", "001-.sql", true}, + {"no extension", "001-init.", true}, + {"wrong extension", "001-init.txt", true}, + {"non-numeric version number", "0a1-init.sql", true}, + {"underscore separator", "001_init.sql", true}, + {"multiple dashes in description", "001-add-feature-v2.sql", false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + migrationsFs := fstest.MapFS{ + tt.filename: &fstest.MapFile{ + Data: []byte("SELECT 1;"), + }, + } + + err := migrate(db, migrationsFs) + if (err != nil) != tt.wantErr { + t.Errorf("error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/pkg/server/database/migrations/.gitkeep b/pkg/server/database/migrations/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/pkg/server/database/migrations/100-create-fts-table.sql b/pkg/server/database/migrations/100-create-fts-table.sql new file mode 100644 index 00000000..21b43704 --- /dev/null +++ b/pkg/server/database/migrations/100-create-fts-table.sql @@ -0,0 +1,18 @@ +-- Create FTS5 virtual table for full-text search on notes +CREATE VIRTUAL TABLE IF NOT EXISTS notes_fts USING fts5( + content=notes, + body, + tokenize="porter unicode61 categories 'L* N* Co Ps Pe'" +); + +-- Create triggers to keep notes_fts in sync with notes +CREATE TRIGGER IF NOT EXISTS notes_insert AFTER INSERT ON notes BEGIN + INSERT INTO notes_fts(rowid, body) VALUES (new.rowid, new.body); +END; +CREATE TRIGGER IF NOT EXISTS notes_delete AFTER DELETE ON notes BEGIN + INSERT INTO notes_fts(notes_fts, rowid, body) VALUES ('delete', old.rowid, old.body); +END; +CREATE TRIGGER IF NOT EXISTS notes_update AFTER UPDATE ON notes BEGIN + INSERT INTO notes_fts(notes_fts, rowid, body) VALUES ('delete', old.rowid, old.body); + INSERT INTO notes_fts(rowid, body) VALUES (new.rowid, new.body); +END; \ No newline at end of file diff --git a/pkg/server/database/migrations/20190819115834-full-text-search.sql b/pkg/server/database/migrations/20190819115834-full-text-search.sql deleted file mode 100644 index b3d884e9..00000000 --- a/pkg/server/database/migrations/20190819115834-full-text-search.sql +++ /dev/null @@ -1,41 +0,0 @@ - --- +migrate Up - --- Configure full text search -CREATE TEXT SEARCH DICTIONARY english_nostop ( - Template = snowball, - Language = english -); - -CREATE TEXT SEARCH CONFIGURATION public.english_nostop ( COPY = pg_catalog.english ); - -ALTER TEXT SEARCH CONFIGURATION public.english_nostop -ALTER MAPPING FOR asciiword, asciihword, hword_asciipart, hword, hword_part, word WITH english_nostop; - - --- Create a trigger --- +migrate StatementBegin -CREATE OR REPLACE FUNCTION note_tsv_trigger() RETURNS trigger AS $$ -begin - new.tsv := setweight(to_tsvector('english_nostop', new.body), 'A'); - return new; -end -$$ LANGUAGE plpgsql; - -DROP TRIGGER IF EXISTS tsvectorupdate ON notes; -CREATE TRIGGER tsvectorupdate -BEFORE INSERT OR UPDATE ON notes -FOR EACH ROW EXECUTE PROCEDURE note_tsv_trigger(); --- +migrate StatementEnd - --- index tsv -CREATE INDEX IF NOT EXISTS idx_notes_tsv -ON notes -USING gin(tsv); - --- initialize tsv -UPDATE notes -SET tsv = setweight(to_tsvector('english_nostop', notes.body), 'A') -WHERE notes.encrypted = false; - --- +migrate Down diff --git a/pkg/server/database/migrations/20191028103522-create-weekly-repetition.sql b/pkg/server/database/migrations/20191028103522-create-weekly-repetition.sql deleted file mode 100644 index 22ac6f3a..00000000 --- a/pkg/server/database/migrations/20191028103522-create-weekly-repetition.sql +++ /dev/null @@ -1,8 +0,0 @@ --- this migration is noop because repetition_rules have been removed - --- create-weekly-repetition.sql creates the default repetition rules for the users --- that used to have the weekly email digest on Friday 20:00 UTC - --- +migrate Up - --- +migrate Down diff --git a/pkg/server/database/migrations/20191225185502-populate-digest-version.sql b/pkg/server/database/migrations/20191225185502-populate-digest-version.sql deleted file mode 100644 index 73098dbf..00000000 --- a/pkg/server/database/migrations/20191225185502-populate-digest-version.sql +++ /dev/null @@ -1,9 +0,0 @@ --- this migration is noop because digests have been removed - --- populate-digest-version.sql populates the `version` column for the digests --- by assigining an incremental integer scoped to a repetition rule that each --- digest belongs, ordered by created_at timestamp of the digests. - --- +migrate Up - --- +migrate Down diff --git a/pkg/server/database/migrations/20191226093447-add-digest-id-primary-key.sql b/pkg/server/database/migrations/20191226093447-add-digest-id-primary-key.sql deleted file mode 100644 index c7d17d2e..00000000 --- a/pkg/server/database/migrations/20191226093447-add-digest-id-primary-key.sql +++ /dev/null @@ -1,5 +0,0 @@ --- this migration is noop because digests have been removed - --- +migrate Up - --- +migrate Down diff --git a/pkg/server/database/migrations/20191226105659-use-id-in-digest-notes-joining-table.sql b/pkg/server/database/migrations/20191226105659-use-id-in-digest-notes-joining-table.sql deleted file mode 100644 index faec52ff..00000000 --- a/pkg/server/database/migrations/20191226105659-use-id-in-digest-notes-joining-table.sql +++ /dev/null @@ -1,8 +0,0 @@ --- this migration is noop because digests have been removed - --- -use-id-in-digest-notes-joining-table.sql replaces uuids with ids --- as foreign keys in the digest_notes joining table. - --- +migrate Up - --- +migrate Down diff --git a/pkg/server/database/migrations/20191226152111-delete-outdated-digests.sql b/pkg/server/database/migrations/20191226152111-delete-outdated-digests.sql deleted file mode 100644 index 84c8ccf3..00000000 --- a/pkg/server/database/migrations/20191226152111-delete-outdated-digests.sql +++ /dev/null @@ -1,8 +0,0 @@ --- this migration is noop because digests have been removed - --- delete-outdated-digests.sql deletes digests that do not belong to any repetition rules, --- along with digest_notes associations. - --- +migrate Up - --- +migrate Down diff --git a/pkg/server/database/migrations/20200522170529-remove-billing-columns.sql b/pkg/server/database/migrations/20200522170529-remove-billing-columns.sql deleted file mode 100644 index a814f26b..00000000 --- a/pkg/server/database/migrations/20200522170529-remove-billing-columns.sql +++ /dev/null @@ -1,9 +0,0 @@ --- remove-billing-columns.sql drops billing related columns that are now obsolete. - --- +migrate Up - -ALTER TABLE users DROP COLUMN IF EXISTS stripe_customer_id; -ALTER TABLE users DROP COLUMN IF EXISTS billing_country; - --- +migrate Down - diff --git a/pkg/server/database/migrations/embed.go b/pkg/server/database/migrations/embed.go index d5764ae3..1c644d57 100644 --- a/pkg/server/database/migrations/embed.go +++ b/pkg/server/database/migrations/embed.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package migrations diff --git a/pkg/server/database/models.go b/pkg/server/database/models.go index 06a5afbd..6ee4d3a7 100644 --- a/pkg/server/database/models.go +++ b/pkg/server/database/models.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package database @@ -24,61 +21,49 @@ import ( // Model is the base model definition type Model struct { - ID int `gorm:"primary_key" json:"-"` - CreatedAt time.Time `json:"created_at" gorm:"default:now()"` - UpdatedAt time.Time `json:"updated_at"` + ID int `gorm:"primaryKey" json:"-"` + CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"` + UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"` } // Book is a model for a book type Book struct { Model - UUID string `json:"uuid" gorm:"index;type:uuid;default:uuid_generate_v4()"` + UUID string `json:"uuid" gorm:"uniqueIndex;type:text"` UserID int `json:"user_id" gorm:"index"` Label string `json:"label" gorm:"index"` - Notes []Note `json:"notes" gorm:"foreignkey:book_uuid"` + Notes []Note `json:"notes" gorm:"foreignKey:BookUUID;references:UUID"` AddedOn int64 `json:"added_on"` EditedOn int64 `json:"edited_on"` USN int `json:"-" gorm:"index"` Deleted bool `json:"-" gorm:"default:false"` - Encrypted bool `json:"-" gorm:"default:false"` } // Note is a model for a note type Note struct { Model - UUID string `json:"uuid" gorm:"index;type:uuid;default:uuid_generate_v4()"` - Book Book `json:"book" gorm:"foreignkey:BookUUID"` + UUID string `json:"uuid" gorm:"index;type:text"` + Book Book `json:"book" gorm:"foreignKey:BookUUID;references:UUID"` User User `json:"user"` UserID int `json:"user_id" gorm:"index"` - BookUUID string `json:"book_uuid" gorm:"index;type:uuid"` + BookUUID string `json:"book_uuid" gorm:"index;type:text"` Body string `json:"content"` AddedOn int64 `json:"added_on"` EditedOn int64 `json:"edited_on"` - TSV string `json:"-" gorm:"type:tsvector"` - Public bool `json:"public" gorm:"default:false"` USN int `json:"-" gorm:"index"` Deleted bool `json:"-" gorm:"default:false"` - Encrypted bool `json:"-" gorm:"default:false"` Client string `gorm:"index"` } // User is a model for a user type User struct { Model - UUID string `json:"uuid" gorm:"type:uuid;index;default:uuid_generate_v4()"` - Account Account - LastLoginAt *time.Time `json:"-"` - MaxUSN int `json:"-" gorm:"default:0"` - Cloud bool `json:"-" gorm:"default:false"` -} - -// Account is a model for an account -type Account struct { - Model - UserID int `gorm:"index"` - Email NullString - EmailVerified bool `gorm:"default:false"` - Password NullString + UUID string `json:"uuid" gorm:"type:text;index"` + Email NullString `gorm:"index"` + Password NullString `json:"-"` + LastLoginAt *time.Time `json:"-"` + MaxUSN int `json:"-" gorm:"default:0"` + FullSyncBefore int64 `json:"-" gorm:"default:0"` } // Token is a model for a token @@ -90,21 +75,6 @@ type Token struct { UsedAt *time.Time } -// Notification is the learning notification sent to the user -type Notification struct { - Model - Type string - UserID int `gorm:"index"` -} - -// EmailPreference is a preference per user for receiving email communication -type EmailPreference struct { - Model - UserID int `gorm:"index" json:"-"` - InactiveReminder bool `json:"inactive_reminder" gorm:"default:false"` - ProductUpdate bool `json:"product_update" gorm:"default:true"` -} - // Session represents a user session type Session struct { Model diff --git a/pkg/server/database/notes.go b/pkg/server/database/notes.go index 96471098..b8cb8450 100644 --- a/pkg/server/database/notes.go +++ b/pkg/server/database/notes.go @@ -1,25 +1,22 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package database import ( - "github.com/jinzhu/gorm" + "gorm.io/gorm" ) // PreloadNote preloads the associations for a notes for the given query diff --git a/pkg/server/database/scripts/create-migration.sh b/pkg/server/database/scripts/create-migration.sh deleted file mode 100755 index ef168165..00000000 --- a/pkg/server/database/scripts/create-migration.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env bash -# create-migration.sh creates a new SQL migration file for the -# server side Postgres database using the sql-migrate tool. -set -eux - -is_command () { - command -v "$1" >/dev/null 2>&1; -} - -if ! is_command sql-migrate; then - echo "sql-migrate is not found. Please run install-sql-migrate.sh" - exit 1 -fi - -if [ "$#" == 0 ]; then - echo "filename not provided" - exit 1 -fi - -filename=$1 -sql-migrate new -config=./sql-migrate.yml "$filename" diff --git a/pkg/server/database/scripts/install-sql-migrate.sh b/pkg/server/database/scripts/install-sql-migrate.sh deleted file mode 100755 index 334fb817..00000000 --- a/pkg/server/database/scripts/install-sql-migrate.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash - -go get -v github.com/rubenv/sql-migrate/... diff --git a/pkg/server/database/sql-migrate.yml b/pkg/server/database/sql-migrate.yml deleted file mode 100644 index f9c90d83..00000000 --- a/pkg/server/database/sql-migrate.yml +++ /dev/null @@ -1,8 +0,0 @@ -# A configuration for sql-migrate tool for generating migrations -# using `sql-migrate new`. This file is not actually used for running -# migrations because we run them programmatically. - -development: - dialect: postgres - datasource: dbname=dnote sslmode=disable - dir: ./migrations diff --git a/pkg/server/database/types.go b/pkg/server/database/types.go index 4e573d00..60544c29 100644 --- a/pkg/server/database/types.go +++ b/pkg/server/database/types.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package database diff --git a/pkg/server/helpers/const.go b/pkg/server/helpers/const.go index c7a31f23..3a634d09 100644 --- a/pkg/server/helpers/const.go +++ b/pkg/server/helpers/const.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package helpers diff --git a/pkg/server/helpers/url.go b/pkg/server/helpers/url.go index 2f4eb8f8..0d935754 100644 --- a/pkg/server/helpers/url.go +++ b/pkg/server/helpers/url.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package helpers diff --git a/pkg/server/helpers/url_test.go b/pkg/server/helpers/url_test.go index 3fdfeccc..78142b29 100644 --- a/pkg/server/helpers/url_test.go +++ b/pkg/server/helpers/url_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package helpers diff --git a/pkg/server/helpers/uuid.go b/pkg/server/helpers/uuid.go index 4365a351..ed1090d4 100644 --- a/pkg/server/helpers/uuid.go +++ b/pkg/server/helpers/uuid.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package helpers diff --git a/pkg/server/job/job.go b/pkg/server/job/job.go deleted file mode 100644 index 2d3e764c..00000000 --- a/pkg/server/job/job.go +++ /dev/null @@ -1,127 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package job - -import ( - slog "log" - - "github.com/dnote/dnote/pkg/clock" - "github.com/dnote/dnote/pkg/server/config" - "github.com/dnote/dnote/pkg/server/mailer" - "github.com/jinzhu/gorm" - "github.com/pkg/errors" - "github.com/robfig/cron" -) - -var ( - // ErrEmptyDB is an error for missing database connection in the app configuration - ErrEmptyDB = errors.New("No database connection was provided") - // ErrEmptyClock is an error for missing clock in the app configuration - ErrEmptyClock = errors.New("No clock was provided") - // ErrEmptyWebURL is an error for missing WebURL content in the app configuration - ErrEmptyWebURL = errors.New("No WebURL was provided") - // ErrEmptyEmailTemplates is an error for missing EmailTemplates content in the app configuration - ErrEmptyEmailTemplates = errors.New("No EmailTemplate store was provided") - // ErrEmptyEmailBackend is an error for missing EmailBackend content in the app configuration - ErrEmptyEmailBackend = errors.New("No EmailBackend was provided") -) - -// Runner is a configuration for job -type Runner struct { - DB *gorm.DB - Clock clock.Clock - EmailTmpl mailer.Templates - EmailBackend mailer.Backend - Config config.Config -} - -// NewRunner returns a new runner -func NewRunner(db *gorm.DB, c clock.Clock, t mailer.Templates, b mailer.Backend, config config.Config) (Runner, error) { - ret := Runner{ - DB: db, - EmailTmpl: t, - EmailBackend: b, - Clock: c, - Config: config, - } - - if err := ret.validate(); err != nil { - return Runner{}, errors.Wrap(err, "validating runner configuration") - } - - return ret, nil -} - -func (r *Runner) validate() error { - if r.DB == nil { - return ErrEmptyDB - } - if r.Clock == nil { - return ErrEmptyClock - } - if r.EmailTmpl == nil { - return ErrEmptyEmailTemplates - } - if r.EmailBackend == nil { - return ErrEmptyEmailBackend - } - if r.Config.WebURL == "" { - return ErrEmptyWebURL - } - - return nil -} - -func scheduleJob(c *cron.Cron, spec string, cmd func()) { - s, err := cron.ParseStandard(spec) - if err != nil { - panic(errors.Wrap(err, "parsing schedule")) - } - - c.Schedule(s, cron.FuncJob(cmd)) -} - -func (r *Runner) schedule(ch chan error) { - // Schedule jobs - cr := cron.New() - cr.Start() - - ch <- nil - - // Block forever - select {} -} - -// Do starts the background tasks in a separate goroutine that runs forever -func (r *Runner) Do() error { - // validate - if err := r.validate(); err != nil { - return errors.Wrap(err, "validating job configurations") - } - - ch := make(chan error) - go r.schedule(ch) - if err := <-ch; err != nil { - return errors.Wrap(err, "scheduling jobs") - } - - slog.Println("Started background tasks") - - return nil -} diff --git a/pkg/server/job/job_test.go b/pkg/server/job/job_test.go deleted file mode 100644 index 188dc2bc..00000000 --- a/pkg/server/job/job_test.go +++ /dev/null @@ -1,104 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package job - -import ( - "fmt" - "testing" - - "github.com/dnote/dnote/pkg/assert" - "github.com/dnote/dnote/pkg/clock" - "github.com/dnote/dnote/pkg/server/config" - "github.com/dnote/dnote/pkg/server/mailer" - "github.com/dnote/dnote/pkg/server/testutils" - "github.com/jinzhu/gorm" - "github.com/pkg/errors" -) - -func TestNewRunner(t *testing.T) { - testCases := []struct { - db *gorm.DB - clock clock.Clock - emailTmpl mailer.Templates - emailBackend mailer.Backend - webURL string - expectedErr error - }{ - { - db: &gorm.DB{}, - clock: clock.NewMock(), - emailTmpl: mailer.Templates{}, - emailBackend: &testutils.MockEmailbackendImplementation{}, - webURL: "http://mock.url", - expectedErr: nil, - }, - { - db: nil, - clock: clock.NewMock(), - emailTmpl: mailer.Templates{}, - emailBackend: &testutils.MockEmailbackendImplementation{}, - webURL: "http://mock.url", - expectedErr: ErrEmptyDB, - }, - { - db: &gorm.DB{}, - clock: nil, - emailTmpl: mailer.Templates{}, - emailBackend: &testutils.MockEmailbackendImplementation{}, - webURL: "http://mock.url", - expectedErr: ErrEmptyClock, - }, - { - db: &gorm.DB{}, - clock: clock.NewMock(), - emailTmpl: nil, - emailBackend: &testutils.MockEmailbackendImplementation{}, - webURL: "http://mock.url", - expectedErr: ErrEmptyEmailTemplates, - }, - { - db: &gorm.DB{}, - clock: clock.NewMock(), - emailTmpl: mailer.Templates{}, - emailBackend: nil, - webURL: "http://mock.url", - expectedErr: ErrEmptyEmailBackend, - }, - { - db: &gorm.DB{}, - clock: clock.NewMock(), - emailTmpl: mailer.Templates{}, - emailBackend: &testutils.MockEmailbackendImplementation{}, - webURL: "", - expectedErr: ErrEmptyWebURL, - }, - } - - for idx, tc := range testCases { - t.Run(fmt.Sprintf("test case %d", idx), func(t *testing.T) { - - c := config.Load() - c.WebURL = tc.webURL - - _, err := NewRunner(tc.db, tc.clock, tc.emailTmpl, tc.emailBackend, c) - - assert.Equal(t, errors.Cause(err), tc.expectedErr, "error mismatch") - }) - } -} diff --git a/pkg/server/log/log.go b/pkg/server/log/log.go index 06048cd0..172255ba 100644 --- a/pkg/server/log/log.go +++ b/pkg/server/log/log.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package log provides interfaces to write structured logs @@ -32,9 +29,19 @@ const ( fieldKeyTimestamp = "ts" fieldKeyUnixTimestamp = "ts_unix" - levelInfo = "info" - levelWarn = "warn" - levelError = "error" + // LevelDebug represents debug log level + LevelDebug = "debug" + // LevelInfo represents info log level + LevelInfo = "info" + // LevelWarn represents warn log level + LevelWarn = "warn" + // LevelError represents error log level + LevelError = "error" +) + +var ( + // currentLevel is the currently configured log level + currentLevel = LevelInfo ) // Fields represents a set of information to be included in the log @@ -58,19 +65,65 @@ func WithFields(fields Fields) Entry { return newEntry(fields) } +// SetLevel sets the global log level +func SetLevel(level string) { + currentLevel = level +} + +// GetLevel returns the current global log level +func GetLevel() string { + return currentLevel +} + +// shouldLog returns true if the given level should be logged based on currentLevel. +// +// Log level behavior (hierarchical): +// - LevelDebug: shows all messages (debug, info, warn, error) +// - LevelInfo: shows info, warn, and error messages +// - LevelWarn: shows warn and error messages +// - LevelError: shows only error messages +func shouldLog(level string) bool { + // Debug level shows everything + if currentLevel == LevelDebug { + return true + } + + // Info level shows info + warn + error + if currentLevel == LevelInfo { + return level == LevelInfo || level == LevelWarn || level == LevelError + } + + // Warn level shows warn + error + if currentLevel == LevelWarn { + return level == LevelWarn || level == LevelError + } + + // Error level shows only error + if currentLevel == LevelError { + return level == LevelError + } + + return false +} + +// Debug logs the given entry at a debug level +func (e Entry) Debug(msg string) { + e.write(LevelDebug, msg) +} + // Info logs the given entry at an info level func (e Entry) Info(msg string) { - e.write(levelInfo, msg) + e.write(LevelInfo, msg) } // Warn logs the given entry at a warning level func (e Entry) Warn(msg string) { - e.write(levelWarn, msg) + e.write(LevelWarn, msg) } // Error logs the given entry at an error level func (e Entry) Error(msg string) { - e.write(levelError, msg) + e.write(LevelError, msg) } // ErrorWrap logs the given entry with the error message annotated by the given message @@ -106,6 +159,10 @@ func (e Entry) formatJSON(level, msg string) []byte { } func (e Entry) write(level, msg string) { + if !shouldLog(level) { + return + } + serialized := e.formatJSON(level, msg) _, err := fmt.Fprintln(os.Stderr, string(serialized)) @@ -114,6 +171,11 @@ func (e Entry) write(level, msg string) { } } +// Debug logs a debug message without additional fields +func Debug(msg string) { + newEntry(Fields{}).Debug(msg) +} + // Info logs an info message without additional fields func Info(msg string) { newEntry(Fields{}).Info(msg) diff --git a/pkg/server/log/log_test.go b/pkg/server/log/log_test.go new file mode 100644 index 00000000..dd98c685 --- /dev/null +++ b/pkg/server/log/log_test.go @@ -0,0 +1,79 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package log + +import ( + "testing" +) + +func TestSetLevel(t *testing.T) { + // Reset to default after test + defer SetLevel(LevelInfo) + + SetLevel(LevelDebug) + if currentLevel != LevelDebug { + t.Errorf("Expected level %s, got %s", LevelDebug, currentLevel) + } + + SetLevel(LevelError) + if currentLevel != LevelError { + t.Errorf("Expected level %s, got %s", LevelError, currentLevel) + } +} + +func TestShouldLog(t *testing.T) { + // Reset to default after test + defer SetLevel(LevelInfo) + + testCases := []struct { + currentLevel string + logLevel string + expected bool + description string + }{ + // Debug level shows everything + {LevelDebug, LevelDebug, true, "debug level should show debug"}, + {LevelDebug, LevelInfo, true, "debug level should show info"}, + {LevelDebug, LevelWarn, true, "debug level should show warn"}, + {LevelDebug, LevelError, true, "debug level should show error"}, + + // Info level shows info + warn + error + {LevelInfo, LevelDebug, false, "info level should not show debug"}, + {LevelInfo, LevelInfo, true, "info level should show info"}, + {LevelInfo, LevelWarn, true, "info level should show warn"}, + {LevelInfo, LevelError, true, "info level should show error"}, + + // Warn level shows warn + error + {LevelWarn, LevelDebug, false, "warn level should not show debug"}, + {LevelWarn, LevelInfo, false, "warn level should not show info"}, + {LevelWarn, LevelWarn, true, "warn level should show warn"}, + {LevelWarn, LevelError, true, "warn level should show error"}, + + // Error level shows only error + {LevelError, LevelDebug, false, "error level should not show debug"}, + {LevelError, LevelInfo, false, "error level should not show info"}, + {LevelError, LevelWarn, false, "error level should not show warn"}, + {LevelError, LevelError, true, "error level should show error"}, + } + + for _, tc := range testCases { + SetLevel(tc.currentLevel) + result := shouldLog(tc.logLevel) + if result != tc.expected { + t.Errorf("%s: expected %v, got %v", tc.description, tc.expected, result) + } + } +} diff --git a/pkg/server/mailer/backend.go b/pkg/server/mailer/backend.go index f5d4f5ec..a09cf2e6 100644 --- a/pkg/server/mailer/backend.go +++ b/pkg/server/mailer/backend.go @@ -1,29 +1,25 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package mailer import ( - "fmt" - "log" "os" "strconv" + "github.com/dnote/dnote/pkg/server/log" "github.com/pkg/errors" "gopkg.in/gomail.v2" ) @@ -33,12 +29,25 @@ var ErrSMTPNotConfigured = errors.New("SMTP is not configured") // Backend is an interface for sending emails. type Backend interface { - Queue(subject, from string, to []string, contentType, body string) error + SendEmail(templateType, from string, to []string, data interface{}) error } -// SimpleBackendImplementation is an implementation of the Backend +// EmailDialer is an interface for sending email messages +type EmailDialer interface { + DialAndSend(m ...*gomail.Message) error +} + +// gomailDialer wraps gomail.Dialer to implement EmailDialer interface +type gomailDialer struct { + *gomail.Dialer +} + +// DefaultBackend is an implementation of the Backend // that sends an email without queueing. -type SimpleBackendImplementation struct { +// This backend is always enabled and will send emails via SMTP. +type DefaultBackend struct { + Dialer EmailDialer + Templates Templates } type dialerParams struct { @@ -73,31 +82,74 @@ func getSMTPParams() (*dialerParams, error) { return p, nil } -// Queue is an implementation of Backend.Queue. -func (b *SimpleBackendImplementation) Queue(subject, from string, to []string, contentType, body string) error { - // If not production, never actually send an email - if os.Getenv("GO_ENV") != "PRODUCTION" { - log.Println("Not sending email because Dnote is not running in a production environment.") - log.Printf("Subject: %s, to: %s, from: %s", subject, to, from) - fmt.Println(body) - return nil +// NewDefaultBackend creates a default backend +func NewDefaultBackend() (*DefaultBackend, error) { + p, err := getSMTPParams() + if err != nil { + return nil, err } + d := gomail.NewDialer(p.Host, p.Port, p.Username, p.Password) + + return &DefaultBackend{ + Dialer: &gomailDialer{Dialer: d}, + Templates: NewTemplates(), + }, nil +} + +// SendEmail is an implementation of Backend.SendEmail. +// It renders the template and sends the email immediately via SMTP. +func (b *DefaultBackend) SendEmail(templateType, from string, to []string, data interface{}) error { + subject, body, err := b.Templates.Execute(templateType, EmailKindText, data) + if err != nil { + return errors.Wrap(err, "executing template") + } + + return b.queue(subject, from, to, EmailKindText, body) +} + +// queue sends the email immediately via SMTP. +func (b *DefaultBackend) queue(subject, from string, to []string, contentType, body string) error { m := gomail.NewMessage() m.SetHeader("From", from) m.SetHeader("To", to...) m.SetHeader("Subject", subject) m.SetBody(contentType, body) - p, err := getSMTPParams() - if err != nil { - return errors.Wrap(err, "getting dialer params") - } - - d := gomail.NewPlainDialer(p.Host, p.Port, p.Username, p.Password) - if err := d.DialAndSend(m); err != nil { + if err := b.Dialer.DialAndSend(m); err != nil { return errors.Wrap(err, "dialing and sending email") } return nil } + +// StdoutBackend is an implementation of the Backend +// that prints emails to stdout instead of sending them. +// This is useful for development and testing. +type StdoutBackend struct { + Templates Templates +} + +// NewStdoutBackend creates a stdout backend +func NewStdoutBackend() *StdoutBackend { + return &StdoutBackend{ + Templates: NewTemplates(), + } +} + +// SendEmail is an implementation of Backend.SendEmail. +// It renders the template and logs the email to stdout instead of sending it. +func (b *StdoutBackend) SendEmail(templateType, from string, to []string, data interface{}) error { + subject, body, err := b.Templates.Execute(templateType, EmailKindText, data) + if err != nil { + return errors.Wrap(err, "executing template") + } + + log.WithFields(log.Fields{ + "subject": subject, + "to": to, + "from": from, + "body": body, + }).Info("Email (not sent, using StdoutBackend)") + return nil +} diff --git a/pkg/server/mailer/backend_test.go b/pkg/server/mailer/backend_test.go new file mode 100644 index 00000000..2388c16b --- /dev/null +++ b/pkg/server/mailer/backend_test.go @@ -0,0 +1,107 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mailer + +import ( + "testing" + + "gopkg.in/gomail.v2" +) + +type mockDialer struct { + sentMessages []*gomail.Message + err error +} + +func (m *mockDialer) DialAndSend(msgs ...*gomail.Message) error { + m.sentMessages = append(m.sentMessages, msgs...) + return m.err +} + +func TestDefaultBackendSendEmail(t *testing.T) { + t.Run("sends email", func(t *testing.T) { + mock := &mockDialer{} + backend := &DefaultBackend{ + Dialer: mock, + Templates: NewTemplates(), + } + + data := WelcomeTmplData{ + AccountEmail: "bob@example.com", + BaseURL: "https://example.com", + } + + err := backend.SendEmail(EmailTypeWelcome, "alice@example.com", []string{"bob@example.com"}, data) + if err != nil { + t.Fatalf("SendEmail failed: %v", err) + } + + if len(mock.sentMessages) != 1 { + t.Errorf("expected 1 message sent, got %d", len(mock.sentMessages)) + } + }) +} + +func TestNewDefaultBackend(t *testing.T) { + t.Run("with all env vars set", func(t *testing.T) { + t.Setenv("SmtpHost", "smtp.example.com") + t.Setenv("SmtpPort", "587") + t.Setenv("SmtpUsername", "user@example.com") + t.Setenv("SmtpPassword", "secret") + + backend, err := NewDefaultBackend() + if err != nil { + t.Fatalf("NewDefaultBackend failed: %v", err) + } + + if backend.Dialer == nil { + t.Error("expected Dialer to be set") + } + }) + + t.Run("missing SMTP config returns error", func(t *testing.T) { + t.Setenv("SmtpHost", "") + t.Setenv("SmtpPort", "") + t.Setenv("SmtpUsername", "") + t.Setenv("SmtpPassword", "") + + _, err := NewDefaultBackend() + if err == nil { + t.Error("expected error when SMTP not configured") + } + if err != ErrSMTPNotConfigured { + t.Errorf("expected ErrSMTPNotConfigured, got %v", err) + } + }) +} + +func TestStdoutBackendSendEmail(t *testing.T) { + t.Run("logs email without sending", func(t *testing.T) { + backend := NewStdoutBackend() + + data := WelcomeTmplData{ + AccountEmail: "bob@example.com", + BaseURL: "https://example.com", + } + + err := backend.SendEmail(EmailTypeWelcome, "alice@example.com", []string{"bob@example.com"}, data) + if err != nil { + t.Fatalf("SendEmail failed: %v", err) + } + + // StdoutBackend should never return an error, just log + }) +} diff --git a/pkg/server/mailer/mailer.go b/pkg/server/mailer/mailer.go index 5a660bbc..18887cf5 100644 --- a/pkg/server/mailer/mailer.go +++ b/pkg/server/mailer/mailer.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package mailer provides a functionality to send emails @@ -21,13 +18,11 @@ package mailer import ( "bytes" - "embed" "fmt" - htemplate "html/template" "io" ttemplate "text/template" - "github.com/aymerick/douceur/inliner" + "github.com/dnote/dnote/pkg/server/mailer/templates" "github.com/pkg/errors" ) @@ -36,34 +31,29 @@ var ( EmailTypeResetPassword = "reset_password" // EmailTypeResetPasswordAlert represents a password change notification email EmailTypeResetPasswordAlert = "reset_password_alert" - // EmailTypeEmailVerification represents an email verification email - EmailTypeEmailVerification = "verify_email" // EmailTypeWelcome represents an welcome email EmailTypeWelcome = "welcome" - // EmailTypeInactiveReminder represents an inactivity reminder email - EmailTypeInactiveReminder = "inactive" - // EmailTypeSubscriptionConfirmation represents an inactivity reminder email - EmailTypeSubscriptionConfirmation = "subscription_confirmation" ) var ( - // EmailKindHTML is the type of html email - EmailKindHTML = "text/html" // EmailKindText is the type of text email EmailKindText = "text/plain" ) -// template is the common interface shared between Template from +// tmpl is the common interface shared between Template from // html/template and text/template -type template interface { +type tmpl interface { Execute(wr io.Writer, data interface{}) error } -// Templates holds the parsed email templates -type Templates map[string]template +// template wraps a template with its subject line +type template struct { + tmpl tmpl + subject string +} -//go:embed templates/src -var templateDir embed.FS +// Templates holds the parsed email templates with their subjects +type Templates map[string]template func getTemplateKey(name, kind string) string { return fmt.Sprintf("%s.%s", name, kind) @@ -72,16 +62,19 @@ func getTemplateKey(name, kind string) string { func (tmpl Templates) get(name, kind string) (template, error) { key := getTemplateKey(name, kind) t := tmpl[key] - if t == nil { - return nil, errors.Errorf("unsupported template '%s' with type '%s'", name, kind) + if t.tmpl == nil { + return template{}, errors.Errorf("unsupported template '%s' with type '%s'", name, kind) } return t, nil } -func (tmpl Templates) set(name, kind string, t template) { +func (tmpl Templates) set(name, kind string, t tmpl, subject string) { key := getTemplateKey(name, kind) - tmpl[key] = t + tmpl[key] = template{ + tmpl: t, + subject: subject, + } } // NewTemplates initializes templates @@ -90,10 +83,6 @@ func NewTemplates() Templates { if err != nil { panic(errors.Wrap(err, "initializing welcome template")) } - verifyEmailText, err := initTextTmpl(EmailTypeEmailVerification) - if err != nil { - panic(errors.Wrap(err, "initializing email verification template")) - } passwordResetText, err := initTextTmpl(EmailTypeResetPassword) if err != nil { panic(errors.Wrap(err, "initializing password reset template")) @@ -102,63 +91,20 @@ func NewTemplates() Templates { if err != nil { panic(errors.Wrap(err, "initializing password reset template")) } - inactiveReminderText, err := initTextTmpl(EmailTypeInactiveReminder) - if err != nil { - panic(errors.Wrap(err, "initializing password reset template")) - } - subscriptionConfirmationText, err := initTextTmpl(EmailTypeSubscriptionConfirmation) - if err != nil { - panic(errors.Wrap(err, "initializing password reset template")) - } T := Templates{} - T.set(EmailTypeResetPassword, EmailKindText, passwordResetText) - T.set(EmailTypeResetPasswordAlert, EmailKindText, passwordResetAlertText) - T.set(EmailTypeEmailVerification, EmailKindText, verifyEmailText) - T.set(EmailTypeWelcome, EmailKindText, welcomeText) - T.set(EmailTypeInactiveReminder, EmailKindText, inactiveReminderText) - T.set(EmailTypeSubscriptionConfirmation, EmailKindText, subscriptionConfirmationText) + T.set(EmailTypeResetPassword, EmailKindText, passwordResetText, "Reset your Dnote password") + T.set(EmailTypeResetPasswordAlert, EmailKindText, passwordResetAlertText, "Your Dnote password was changed") + T.set(EmailTypeWelcome, EmailKindText, welcomeText, "Welcome to Dnote!") return T } -// initHTMLTmpl returns a template instance by parsing the template with the -// given name along with partials -func initHTMLTmpl(templateName string) (template, error) { - filename := fmt.Sprintf("templates/src/%s.html", templateName) - - content, err := templateDir.ReadFile(filename) - if err != nil { - return nil, errors.Wrap(err, "reading template") - } - headerContent, err := templateDir.ReadFile("templates/header.html") - if err != nil { - return nil, errors.Wrap(err, "reading header template") - } - footerContent, err := templateDir.ReadFile("templates/footer.html") - if err != nil { - return nil, errors.Wrap(err, "reading footer template") - } - - t := htemplate.New(templateName) - if _, err = t.Parse(string(content)); err != nil { - return nil, errors.Wrap(err, "parsing template") - } - if _, err = t.Parse(string(headerContent)); err != nil { - return nil, errors.Wrap(err, "parsing template") - } - if _, err = t.Parse(string(footerContent)); err != nil { - return nil, errors.Wrap(err, "parsing template") - } - - return t, nil -} - // initTextTmpl returns a template instance by parsing the template with the given name -func initTextTmpl(templateName string) (template, error) { - filename := fmt.Sprintf("templates/src/%s.txt", templateName) +func initTextTmpl(templateName string) (tmpl, error) { + filename := fmt.Sprintf("%s.txt", templateName) - content, err := templateDir.ReadFile(filename) + content, err := templates.Files.ReadFile(filename) if err != nil { return nil, errors.Wrap(err, "reading template") } @@ -171,27 +117,17 @@ func initTextTmpl(templateName string) (template, error) { return t, nil } -// Execute executes the template with the given name with the givn data -func (tmpl Templates) Execute(name, kind string, data interface{}) (string, error) { +// Execute executes the template and returns the subject, body, and any error +func (tmpl Templates) Execute(name, kind string, data any) (subject, body string, err error) { t, err := tmpl.get(name, kind) if err != nil { - return "", errors.Wrap(err, "getting template") + return "", "", errors.Wrap(err, "getting template") } buf := new(bytes.Buffer) - if err := t.Execute(buf, data); err != nil { - return "", errors.Wrap(err, "executing the template") + if err := t.tmpl.Execute(buf, data); err != nil { + return "", "", errors.Wrap(err, "executing the template") } - // If HTML email, inline the CSS rules - if kind == EmailKindHTML { - html, err := inliner.Inline(buf.String()) - if err != nil { - return "", errors.Wrap(err, "inlining the css rules") - } - - return html, nil - } - - return buf.String(), nil + return t.subject, buf.String(), nil } diff --git a/pkg/server/mailer/mailer_test.go b/pkg/server/mailer/mailer_test.go index a4521d6f..71b48209 100644 --- a/pkg/server/mailer/mailer_test.go +++ b/pkg/server/mailer/mailer_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package mailer @@ -26,39 +23,20 @@ import ( "github.com/pkg/errors" ) -func TestEmailVerificationEmail(t *testing.T) { - testCases := []struct { - token string - webURL string - }{ - { - token: "someRandomToken1", - webURL: "http://localhost:3000", - }, - { - token: "someRandomToken2", - webURL: "http://localhost:3001", - }, - } - +func TestAllTemplatesInitialized(t *testing.T) { tmpl := NewTemplates() - for _, tc := range testCases { - t.Run(fmt.Sprintf("with WebURL %s", tc.webURL), func(t *testing.T) { - dat := EmailVerificationTmplData{ - Token: tc.token, - WebURL: tc.webURL, - } - body, err := tmpl.Execute(EmailTypeEmailVerification, EmailKindText, dat) - if err != nil { - t.Fatal(errors.Wrap(err, "executing")) - } + emailTypes := []string{ + EmailTypeResetPassword, + EmailTypeResetPasswordAlert, + EmailTypeWelcome, + } - if ok := strings.Contains(body, tc.webURL); !ok { - t.Errorf("email body did not contain %s", tc.webURL) - } - if ok := strings.Contains(body, tc.token); !ok { - t.Errorf("email body did not contain %s", tc.token) + for _, emailType := range emailTypes { + t.Run(emailType, func(t *testing.T) { + _, err := tmpl.get(emailType, EmailKindText) + if err != nil { + t.Errorf("template %s not initialized: %v", emailType, err) } }) } @@ -66,34 +44,37 @@ func TestEmailVerificationEmail(t *testing.T) { func TestResetPasswordEmail(t *testing.T) { testCases := []struct { - token string - webURL string + token string + baseURL string }{ { - token: "someRandomToken1", - webURL: "http://localhost:3000", + token: "someRandomToken1", + baseURL: "http://localhost:3000", }, { - token: "someRandomToken2", - webURL: "http://localhost:3001", + token: "someRandomToken2", + baseURL: "http://localhost:3001", }, } tmpl := NewTemplates() for _, tc := range testCases { - t.Run(fmt.Sprintf("with WebURL %s", tc.webURL), func(t *testing.T) { + t.Run(fmt.Sprintf("with BaseURL %s", tc.baseURL), func(t *testing.T) { dat := EmailResetPasswordTmplData{ - Token: tc.token, - WebURL: tc.webURL, + Token: tc.token, + BaseURL: tc.baseURL, } - body, err := tmpl.Execute(EmailTypeResetPassword, EmailKindText, dat) + subject, body, err := tmpl.Execute(EmailTypeResetPassword, EmailKindText, dat) if err != nil { t.Fatal(errors.Wrap(err, "executing")) } - if ok := strings.Contains(body, tc.webURL); !ok { - t.Errorf("email body did not contain %s", tc.webURL) + if subject != "Reset your Dnote password" { + t.Errorf("expected subject 'Reset your Dnote password', got '%s'", subject) + } + if ok := strings.Contains(body, tc.baseURL); !ok { + t.Errorf("email body did not contain %s", tc.baseURL) } if ok := strings.Contains(body, tc.token); !ok { t.Errorf("email body did not contain %s", tc.token) @@ -101,3 +82,85 @@ func TestResetPasswordEmail(t *testing.T) { }) } } + +func TestWelcomeEmail(t *testing.T) { + testCases := []struct { + accountEmail string + baseURL string + }{ + { + accountEmail: "test@example.com", + baseURL: "http://localhost:3000", + }, + { + accountEmail: "user@example.org", + baseURL: "http://localhost:3001", + }, + } + + tmpl := NewTemplates() + + for _, tc := range testCases { + t.Run(fmt.Sprintf("with BaseURL %s and email %s", tc.baseURL, tc.accountEmail), func(t *testing.T) { + dat := WelcomeTmplData{ + AccountEmail: tc.accountEmail, + BaseURL: tc.baseURL, + } + subject, body, err := tmpl.Execute(EmailTypeWelcome, EmailKindText, dat) + if err != nil { + t.Fatal(errors.Wrap(err, "executing")) + } + + if subject != "Welcome to Dnote!" { + t.Errorf("expected subject 'Welcome to Dnote!', got '%s'", subject) + } + if ok := strings.Contains(body, tc.baseURL); !ok { + t.Errorf("email body did not contain %s", tc.baseURL) + } + if ok := strings.Contains(body, tc.accountEmail); !ok { + t.Errorf("email body did not contain %s", tc.accountEmail) + } + }) + } +} + +func TestResetPasswordAlertEmail(t *testing.T) { + testCases := []struct { + accountEmail string + baseURL string + }{ + { + accountEmail: "test@example.com", + baseURL: "http://localhost:3000", + }, + { + accountEmail: "user@example.org", + baseURL: "http://localhost:3001", + }, + } + + tmpl := NewTemplates() + + for _, tc := range testCases { + t.Run(fmt.Sprintf("with BaseURL %s and email %s", tc.baseURL, tc.accountEmail), func(t *testing.T) { + dat := EmailResetPasswordAlertTmplData{ + AccountEmail: tc.accountEmail, + BaseURL: tc.baseURL, + } + subject, body, err := tmpl.Execute(EmailTypeResetPasswordAlert, EmailKindText, dat) + if err != nil { + t.Fatal(errors.Wrap(err, "executing")) + } + + if subject != "Your Dnote password was changed" { + t.Errorf("expected subject 'Your Dnote password was changed', got '%s'", subject) + } + if ok := strings.Contains(body, tc.baseURL); !ok { + t.Errorf("email body did not contain %s", tc.baseURL) + } + if ok := strings.Contains(body, tc.accountEmail); !ok { + t.Errorf("email body did not contain %s", tc.accountEmail) + } + }) + } +} diff --git a/pkg/server/mailer/templates/.env.dev b/pkg/server/mailer/templates/.env.dev deleted file mode 100644 index 7808cb4a..00000000 --- a/pkg/server/mailer/templates/.env.dev +++ /dev/null @@ -1,12 +0,0 @@ -DBHost=localhost -DBPort=5433 -DBName=dnote -DBUser=postgres -DBPassword= - -SmtpUsername=mock-SmtpUsername -SmtpPassword=mock-SmtpPassword -SmtpHost=mock-SmtpHost - -WebURL=http://localhost:3000 -DisableRegistration=false diff --git a/pkg/server/mailer/templates/.gitignore b/pkg/server/mailer/templates/.gitignore deleted file mode 100644 index f8a26871..00000000 --- a/pkg/server/mailer/templates/.gitignore +++ /dev/null @@ -1 +0,0 @@ -templates diff --git a/pkg/server/mailer/templates/README.md b/pkg/server/mailer/templates/README.md deleted file mode 100644 index 9329442a..00000000 --- a/pkg/server/mailer/templates/README.md +++ /dev/null @@ -1,13 +0,0 @@ -# templates - -Email templates - -* `/src` contains templates. - -## Development - -Run the server to develop templates locally. - -``` -./dev.sh -``` diff --git a/pkg/server/mailer/templates/dev.sh b/pkg/server/mailer/templates/dev.sh deleted file mode 100755 index 035220b1..00000000 --- a/pkg/server/mailer/templates/dev.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env bash -set -eux - -PID="" - -function cleanup { - if [ "$PID" != "" ]; then - kill "$PID" - fi -} -trap cleanup EXIT - -while true; do - go build main.go - ./main & - PID=$! - inotifywait -r -e modify . - kill $PID -done - - diff --git a/pkg/server/mailer/templates/main.go b/pkg/server/mailer/templates/main.go deleted file mode 100644 index d22c964e..00000000 --- a/pkg/server/mailer/templates/main.go +++ /dev/null @@ -1,139 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package main - -import ( - "log" - "net/http" - - "github.com/dnote/dnote/pkg/server/config" - "github.com/dnote/dnote/pkg/server/database" - "github.com/dnote/dnote/pkg/server/mailer" - "github.com/jinzhu/gorm" - "github.com/joho/godotenv" - _ "github.com/lib/pq" -) - -func (c Context) passwordResetHandler(w http.ResponseWriter, r *http.Request) { - data := mailer.EmailResetPasswordTmplData{ - AccountEmail: "alice@example.com", - Token: "testToken", - WebURL: "http://localhost:3000", - } - body, err := c.Tmpl.Execute(mailer.EmailTypeResetPassword, mailer.EmailKindText, data) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.Write([]byte(body)) -} - -func (c Context) passwordResetAlertHandler(w http.ResponseWriter, r *http.Request) { - data := mailer.EmailResetPasswordAlertTmplData{ - AccountEmail: "alice@example.com", - WebURL: "http://localhost:3000", - } - body, err := c.Tmpl.Execute(mailer.EmailTypeResetPasswordAlert, mailer.EmailKindText, data) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.Write([]byte(body)) -} - -func (c Context) emailVerificationHandler(w http.ResponseWriter, r *http.Request) { - data := mailer.EmailVerificationTmplData{ - Token: "testToken", - WebURL: "http://localhost:3000", - } - body, err := c.Tmpl.Execute(mailer.EmailTypeEmailVerification, mailer.EmailKindText, data) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.Write([]byte(body)) -} - -func (c Context) welcomeHandler(w http.ResponseWriter, r *http.Request) { - data := mailer.WelcomeTmplData{ - AccountEmail: "alice@example.com", - WebURL: "http://localhost:3000", - } - body, err := c.Tmpl.Execute(mailer.EmailTypeWelcome, mailer.EmailKindText, data) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.Write([]byte(body)) -} - -func (c Context) inactiveHandler(w http.ResponseWriter, r *http.Request) { - data := mailer.InactiveReminderTmplData{ - SampleNoteUUID: "some-uuid", - WebURL: "http://localhost:3000", - Token: "some-random-token", - } - body, err := c.Tmpl.Execute(mailer.EmailTypeInactiveReminder, mailer.EmailKindText, data) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - w.Write([]byte(body)) -} - -func (c Context) homeHandler(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("Email development server is running.")) -} - -func init() { - err := godotenv.Load(".env.dev") - if err != nil { - panic(err) - } -} - -// Context is a context holding global information -type Context struct { - DB *gorm.DB - Tmpl mailer.Templates -} - -func main() { - c := config.Load() - db := database.Open(c) - defer db.Close() - - log.Println("Email template development server running on http://127.0.0.1:2300") - - tmpl := mailer.NewTemplates() - ctx := Context{DB: db, Tmpl: tmpl} - - http.HandleFunc("/", ctx.homeHandler) - http.HandleFunc("/email-verification", ctx.emailVerificationHandler) - http.HandleFunc("/password-reset", ctx.passwordResetHandler) - http.HandleFunc("/password-reset-alert", ctx.passwordResetAlertHandler) - http.HandleFunc("/welcome", ctx.welcomeHandler) - http.HandleFunc("/inactive-reminder", ctx.inactiveHandler) - log.Fatal(http.ListenAndServe(":2300", nil)) -} diff --git a/pkg/server/mailer/templates/reset_password.txt b/pkg/server/mailer/templates/reset_password.txt new file mode 100644 index 00000000..9d31d605 --- /dev/null +++ b/pkg/server/mailer/templates/reset_password.txt @@ -0,0 +1,5 @@ +You are receiving this because you requested to reset the password of the '{{ .AccountEmail }}' Dnote account. + +Please click on the following link, or paste this into your browser to complete the process: + + {{ .BaseURL }}/password-reset/{{ .Token }} diff --git a/pkg/server/mailer/templates/src/reset_password_alert.txt b/pkg/server/mailer/templates/reset_password_alert.txt similarity index 50% rename from pkg/server/mailer/templates/src/reset_password_alert.txt rename to pkg/server/mailer/templates/reset_password_alert.txt index 3aa9bdd6..ea67dce3 100644 --- a/pkg/server/mailer/templates/src/reset_password_alert.txt +++ b/pkg/server/mailer/templates/reset_password_alert.txt @@ -2,7 +2,7 @@ Hi, This email is to notify you that the password for your Dnote account "{{ .AccountEmail }}" has changed. -If you did not initiate this password change, please notify us by replying, and reset your password at {{ .WebURL }}/password-reset +If you did not initiate this password change, reset your password at {{ .BaseURL }}/password-reset. Thanks. diff --git a/pkg/server/mailer/templates/scripts/run.sh b/pkg/server/mailer/templates/scripts/run.sh deleted file mode 100755 index fd8e8ac5..00000000 --- a/pkg/server/mailer/templates/scripts/run.sh +++ /dev/null @@ -1 +0,0 @@ -CompileDaemon -directory=. -command="./templates" -include="*.html" diff --git a/pkg/server/mailer/templates/src/inactive.txt b/pkg/server/mailer/templates/src/inactive.txt deleted file mode 100644 index b6f4d508..00000000 --- a/pkg/server/mailer/templates/src/inactive.txt +++ /dev/null @@ -1,9 +0,0 @@ -Hi, nothing has been added to your Dnote for some time. - -What about revisiting one of your previous notes? {{ .WebURL }}/notes/{{ .SampleNoteUUID }} - -You can add new notes at {{ .WebURL }}/new or using Dnote apps. - -- Dnote team - -UNSUBSCRIBE: {{ .WebURL }}/settings/notifications?token={{ .Token }} diff --git a/pkg/server/mailer/templates/src/reset_password.txt b/pkg/server/mailer/templates/src/reset_password.txt deleted file mode 100644 index 3bc34850..00000000 --- a/pkg/server/mailer/templates/src/reset_password.txt +++ /dev/null @@ -1,9 +0,0 @@ -You are receiving this because you (or someone else) requested to reset the password of the '{{ .AccountEmail }}' Dnote account. - -Please click on the following link, or paste this into your browser to complete the process: - - {{ .WebURL }}/password-reset/{{ .Token }} - -You can reply to this message, if you have questions. - -- Dnote team diff --git a/pkg/server/mailer/templates/src/subscription_confirmation.txt b/pkg/server/mailer/templates/src/subscription_confirmation.txt deleted file mode 100644 index e212211f..00000000 --- a/pkg/server/mailer/templates/src/subscription_confirmation.txt +++ /dev/null @@ -1,12 +0,0 @@ -Hi, thanks for signing up for Dnote Pro. - -Now you can take your notes with you wherever you go! - -* Synchronize data among an unlimited number of machines. -* Manage notes via REST API. - -Your account is "{{ .AccountEmail }}". Log in at {{ .WebURL }}/login - -Thank you for using Dnote. Your support makes it possible to develop it for developers around the world. - -- Dnote team diff --git a/pkg/server/mailer/templates/src/verify_email.txt b/pkg/server/mailer/templates/src/verify_email.txt deleted file mode 100644 index a85ab705..00000000 --- a/pkg/server/mailer/templates/src/verify_email.txt +++ /dev/null @@ -1,9 +0,0 @@ -Hi. - -Welcome to Dnote! To verify your email, visit the following link: - - {{ .WebURL }}/verify-email/{{ .Token }} - -Thanks for using Dnote. - -- Dnote team diff --git a/pkg/server/mailer/templates/src/welcome.txt b/pkg/server/mailer/templates/src/welcome.txt deleted file mode 100644 index 7a33207a..00000000 --- a/pkg/server/mailer/templates/src/welcome.txt +++ /dev/null @@ -1,16 +0,0 @@ -Hi, welcome to Dnote. - -Dnote is a simple command-line notebook. - -YOUR ACCOUNT - -Your {{ .WebURL }} account is "{{ .AccountEmail }}". Log in at {{ .WebURL }}/login -If you ever forget your password, you can reset it at {{ .WebURL }}/password-reset - -SOURCE CODE - -Dnote is open source and you can see the source code at https://github.com/dnote/dnote - -Feel free to reply anytime. Thanks for using Dnote. - -- Dnote team diff --git a/pkg/server/mailer/templates/templates.go b/pkg/server/mailer/templates/templates.go new file mode 100644 index 00000000..78fedd07 --- /dev/null +++ b/pkg/server/mailer/templates/templates.go @@ -0,0 +1,22 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package mailer provides a functionality to send emails +package templates + +import "embed" + +//go:embed *.txt +var Files embed.FS diff --git a/pkg/server/mailer/templates/welcome.txt b/pkg/server/mailer/templates/welcome.txt new file mode 100644 index 00000000..cced536c --- /dev/null +++ b/pkg/server/mailer/templates/welcome.txt @@ -0,0 +1,12 @@ +Hi, welcome to Dnote. + +Dnote is a simple command-line notebook. + +YOUR ACCOUNT + +Your {{ .BaseURL }} account is "{{ .AccountEmail }}". Log in at {{ .BaseURL }}/login +If you ever forget your password, you can reset it at {{ .BaseURL }}/password-reset + +SOURCE CODE + +Dnote is open source and you can see the source code at https://github.com/dnote/dnote diff --git a/pkg/server/mailer/tokens.go b/pkg/server/mailer/tokens.go index 6452f958..669c4820 100644 --- a/pkg/server/mailer/tokens.go +++ b/pkg/server/mailer/tokens.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package mailer @@ -23,15 +20,14 @@ import ( "encoding/base64" "github.com/dnote/dnote/pkg/server/database" - "github.com/jinzhu/gorm" "github.com/pkg/errors" + "gorm.io/gorm" ) func generateRandomToken(bits int) (string, error) { b := make([]byte, bits) - _, err := rand.Read(b) - if err != nil { + if _, err := rand.Read(b); err != nil { return "", errors.Wrap(err, "generating random bytes") } @@ -42,16 +38,16 @@ func generateRandomToken(bits int) (string, error) { // by first looking up any unused record and creating one if none exists. func GetToken(db *gorm.DB, userID int, kind string) (database.Token, error) { var tok database.Token - conn := db. + err := db. Where("user_id = ? AND type =? AND used_at IS NULL", userID, kind). - First(&tok) + First(&tok).Error - tokenVal, err := generateRandomToken(16) - if err != nil { - return tok, errors.Wrap(err, "generating token value") + tokenVal, genErr := generateRandomToken(16) + if genErr != nil { + return tok, errors.Wrap(genErr, "generating token value") } - if conn.RecordNotFound() { + if errors.Is(err, gorm.ErrRecordNotFound) { tok = database.Token{ UserID: userID, Type: kind, @@ -62,7 +58,7 @@ func GetToken(db *gorm.DB, userID int, kind string) (database.Token, error) { } return tok, nil - } else if err := conn.Error; err != nil { + } else if err != nil { return tok, errors.Wrap(err, "finding token") } diff --git a/pkg/server/mailer/tokens_test.go b/pkg/server/mailer/tokens_test.go new file mode 100644 index 00000000..4f4fa73d --- /dev/null +++ b/pkg/server/mailer/tokens_test.go @@ -0,0 +1,80 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mailer + +import ( + "testing" + + "github.com/dnote/dnote/pkg/server/database" + "github.com/dnote/dnote/pkg/server/testutils" +) + +func TestGetToken(t *testing.T) { + db := testutils.InitMemoryDB(t) + + userID := 1 + tokenType := "email_verification" + + t.Run("creates new token", func(t *testing.T) { + token, err := GetToken(db, userID, tokenType) + if err != nil { + t.Fatalf("GetToken failed: %v", err) + } + + if token.UserID != userID { + t.Errorf("expected UserID %d, got %d", userID, token.UserID) + } + if token.Type != tokenType { + t.Errorf("expected Type %s, got %s", tokenType, token.Type) + } + if token.Value == "" { + t.Error("expected non-empty token Value") + } + if token.UsedAt != nil { + t.Error("expected UsedAt to be nil for new token") + } + }) + + t.Run("reuses unused token", func(t *testing.T) { + // Get token again - should return the same one + token2, err := GetToken(db, userID, tokenType) + if err != nil { + t.Fatalf("second GetToken failed: %v", err) + } + + // Get first token to compare + var token1 database.Token + if err := db.Where("user_id = ? AND type = ?", userID, tokenType).First(&token1).Error; err != nil { + t.Fatalf("failed to get first token: %v", err) + } + + if token1.ID != token2.ID { + t.Errorf("expected same token ID %d, got %d", token1.ID, token2.ID) + } + if token1.Value != token2.Value { + t.Errorf("expected same token Value %s, got %s", token1.Value, token2.Value) + } + + // Verify only one token exists in database + var count int64 + if err := db.Model(&database.Token{}).Where("user_id = ? AND type = ?", userID, tokenType).Count(&count).Error; err != nil { + t.Fatalf("failed to count tokens: %v", err) + } + if count != 1 { + t.Errorf("expected 1 token in database, got %d", count) + } + }) +} diff --git a/pkg/server/mailer/types.go b/pkg/server/mailer/types.go index ff2e205f..40468b84 100644 --- a/pkg/server/mailer/types.go +++ b/pkg/server/mailer/types.go @@ -1,57 +1,35 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package mailer -// EmailVerificationTmplData is a template data for email verification emails -type EmailVerificationTmplData struct { - Token string - WebURL string -} - // EmailResetPasswordTmplData is a template data for reset password emails type EmailResetPasswordTmplData struct { AccountEmail string Token string - WebURL string + BaseURL string } // EmailResetPasswordAlertTmplData is a template data for reset password emails type EmailResetPasswordAlertTmplData struct { AccountEmail string - WebURL string + BaseURL string } // WelcomeTmplData is a template data for welcome emails type WelcomeTmplData struct { AccountEmail string - WebURL string -} - -// InactiveReminderTmplData is a template data for welcome emails -type InactiveReminderTmplData struct { - SampleNoteUUID string - WebURL string - Token string -} - -// EmailTypeSubscriptionConfirmationTmplData is a template data for reset password emails -type EmailTypeSubscriptionConfirmationTmplData struct { - AccountEmail string - WebURL string + BaseURL string } diff --git a/pkg/server/main.go b/pkg/server/main.go index b59d203a..737cc7a6 100644 --- a/pkg/server/main.go +++ b/pkg/server/main.go @@ -1,137 +1,24 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package main import ( - "flag" - "fmt" - "log" - "net/http" - - "github.com/dnote/dnote/pkg/clock" - "github.com/dnote/dnote/pkg/server/app" - "github.com/dnote/dnote/pkg/server/buildinfo" - "github.com/dnote/dnote/pkg/server/config" - "github.com/dnote/dnote/pkg/server/controllers" - "github.com/dnote/dnote/pkg/server/database" - "github.com/dnote/dnote/pkg/server/job" - "github.com/dnote/dnote/pkg/server/mailer" - "github.com/jinzhu/gorm" - - "github.com/pkg/errors" + "github.com/dnote/dnote/pkg/server/cmd" ) -var port = flag.String("port", "3000", "port to connect to") - -func initDB(c config.Config) *gorm.DB { - db, err := gorm.Open("postgres", c.DB.GetConnectionStr()) - if err != nil { - panic(errors.Wrap(err, "opening database connection")) - } - database.InitSchema(db) - - return db -} - -func initApp(cfg config.Config) app.App { - db := initDB(cfg) - - return app.App{ - DB: db, - Clock: clock.New(), - EmailTemplates: mailer.NewTemplates(), - EmailBackend: &mailer.SimpleBackendImplementation{}, - Config: cfg, - HTTP500Page: cfg.HTTP500Page, - } -} - -func runJob(a app.App) error { - runner, err := job.NewRunner(a.DB, a.Clock, a.EmailTemplates, a.EmailBackend, a.Config) - if err != nil { - return errors.Wrap(err, "getting a job runner") - } - if err := runner.Do(); err != nil { - return errors.Wrap(err, "running job") - } - - return nil -} - -func startCmd() { - cfg := config.Load() - cfg.SetAssetBaseURL("/static") - - app := initApp(cfg) - defer app.DB.Close() - - if err := database.Migrate(app.DB); err != nil { - panic(errors.Wrap(err, "running migrations")) - } - if err := runJob(app); err != nil { - panic(errors.Wrap(err, "running job")) - } - - ctl := controllers.New(&app) - rc := controllers.RouteConfig{ - WebRoutes: controllers.NewWebRoutes(&app, ctl), - APIRoutes: controllers.NewAPIRoutes(&app, ctl), - Controllers: ctl, - } - - r, err := controllers.NewRouter(&app, rc) - if err != nil { - panic(errors.Wrap(err, "initializing router")) - } - - log.Printf("Dnote version %s is running on port %s", buildinfo.Version, *port) - log.Fatalln(http.ListenAndServe(fmt.Sprintf(":%s", *port), r)) -} - -func versionCmd() { - fmt.Printf("dnote-server-%s\n", buildinfo.Version) -} - -func rootCmd() { - fmt.Printf(`Dnote server - a simple personal knowledge base - -Usage: - dnote-server [command] - -Available commands: - start: Start the server - version: Print the version -`) -} - func main() { - flag.Parse() - cmd := flag.Arg(0) - - switch cmd { - case "": - rootCmd() - case "start": - startCmd() - case "version": - versionCmd() - default: - fmt.Printf("Unknown command %s", cmd) - } + cmd.Execute() } diff --git a/pkg/server/middleware/auth.go b/pkg/server/middleware/auth.go index 115aa8cd..9383d33d 100644 --- a/pkg/server/middleware/auth.go +++ b/pkg/server/middleware/auth.go @@ -1,39 +1,35 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package middleware import ( + "errors" "net/http" "net/url" - "strings" "time" - "github.com/dnote/dnote/pkg/server/app" "github.com/dnote/dnote/pkg/server/context" "github.com/dnote/dnote/pkg/server/database" "github.com/dnote/dnote/pkg/server/helpers" "github.com/dnote/dnote/pkg/server/log" - "github.com/jinzhu/gorm" - "github.com/pkg/errors" + pkgErrors "github.com/pkg/errors" + "gorm.io/gorm" ) -func authWithToken(db *gorm.DB, r *http.Request, tokenType string, p *AuthParams) (database.User, database.Token, bool, error) { +func authWithToken(db *gorm.DB, r *http.Request, tokenType string) (database.User, database.Token, bool, error) { var user database.User var token database.Token @@ -43,11 +39,11 @@ func authWithToken(db *gorm.DB, r *http.Request, tokenType string, p *AuthParams return user, token, false, nil } - conn := db.Where("value = ? AND type = ?", tokenValue, tokenType).First(&token) - if conn.RecordNotFound() { + err := db.Where("value = ? AND type = ?", tokenValue, tokenType).First(&token).Error + if errors.Is(err, gorm.ErrRecordNotFound) { return user, token, false, nil - } else if err := conn.Error; err != nil { - return user, token, false, errors.Wrap(err, "finding token") + } else if err != nil { + return user, token, false, pkgErrors.Wrap(err, "finding token") } if token.UsedAt != nil && time.Since(*token.UsedAt).Minutes() > 10 { @@ -55,38 +51,21 @@ func authWithToken(db *gorm.DB, r *http.Request, tokenType string, p *AuthParams } if err := db.Where("id = ?", token.UserID).First(&user).Error; err != nil { - return user, token, false, errors.Wrap(err, "finding user") + return user, token, false, pkgErrors.Wrap(err, "finding user") } return user, token, true, nil } -// Cors allows browser extensions to load resources -func Cors(next http.HandlerFunc) http.HandlerFunc { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - origin := r.Header.Get("Origin") - - // Allow browser extensions - if strings.HasPrefix(origin, "moz-extension://") || strings.HasPrefix(origin, "chrome-extension://") { - w.Header().Set("Access-Control-Allow-Origin", origin) - } - - next.ServeHTTP(w, r) - }) -} - // AuthParams is the params for the authentication middleware type AuthParams struct { - ProOnly bool RedirectGuestsToLogin bool } // Auth is an authentication middleware -func Auth(a *app.App, next http.HandlerFunc, p *AuthParams) http.HandlerFunc { - next = WithAccount(a, next) - +func Auth(db *gorm.DB, next http.HandlerFunc, p *AuthParams) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - user, ok, err := AuthWithSession(a.DB, r) + user, ok, err := AuthWithSession(db, r) if !ok { if p != nil && p.RedirectGuestsToLogin { @@ -106,39 +85,15 @@ func Auth(a *app.App, next http.HandlerFunc, p *AuthParams) http.HandlerFunc { return } - if p != nil && p.ProOnly { - if !user.Cloud { - RespondForbidden(w) - return - } - } - ctx := context.WithUser(r.Context(), &user) next.ServeHTTP(w, r.WithContext(ctx)) }) - -} - -func WithAccount(a *app.App, next http.HandlerFunc) http.HandlerFunc { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - user := context.User(r.Context()) - - var account database.Account - if err := a.DB.Where("user_id = ?", user.ID).First(&account).Error; err != nil { - DoError(w, "finding account", err, http.StatusInternalServerError) - return - } - - ctx := context.WithAccount(r.Context(), &account) - - next.ServeHTTP(w, r.WithContext(ctx)) - }) } // TokenAuth is an authentication middleware with token -func TokenAuth(a *app.App, next http.HandlerFunc, tokenType string, p *AuthParams) http.HandlerFunc { +func TokenAuth(db *gorm.DB, next http.HandlerFunc, tokenType string, p *AuthParams) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - user, token, ok, err := authWithToken(a.DB, r, tokenType, p) + user, token, ok, err := authWithToken(db, r, tokenType) if err != nil { // log the error and continue log.ErrorWrap(err, "authenticating with token") @@ -150,7 +105,7 @@ func TokenAuth(a *app.App, next http.HandlerFunc, tokenType string, p *AuthParam ctx = context.WithToken(ctx, &token) } else { // If token-based auth fails, fall back to session-based auth - user, ok, err = AuthWithSession(a.DB, r) + user, ok, err = AuthWithSession(db, r) if err != nil { DoError(w, "authenticating with session", err, http.StatusInternalServerError) return @@ -162,13 +117,6 @@ func TokenAuth(a *app.App, next http.HandlerFunc, tokenType string, p *AuthParam } } - if p != nil && p.ProOnly { - if !user.Cloud { - RespondForbidden(w) - return - } - } - ctx = context.WithUser(ctx, &user) next.ServeHTTP(w, r.WithContext(ctx)) }) @@ -180,39 +128,39 @@ func AuthWithSession(db *gorm.DB, r *http.Request) (database.User, bool, error) sessionKey, err := GetCredential(r) if err != nil { - return user, false, errors.Wrap(err, "getting credential") + return user, false, pkgErrors.Wrap(err, "getting credential") } if sessionKey == "" { return user, false, nil } var session database.Session - conn := db.Where("key = ?", sessionKey).First(&session) + err = db.Where("key = ?", sessionKey).First(&session).Error - if conn.RecordNotFound() { + if errors.Is(err, gorm.ErrRecordNotFound) { return user, false, nil - } else if err := conn.Error; err != nil { - return user, false, errors.Wrap(err, "finding session") + } else if err != nil { + return user, false, pkgErrors.Wrap(err, "finding session") } if session.ExpiresAt.Before(time.Now()) { return user, false, nil } - conn = db.Where("id = ?", session.UserID).First(&user) + err = db.Where("id = ?", session.UserID).First(&user).Error - if conn.RecordNotFound() { + if errors.Is(err, gorm.ErrRecordNotFound) { return user, false, nil - } else if err := conn.Error; err != nil { - return user, false, errors.Wrap(err, "finding user from token") + } else if err != nil { + return user, false, pkgErrors.Wrap(err, "finding user from token") } return user, true, nil } -func GuestOnly(a *app.App, next http.HandlerFunc) http.HandlerFunc { +func GuestOnly(db *gorm.DB, next http.HandlerFunc) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - _, ok, err := AuthWithSession(a.DB, r) + _, ok, err := AuthWithSession(db, r) if err != nil { // log the error and continue log.ErrorWrap(err, "authenticating with session") diff --git a/pkg/server/middleware/auth_test.go b/pkg/server/middleware/auth_test.go new file mode 100644 index 00000000..c6d0ead4 --- /dev/null +++ b/pkg/server/middleware/auth_test.go @@ -0,0 +1,251 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package middleware + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/server/database" + "github.com/dnote/dnote/pkg/server/testutils" +) + +func TestGuestOnly(t *testing.T) { + db := testutils.InitMemoryDB(t) + + handler := func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + } + + server := httptest.NewServer(GuestOnly(db, handler)) + defer server.Close() + + t.Run("guest", func(t *testing.T) { + req := testutils.MakeReq(server.URL, "GET", "/", "") + res := testutils.HTTPDo(t, req) + + assert.Equal(t, res.StatusCode, http.StatusOK, "status code mismatch") + }) + + t.Run("logged in", func(t *testing.T) { + user := testutils.SetupUserData(db, "user@test.com", "password123") + req := testutils.MakeReq(server.URL, "GET", "/", "") + res := testutils.HTTPAuthDo(t, db, req, user) + + assert.Equal(t, res.StatusCode, http.StatusFound, "status code mismatch") + assert.Equal(t, res.Header.Get("Location"), "/", "location mismatch") + }) + + t.Run("error getting credential", func(t *testing.T) { + req := testutils.MakeReq(server.URL, "GET", "/", "") + req.Header.Set("Authorization", "InvalidFormat") + res := testutils.HTTPDo(t, req) + + assert.Equal(t, res.StatusCode, http.StatusOK, "status code mismatch") + }) +} + +func TestAuth(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@test.com", "pass1234") + + session := database.Session{ + Key: "A9xgggqzTHETy++GDi1NpDNe0iyqosPm9bitdeNGkJU=", + UserID: user.ID, + ExpiresAt: time.Now().Add(time.Hour * 24), + } + testutils.MustExec(t, db.Save(&session), "preparing session") + expiredSession := database.Session{ + Key: "Vvgm3eBXfXGEFWERI7faiRJ3DAzJw+7DdT9J1LEyNfI=", + UserID: user.ID, + ExpiresAt: time.Now().Add(-time.Hour * 24), + } + testutils.MustExec(t, db.Save(&expiredSession), "preparing expired session") + + handler := func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + } + + t.Run("valid session with header", func(t *testing.T) { + server := httptest.NewServer(Auth(db, handler, nil)) + defer server.Close() + + req := testutils.MakeReq(server.URL, "GET", "/", "") + req.Header.Set("Authorization", "Bearer "+session.Key) + res := testutils.HTTPDo(t, req) + + assert.Equal(t, res.StatusCode, http.StatusOK, "status code mismatch") + }) + + t.Run("expired session with header", func(t *testing.T) { + server := httptest.NewServer(Auth(db, handler, nil)) + defer server.Close() + + req := testutils.MakeReq(server.URL, "GET", "/", "") + req.Header.Set("Authorization", "Bearer "+expiredSession.Key) + res := testutils.HTTPDo(t, req) + + assert.Equal(t, res.StatusCode, http.StatusUnauthorized, "status code mismatch") + }) + + t.Run("invalid session with header", func(t *testing.T) { + server := httptest.NewServer(Auth(db, handler, nil)) + defer server.Close() + + req := testutils.MakeReq(server.URL, "GET", "/", "") + req.Header.Set("Authorization", "Bearer someInvalidSessionKey=") + res := testutils.HTTPDo(t, req) + + assert.Equal(t, res.StatusCode, http.StatusUnauthorized, "status code mismatch") + }) + + t.Run("valid session with cookie", func(t *testing.T) { + server := httptest.NewServer(Auth(db, handler, nil)) + defer server.Close() + + req := testutils.MakeReq(server.URL, "GET", "/", "") + req.AddCookie(&http.Cookie{ + Name: "id", + Value: session.Key, + HttpOnly: true, + }) + res := testutils.HTTPDo(t, req) + + assert.Equal(t, res.StatusCode, http.StatusOK, "status code mismatch") + }) + + t.Run("expired session with cookie", func(t *testing.T) { + server := httptest.NewServer(Auth(db, handler, nil)) + defer server.Close() + + req := testutils.MakeReq(server.URL, "GET", "/", "") + req.AddCookie(&http.Cookie{ + Name: "id", + Value: expiredSession.Key, + HttpOnly: true, + }) + res := testutils.HTTPDo(t, req) + + assert.Equal(t, res.StatusCode, http.StatusUnauthorized, "status code mismatch") + }) + + t.Run("no auth", func(t *testing.T) { + server := httptest.NewServer(Auth(db, handler, nil)) + defer server.Close() + + req := testutils.MakeReq(server.URL, "GET", "/", "") + res := testutils.HTTPDo(t, req) + + assert.Equal(t, res.StatusCode, http.StatusUnauthorized, "status code mismatch") + }) + + t.Run("redirect guests to login", func(t *testing.T) { + server := httptest.NewServer(Auth(db, handler, &AuthParams{RedirectGuestsToLogin: true})) + defer server.Close() + + req := testutils.MakeReq(server.URL, "GET", "/settings", "") + res := testutils.HTTPDo(t, req) + + assert.Equal(t, res.StatusCode, http.StatusFound, "status code mismatch") + assert.Equal(t, res.Header.Get("Location"), "/login?referrer=%2Fsettings", "location mismatch") + }) +} + +func TestTokenAuth(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + tok := database.Token{ + UserID: user.ID, + Type: database.TokenTypeResetPassword, + Value: "xpwFnc0MdllFUePDq9DLeQ==", + } + testutils.MustExec(t, db.Save(&tok), "preparing token") + session := database.Session{ + Key: "A9xgggqzTHETy++GDi1NpDNe0iyqosPm9bitdeNGkJU=", + UserID: user.ID, + ExpiresAt: time.Now().Add(time.Hour * 24), + } + testutils.MustExec(t, db.Save(&session), "preparing session") + + handler := func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + } + + server := httptest.NewServer(TokenAuth(db, handler, database.TokenTypeResetPassword, nil)) + defer server.Close() + + t.Run("with token", func(t *testing.T) { + req := testutils.MakeReq(server.URL, "GET", "/?token=xpwFnc0MdllFUePDq9DLeQ==", "") + res := testutils.HTTPDo(t, req) + + assert.Equal(t, res.StatusCode, http.StatusOK, "status code mismatch") + }) + + t.Run("with invalid token", func(t *testing.T) { + req := testutils.MakeReq(server.URL, "GET", "/?token=someRandomToken==", "") + res := testutils.HTTPDo(t, req) + + assert.Equal(t, res.StatusCode, http.StatusUnauthorized, "status code mismatch") + }) + + t.Run("with session header", func(t *testing.T) { + req := testutils.MakeReq(server.URL, "GET", "/", "") + req.Header.Set("Authorization", "Bearer "+session.Key) + res := testutils.HTTPDo(t, req) + + assert.Equal(t, res.StatusCode, http.StatusOK, "status code mismatch") + }) + + t.Run("with invalid session", func(t *testing.T) { + req := testutils.MakeReq(server.URL, "GET", "/", "") + req.Header.Set("Authorization", "Bearer someInvalidSessionKey=") + res := testutils.HTTPDo(t, req) + + assert.Equal(t, res.StatusCode, http.StatusUnauthorized, "status code mismatch") + }) + + t.Run("without anything", func(t *testing.T) { + req := testutils.MakeReq(server.URL, "GET", "/", "") + res := testutils.HTTPDo(t, req) + + assert.Equal(t, res.StatusCode, http.StatusUnauthorized, "status code mismatch") + }) +} + +func TestWithAccount(t *testing.T) { + db := testutils.InitMemoryDB(t) + + handler := func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + } + + t.Run("authenticated user", func(t *testing.T) { + user := testutils.SetupUserData(db, "alice@test.com", "pass1234") + + server := httptest.NewServer(Auth(db, handler, nil)) + defer server.Close() + + req := testutils.MakeReq(server.URL, "GET", "/", "") + res := testutils.HTTPAuthDo(t, db, req, user) + + assert.Equal(t, res.StatusCode, http.StatusOK, "status code mismatch") + }) +} diff --git a/pkg/server/middleware/helpers.go b/pkg/server/middleware/helpers.go index 3e7aad1c..580efa7f 100644 --- a/pkg/server/middleware/helpers.go +++ b/pkg/server/middleware/helpers.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package middleware @@ -43,7 +40,7 @@ func RespondForbidden(w http.ResponseWriter) { // RespondUnauthorized responds with unauthorized func RespondUnauthorized(w http.ResponseWriter) { UnsetSessionCookie(w) - w.Header().Add("WWW-Authenticate", `Bearer realm="Dnote Pro", charset="UTF-8"`) + w.Header().Add("WWW-Authenticate", `Bearer realm="Dnote", charset="UTF-8"`) http.Error(w, "unauthorized", http.StatusUnauthorized) } @@ -92,7 +89,6 @@ func DoError(w http.ResponseWriter, msg string, err error, statusCode int) { // NotSupported is the handler for the route that is no longer supported func NotSupported(w http.ResponseWriter, r *http.Request) { http.Error(w, "API version is not supported. Please upgrade your client.", http.StatusGone) - return } // getSessionKeyFromCookie reads and returns a session key from the cookie sent by the diff --git a/pkg/server/middleware/helpers_test.go b/pkg/server/middleware/helpers_test.go index ba1a92c6..3142326f 100644 --- a/pkg/server/middleware/helpers_test.go +++ b/pkg/server/middleware/helpers_test.go @@ -1,34 +1,25 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package middleware import ( - "fmt" "net/http" - "net/http/httptest" "testing" - "time" "github.com/dnote/dnote/pkg/assert" - "github.com/dnote/dnote/pkg/server/app" - "github.com/dnote/dnote/pkg/server/database" - "github.com/dnote/dnote/pkg/server/testutils" "github.com/pkg/errors" ) @@ -180,521 +171,3 @@ func TestGetCredential(t *testing.T) { } } -func TestAuthMiddleware(t *testing.T) { - defer testutils.ClearData(testutils.DB) - - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@test.com", "pass1234") - - session := database.Session{ - Key: "A9xgggqzTHETy++GDi1NpDNe0iyqosPm9bitdeNGkJU=", - UserID: user.ID, - ExpiresAt: time.Now().Add(time.Hour * 24), - } - testutils.MustExec(t, testutils.DB.Save(&session), "preparing session") - session2 := database.Session{ - Key: "Vvgm3eBXfXGEFWERI7faiRJ3DAzJw+7DdT9J1LEyNfI=", - UserID: user.ID, - ExpiresAt: time.Now().Add(-time.Hour * 24), - } - testutils.MustExec(t, testutils.DB.Save(&session2), "preparing session") - - handler := func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - } - a := &app.App{DB: testutils.DB} - server := httptest.NewServer(Auth(a, handler, nil)) - defer server.Close() - - t.Run("with header", func(t *testing.T) { - testCases := []struct { - header string - expectedStatus int - }{ - { - header: fmt.Sprintf("Bearer %s", session.Key), - expectedStatus: http.StatusOK, - }, - { - header: fmt.Sprintf("Bearer %s", session2.Key), - expectedStatus: http.StatusUnauthorized, - }, - { - header: fmt.Sprintf("Bearer someInvalidSessionKey="), - expectedStatus: http.StatusUnauthorized, - }, - } - - for _, tc := range testCases { - t.Run(tc.header, func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", "/", "") - req.Header.Set("Authorization", tc.header) - - // execute - res := testutils.HTTPDo(t, req) - - // test - assert.Equal(t, res.StatusCode, tc.expectedStatus, "status code mismatch") - }) - } - }) - - t.Run("with cookie", func(t *testing.T) { - testCases := []struct { - cookie *http.Cookie - expectedStatus int - }{ - { - cookie: &http.Cookie{ - Name: "id", - Value: session.Key, - HttpOnly: true, - }, - expectedStatus: http.StatusOK, - }, - { - cookie: &http.Cookie{ - Name: "id", - Value: session2.Key, - HttpOnly: true, - }, - expectedStatus: http.StatusUnauthorized, - }, - { - cookie: &http.Cookie{ - Name: "id", - Value: "someInvalidSessionKey=", - HttpOnly: true, - }, - expectedStatus: http.StatusUnauthorized, - }, - } - - for _, tc := range testCases { - t.Run(tc.cookie.Value, func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", "/", "") - req.AddCookie(tc.cookie) - - // execute - res := testutils.HTTPDo(t, req) - - // test - assert.Equal(t, res.StatusCode, tc.expectedStatus, "status code mismatch") - }) - } - }) - - t.Run("without anything", func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", "/", "") - - // execute - res := testutils.HTTPDo(t, req) - - // test - assert.Equal(t, res.StatusCode, http.StatusUnauthorized, "status code mismatch") - }) -} - -func TestAuthMiddleware_ProOnly(t *testing.T) { - defer testutils.ClearData(testutils.DB) - - user := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&user).Update("cloud", false), "preparing session") - session := database.Session{ - Key: "A9xgggqzTHETy++GDi1NpDNe0iyqosPm9bitdeNGkJU=", - UserID: user.ID, - ExpiresAt: time.Now().Add(time.Hour * 24), - } - testutils.MustExec(t, testutils.DB.Save(&session), "preparing session") - - handler := func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - } - - a := &app.App{DB: testutils.DB} - server := httptest.NewServer(Auth(a, handler, &AuthParams{ - ProOnly: true, - })) - - defer server.Close() - - t.Run("with header", func(t *testing.T) { - testCases := []struct { - header string - expectedStatus int - }{ - { - header: fmt.Sprintf("Bearer %s", session.Key), - expectedStatus: http.StatusForbidden, - }, - { - header: fmt.Sprintf("Bearer someInvalidSessionKey="), - expectedStatus: http.StatusUnauthorized, - }, - } - - for _, tc := range testCases { - t.Run(tc.header, func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", "/", "") - req.Header.Set("Authorization", tc.header) - - // execute - res := testutils.HTTPDo(t, req) - - // test - assert.Equal(t, res.StatusCode, tc.expectedStatus, "status code mismatch") - }) - } - }) - - t.Run("with cookie", func(t *testing.T) { - testCases := []struct { - cookie *http.Cookie - expectedStatus int - }{ - { - cookie: &http.Cookie{ - Name: "id", - Value: session.Key, - HttpOnly: true, - }, - expectedStatus: http.StatusForbidden, - }, - { - cookie: &http.Cookie{ - Name: "id", - Value: "someInvalidSessionKey=", - HttpOnly: true, - }, - expectedStatus: http.StatusUnauthorized, - }, - } - - for _, tc := range testCases { - t.Run(tc.cookie.Value, func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", "/", "") - req.AddCookie(tc.cookie) - - // execute - res := testutils.HTTPDo(t, req) - - // test - assert.Equal(t, res.StatusCode, tc.expectedStatus, "status code mismatch") - }) - } - }) -} - -func TestAuthMiddleware_RedirectGuestsToLogin(t *testing.T) { - defer testutils.ClearData(testutils.DB) - - handler := func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - } - - a := &app.App{DB: testutils.DB} - server := httptest.NewServer(Auth(a, handler, &AuthParams{ - RedirectGuestsToLogin: true, - })) - - defer server.Close() - - t.Run("guest", func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", "/", "") - - // execute - res := testutils.HTTPDo(t, req) - - // test - assert.Equal(t, res.StatusCode, http.StatusFound, "status code mismatch") - assert.Equal(t, res.Header.Get("Location"), "/login?referrer=%2F", "location header mismatch") - }) - - t.Run("logged in user", func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", "/", "") - - user := testutils.SetupUserData() - testutils.SetupAccountData(user, "alice@test.com", "pass1234") - - testutils.MustExec(t, testutils.DB.Model(&user).Update("cloud", false), "preparing session") - session := database.Session{ - Key: "A9xgggqzTHETy++GDi1NpDNe0iyqosPm9bitdeNGkJU=", - UserID: user.ID, - ExpiresAt: time.Now().Add(time.Hour * 24), - } - testutils.MustExec(t, testutils.DB.Save(&session), "preparing session") - - // execute - res := testutils.HTTPAuthDo(t, req, user) - req.Header.Set("Authorization", session.Key) - - // test - assert.Equal(t, res.StatusCode, http.StatusOK, "status code mismatch") - assert.Equal(t, res.Header.Get("Location"), "", "location header mismatch") - }) - -} - -func TestTokenAuthMiddleWare(t *testing.T) { - defer testutils.ClearData(testutils.DB) - - user := testutils.SetupUserData() - tok := database.Token{ - UserID: user.ID, - Type: database.TokenTypeEmailPreference, - Value: "xpwFnc0MdllFUePDq9DLeQ==", - } - testutils.MustExec(t, testutils.DB.Save(&tok), "preparing token") - session := database.Session{ - Key: "A9xgggqzTHETy++GDi1NpDNe0iyqosPm9bitdeNGkJU=", - UserID: user.ID, - ExpiresAt: time.Now().Add(time.Hour * 24), - } - testutils.MustExec(t, testutils.DB.Save(&session), "preparing session") - - handler := func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - } - - a := &app.App{DB: testutils.DB} - server := httptest.NewServer(TokenAuth(a, handler, database.TokenTypeEmailPreference, nil)) - defer server.Close() - - t.Run("with token", func(t *testing.T) { - testCases := []struct { - token string - expectedStatus int - }{ - { - token: "xpwFnc0MdllFUePDq9DLeQ==", - expectedStatus: http.StatusOK, - }, - { - token: "someRandomToken==", - expectedStatus: http.StatusUnauthorized, - }, - } - - for _, tc := range testCases { - t.Run(tc.token, func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", fmt.Sprintf("/?token=%s", tc.token), "") - - // execute - res := testutils.HTTPDo(t, req) - - // test - assert.Equal(t, res.StatusCode, tc.expectedStatus, "status code mismatch") - }) - } - }) - - t.Run("with session header", func(t *testing.T) { - testCases := []struct { - header string - expectedStatus int - }{ - { - header: fmt.Sprintf("Bearer %s", session.Key), - expectedStatus: http.StatusOK, - }, - { - header: fmt.Sprintf("Bearer someInvalidSessionKey="), - expectedStatus: http.StatusUnauthorized, - }, - } - - for _, tc := range testCases { - t.Run(tc.header, func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", "/", "") - req.Header.Set("Authorization", tc.header) - - // execute - res := testutils.HTTPDo(t, req) - - // test - assert.Equal(t, res.StatusCode, tc.expectedStatus, "status code mismatch") - }) - } - }) - - t.Run("with session cookie", func(t *testing.T) { - testCases := []struct { - cookie *http.Cookie - expectedStatus int - }{ - { - cookie: &http.Cookie{ - Name: "id", - Value: session.Key, - HttpOnly: true, - }, - expectedStatus: http.StatusOK, - }, - { - cookie: &http.Cookie{ - Name: "id", - Value: "someInvalidSessionKey=", - HttpOnly: true, - }, - expectedStatus: http.StatusUnauthorized, - }, - } - - for _, tc := range testCases { - t.Run(tc.cookie.Value, func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", "/", "") - req.AddCookie(tc.cookie) - - // execute - res := testutils.HTTPDo(t, req) - - // test - assert.Equal(t, res.StatusCode, tc.expectedStatus, "status code mismatch") - }) - } - }) - - t.Run("without anything", func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", "/", "") - - // execute - res := testutils.HTTPDo(t, req) - - // test - assert.Equal(t, res.StatusCode, http.StatusUnauthorized, "status code mismatch") - }) -} - -func TestTokenAuthMiddleWare_ProOnly(t *testing.T) { - defer testutils.ClearData(testutils.DB) - - user := testutils.SetupUserData() - testutils.MustExec(t, testutils.DB.Model(&user).Update("cloud", false), "preparing session") - tok := database.Token{ - UserID: user.ID, - Type: database.TokenTypeEmailPreference, - Value: "xpwFnc0MdllFUePDq9DLeQ==", - } - testutils.MustExec(t, testutils.DB.Save(&tok), "preparing token") - session := database.Session{ - Key: "A9xgggqzTHETy++GDi1NpDNe0iyqosPm9bitdeNGkJU=", - UserID: user.ID, - ExpiresAt: time.Now().Add(time.Hour * 24), - } - testutils.MustExec(t, testutils.DB.Save(&session), "preparing session") - - handler := func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - } - - a := &app.App{DB: testutils.DB} - server := httptest.NewServer(TokenAuth(a, handler, database.TokenTypeEmailPreference, &AuthParams{ - ProOnly: true, - })) - - defer server.Close() - - t.Run("with token", func(t *testing.T) { - testCases := []struct { - token string - expectedStatus int - }{ - { - token: "xpwFnc0MdllFUePDq9DLeQ==", - expectedStatus: http.StatusForbidden, - }, - { - token: "someRandomToken==", - expectedStatus: http.StatusUnauthorized, - }, - } - - for _, tc := range testCases { - t.Run(tc.token, func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", fmt.Sprintf("/?token=%s", tc.token), "") - - // execute - res := testutils.HTTPDo(t, req) - - // test - assert.Equal(t, res.StatusCode, tc.expectedStatus, "status code mismatch") - }) - } - }) - - t.Run("with session header", func(t *testing.T) { - testCases := []struct { - header string - expectedStatus int - }{ - { - header: fmt.Sprintf("Bearer %s", session.Key), - expectedStatus: http.StatusForbidden, - }, - { - header: fmt.Sprintf("Bearer someInvalidSessionKey="), - expectedStatus: http.StatusUnauthorized, - }, - } - - for _, tc := range testCases { - t.Run(tc.header, func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", "/", "") - req.Header.Set("Authorization", tc.header) - - // execute - res := testutils.HTTPDo(t, req) - - // test - assert.Equal(t, res.StatusCode, tc.expectedStatus, "status code mismatch") - }) - } - }) - - t.Run("with session cookie", func(t *testing.T) { - testCases := []struct { - cookie *http.Cookie - expectedStatus int - }{ - { - cookie: &http.Cookie{ - Name: "id", - Value: session.Key, - HttpOnly: true, - }, - expectedStatus: http.StatusForbidden, - }, - { - cookie: &http.Cookie{ - Name: "id", - Value: "someInvalidSessionKey=", - HttpOnly: true, - }, - expectedStatus: http.StatusUnauthorized, - }, - } - - for _, tc := range testCases { - t.Run(tc.cookie.Value, func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", "/", "") - req.AddCookie(tc.cookie) - - // execute - res := testutils.HTTPDo(t, req) - - // test - assert.Equal(t, res.StatusCode, tc.expectedStatus, "status code mismatch") - }) - } - }) - - t.Run("without anything", func(t *testing.T) { - req := testutils.MakeReq(server.URL, "GET", "/", "") - - // execute - res := testutils.HTTPDo(t, req) - - // test - assert.Equal(t, res.StatusCode, http.StatusUnauthorized, "status code mismatch") - }) -} diff --git a/pkg/server/middleware/limit.go b/pkg/server/middleware/limit.go index 280ca472..bc26664f 100644 --- a/pkg/server/middleware/limit.go +++ b/pkg/server/middleware/limit.go @@ -1,26 +1,22 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package middleware import ( "net/http" - "os" "strings" "sync" "time" @@ -29,63 +25,81 @@ import ( "golang.org/x/time/rate" ) +const ( + // serverRateLimitPerSecond is the max requests per second the server will accept per IP + serverRateLimitPerSecond = 50 + // serverRateLimitBurst is the burst capacity for rate limiting + serverRateLimitBurst = 100 +) + type visitor struct { limiter *rate.Limiter lastSeen time.Time } -var visitors = make(map[string]*visitor) -var mtx sync.RWMutex - -func init() { - go cleanupVisitors() +// RateLimiter holds the rate limiting state for visitors +type RateLimiter struct { + visitors map[string]*visitor + mtx sync.RWMutex } -// addVisitor adds a new visitor to the map and returns a limiter for the visitor -func addVisitor(identifier string) *rate.Limiter { - // initialize a token bucket - limiter := rate.NewLimiter(rate.Every(1*time.Second), 60) +// NewRateLimiter creates a new rate limiter instance +func NewRateLimiter() *RateLimiter { + rl := &RateLimiter{ + visitors: make(map[string]*visitor), + } + go rl.cleanupVisitors() + return rl +} - mtx.Lock() - visitors[identifier] = &visitor{ +var defaultLimiter = NewRateLimiter() + +// addVisitor adds a new visitor to the map and returns a limiter for the visitor +func (rl *RateLimiter) addVisitor(identifier string) *rate.Limiter { + // Calculate interval from rate: 1 second / requests per second + interval := time.Second / time.Duration(serverRateLimitPerSecond) + limiter := rate.NewLimiter(rate.Every(interval), serverRateLimitBurst) + + rl.mtx.Lock() + rl.visitors[identifier] = &visitor{ limiter: limiter, lastSeen: time.Now()} - mtx.Unlock() + rl.mtx.Unlock() return limiter } // getVisitor returns a limiter for a visitor with the given identifier. It // adds the visitor to the map if not seen before. -func getVisitor(identifier string) *rate.Limiter { - mtx.RLock() - v, exists := visitors[identifier] +func (rl *RateLimiter) getVisitor(identifier string) *rate.Limiter { + rl.mtx.RLock() + v, exists := rl.visitors[identifier] if !exists { - mtx.RUnlock() - return addVisitor(identifier) + rl.mtx.RUnlock() + return rl.addVisitor(identifier) } v.lastSeen = time.Now() - mtx.RUnlock() + rl.mtx.RUnlock() return v.limiter } // cleanupVisitors deletes visitors that has not been seen in a while from the // map of visitors -func cleanupVisitors() { +func (rl *RateLimiter) cleanupVisitors() { for { time.Sleep(time.Minute) - mtx.Lock() + rl.mtx.Lock() - for identifier, v := range visitors { - if time.Now().Sub(v.lastSeen) > 3*time.Minute { - delete(visitors, identifier) + for identifier, v := range rl.visitors { + if time.Since(v.lastSeen) > 3*time.Minute { + delete(rl.visitors, identifier) } } - mtx.Unlock() + rl.mtx.Unlock() } } @@ -107,10 +121,10 @@ func lookupIP(r *http.Request) string { } // Limit is a middleware to rate limit the handler -func Limit(next http.Handler) http.HandlerFunc { +func (rl *RateLimiter) Limit(next http.Handler) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { identifier := lookupIP(r) - limiter := getVisitor(identifier) + limiter := rl.getVisitor(identifier) if !limiter.Allow() { http.Error(w, "Too many requests", http.StatusTooManyRequests) @@ -124,12 +138,12 @@ func Limit(next http.Handler) http.HandlerFunc { }) } -// ApplyLimit applies rate limit conditionally +// ApplyLimit applies rate limit conditionally using the global limiter func ApplyLimit(h http.HandlerFunc, rateLimit bool) http.Handler { ret := h - if rateLimit && os.Getenv("GO_ENV") != "TEST" { - ret = Limit(ret) + if rateLimit { + ret = defaultLimiter.Limit(ret) } return ret diff --git a/pkg/server/middleware/limit_test.go b/pkg/server/middleware/limit_test.go new file mode 100644 index 00000000..594d0a94 --- /dev/null +++ b/pkg/server/middleware/limit_test.go @@ -0,0 +1,79 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package middleware + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +func TestLimit(t *testing.T) { + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + limiter := NewRateLimiter() + middleware := limiter.Limit(handler) + + // Make burst + 5 requests from same IP + numRequests := serverRateLimitBurst + 5 + blockedCount := 0 + + for range numRequests { + req := httptest.NewRequest("GET", "/test", nil) + req.RemoteAddr = "192.168.1.1:1234" + w := httptest.NewRecorder() + + middleware.ServeHTTP(w, req) + + if w.Code == http.StatusTooManyRequests { + blockedCount++ + } + } + + // At least some requests after burst should be blocked + if blockedCount == 0 { + t.Error("Expected some requests to be rate limited after burst") + } +} + +func TestLimit_DifferentIPs(t *testing.T) { + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + limiter := NewRateLimiter() + middleware := limiter.Limit(handler) + + // Exhaust rate limit for first IP + for range serverRateLimitBurst + 5 { + req := httptest.NewRequest("GET", "/test", nil) + req.RemoteAddr = "192.168.1.1:1234" + w := httptest.NewRecorder() + middleware.ServeHTTP(w, req) + } + + // Request from different IP should still succeed + req := httptest.NewRequest("GET", "/test", nil) + req.RemoteAddr = "192.168.1.2:5678" + w := httptest.NewRecorder() + middleware.ServeHTTP(w, req) + + if w.Code != http.StatusOK { + t.Errorf("Request from different IP should succeed, got status %d", w.Code) + } +} diff --git a/pkg/server/middleware/logging.go b/pkg/server/middleware/logging.go index 44777d1e..13c9a9c9 100644 --- a/pkg/server/middleware/logging.go +++ b/pkg/server/middleware/logging.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package middleware diff --git a/pkg/server/middleware/main_test.go b/pkg/server/middleware/main_test.go deleted file mode 100644 index 3687f95c..00000000 --- a/pkg/server/middleware/main_test.go +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package middleware - -import ( - "os" - "testing" - - "github.com/dnote/dnote/pkg/server/testutils" -) - -func TestMain(m *testing.M) { - testutils.InitTestDB() - - code := m.Run() - testutils.ClearData(testutils.DB) - - os.Exit(code) -} diff --git a/pkg/server/middleware/middleware.go b/pkg/server/middleware/middleware.go index a9ebe4e9..e509c169 100644 --- a/pkg/server/middleware/middleware.go +++ b/pkg/server/middleware/middleware.go @@ -1,51 +1,29 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package middleware import ( "net/http" - "net/url" "github.com/dnote/dnote/pkg/server/app" - "github.com/gorilla/schema" ) // Middleware is a middleware for request handlers type Middleware func(h http.Handler, app *app.App, rateLimit bool) http.Handler -type payload struct { - Method string `schema:"_method"` -} - -func parseValues(values url.Values, dst interface{}) error { - dec := schema.NewDecoder() - - // Ignore CSRF token field - dec.IgnoreUnknownKeys(true) - - if err := dec.Decode(dst, values); err != nil { - return err - } - - return nil -} - // methodOverrideKey is the form key for overriding the method var methodOverrideKey = "_method" diff --git a/pkg/server/operations/doc.go b/pkg/server/operations/doc.go index e6ea7f55..7a4fe7c8 100644 --- a/pkg/server/operations/doc.go +++ b/pkg/server/operations/doc.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ /* diff --git a/pkg/server/operations/main_test.go b/pkg/server/operations/main_test.go deleted file mode 100644 index 1e326fc7..00000000 --- a/pkg/server/operations/main_test.go +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package operations - -import ( - "os" - "testing" - - "github.com/dnote/dnote/pkg/server/testutils" -) - -func TestMain(m *testing.M) { - testutils.InitTestDB() - - code := m.Run() - testutils.ClearData(testutils.DB) - - os.Exit(code) -} diff --git a/pkg/server/operations/notes.go b/pkg/server/operations/notes.go index 6d1b207a..74b279e4 100644 --- a/pkg/server/operations/notes.go +++ b/pkg/server/operations/notes.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package operations @@ -22,8 +19,8 @@ import ( "github.com/dnote/dnote/pkg/server/database" "github.com/dnote/dnote/pkg/server/helpers" "github.com/dnote/dnote/pkg/server/permissions" - "github.com/jinzhu/gorm" "github.com/pkg/errors" + "gorm.io/gorm" ) // GetNote retrieves a note for the given user @@ -33,15 +30,12 @@ func GetNote(db *gorm.DB, uuid string, user *database.User) (database.Note, bool return zeroNote, false, nil } - conn := db.Where("notes.uuid = ? AND deleted = ?", uuid, false) - conn = database.PreloadNote(conn) - var note database.Note - conn = conn.Find(¬e) + err := database.PreloadNote(db.Where("notes.uuid = ? AND deleted = ?", uuid, false)).Find(¬e).Error - if conn.RecordNotFound() { + if errors.Is(err, gorm.ErrRecordNotFound) { return zeroNote, false, nil - } else if err := conn.Error; err != nil { + } else if err != nil { return zeroNote, false, errors.Wrap(err, "finding note") } diff --git a/pkg/server/operations/notes_test.go b/pkg/server/operations/notes_test.go index 1f418920..a09c1c28 100644 --- a/pkg/server/operations/notes_test.go +++ b/pkg/server/operations/notes_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package operations @@ -28,38 +25,29 @@ import ( ) func TestGetNote(t *testing.T) { - user := testutils.SetupUserData() - anotherUser := testutils.SetupUserData() + db := testutils.InitMemoryDB(t) - defer testutils.ClearData(testutils.DB) + user := testutils.SetupUserData(db, "user@test.com", "password123") + anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") b1 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "js", } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1") + testutils.MustExec(t, db.Save(&b1), "preparing b1") - privateNote := database.Note{ + note := database.Note{ + UUID: testutils.MustUUID(t), UserID: user.ID, BookUUID: b1.UUID, - Body: "privateNote content", + Body: "note content", Deleted: false, - Public: false, } - testutils.MustExec(t, testutils.DB.Save(&privateNote), "preparing privateNote") + testutils.MustExec(t, db.Save(¬e), "preparing note") - publicNote := database.Note{ - UserID: user.ID, - BookUUID: b1.UUID, - Body: "privateNote content", - Deleted: false, - Public: true, - } - testutils.MustExec(t, testutils.DB.Save(&publicNote), "preparing privateNote") - - var privateNoteRecord, publicNoteRecord database.Note - testutils.MustExec(t, testutils.DB.Where("uuid = ?", privateNote.UUID).Preload("Book").Preload("User").First(&privateNoteRecord), "finding privateNote") - testutils.MustExec(t, testutils.DB.Where("uuid = ?", publicNote.UUID).Preload("Book").Preload("User").First(&publicNoteRecord), "finding publicNote") + var noteRecord database.Note + testutils.MustExec(t, db.Where("uuid = ?", note.UUID).Preload("Book").Preload("User").First(¬eRecord), "finding note") testCases := []struct { name string @@ -69,45 +57,31 @@ func TestGetNote(t *testing.T) { expectedNote database.Note }{ { - name: "owner accessing private note", + name: "owner accessing note", user: user, - note: privateNote, + note: note, expectedOK: true, - expectedNote: privateNoteRecord, + expectedNote: noteRecord, }, { - name: "non-owner accessing private note", + name: "non-owner accessing note", user: anotherUser, - note: privateNote, + note: note, expectedOK: false, expectedNote: database.Note{}, }, { - name: "non-owner accessing public note", - user: anotherUser, - note: publicNote, - expectedOK: true, - expectedNote: publicNoteRecord, - }, - { - name: "guest accessing private note", + name: "guest accessing note", user: database.User{}, - note: privateNote, + note: note, expectedOK: false, expectedNote: database.Note{}, }, - { - name: "guest accessing public note", - user: database.User{}, - note: publicNote, - expectedOK: true, - expectedNote: publicNoteRecord, - }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - note, ok, err := GetNote(testutils.DB, tc.note.UUID, &tc.user) + note, ok, err := GetNote(db, tc.note.UUID, &tc.user) if err != nil { t.Fatal(errors.Wrap(err, "executing")) } @@ -119,29 +93,28 @@ func TestGetNote(t *testing.T) { } func TestGetNote_nonexistent(t *testing.T) { - user := testutils.SetupUserData() + db := testutils.InitMemoryDB(t) - defer testutils.ClearData(testutils.DB) + user := testutils.SetupUserData(db, "user@test.com", "password123") b1 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "js", } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1") + testutils.MustExec(t, db.Save(&b1), "preparing b1") - n1UUID := "4fd19336-671e-4ff3-8f22-662b80e22edc" n1 := database.Note{ - UUID: n1UUID, + UUID: "4fd19336-671e-4ff3-8f22-662b80e22edc", UserID: user.ID, BookUUID: b1.UUID, Body: "n1 content", Deleted: false, - Public: false, } - testutils.MustExec(t, testutils.DB.Save(&n1), "preparing n1") + testutils.MustExec(t, db.Save(&n1), "preparing n1") nonexistentUUID := "4fd19336-671e-4ff3-8f22-662b80e22edd" - note, ok, err := GetNote(testutils.DB, nonexistentUUID, &user) + note, ok, err := GetNote(db, nonexistentUUID, &user) if err != nil { t.Fatal(errors.Wrap(err, "executing")) } diff --git a/pkg/server/permissions/permissions.go b/pkg/server/permissions/permissions.go index 2dc6e636..54427acf 100644 --- a/pkg/server/permissions/permissions.go +++ b/pkg/server/permissions/permissions.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package permissions @@ -24,9 +21,6 @@ import ( // ViewNote checks if the given user can view the given note func ViewNote(user *database.User, note database.Note) bool { - if note.Public { - return true - } if user == nil { return false } diff --git a/pkg/server/permissions/permissions_test.go b/pkg/server/permissions/permissions_test.go index 2d139361..2a5daca1 100644 --- a/pkg/server/permissions/permissions_test.go +++ b/pkg/server/permissions/permissions_test.go @@ -1,25 +1,21 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package permissions import ( - "os" "testing" "github.com/dnote/dnote/pkg/assert" @@ -27,72 +23,40 @@ import ( "github.com/dnote/dnote/pkg/server/testutils" ) -func TestMain(m *testing.M) { - testutils.InitTestDB() - - code := m.Run() - testutils.ClearData(testutils.DB) - - os.Exit(code) -} - func TestViewNote(t *testing.T) { - user := testutils.SetupUserData() - anotherUser := testutils.SetupUserData() + db := testutils.InitMemoryDB(t) - defer testutils.ClearData(testutils.DB) + user := testutils.SetupUserData(db, "user@test.com", "password123") + anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") b1 := database.Book{ + UUID: testutils.MustUUID(t), UserID: user.ID, Label: "js", } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1") + testutils.MustExec(t, db.Save(&b1), "preparing b1") - privateNote := database.Note{ + note := database.Note{ + UUID: testutils.MustUUID(t), UserID: user.ID, BookUUID: b1.UUID, - Body: "privateNote content", + Body: "note content", Deleted: false, - Public: false, } - testutils.MustExec(t, testutils.DB.Save(&privateNote), "preparing privateNote") + testutils.MustExec(t, db.Save(¬e), "preparing note") - publicNote := database.Note{ - UserID: user.ID, - BookUUID: b1.UUID, - Body: "privateNote content", - Deleted: false, - Public: true, - } - testutils.MustExec(t, testutils.DB.Save(&publicNote), "preparing privateNote") - - t.Run("owner accessing private note", func(t *testing.T) { - result := ViewNote(&user, privateNote) + t.Run("owner accessing note", func(t *testing.T) { + result := ViewNote(&user, note) assert.Equal(t, result, true, "result mismatch") }) - t.Run("owner accessing public note", func(t *testing.T) { - result := ViewNote(&user, publicNote) - assert.Equal(t, result, true, "result mismatch") - }) - - t.Run("non-owner accessing private note", func(t *testing.T) { - result := ViewNote(&anotherUser, privateNote) + t.Run("non-owner accessing note", func(t *testing.T) { + result := ViewNote(&anotherUser, note) assert.Equal(t, result, false, "result mismatch") }) - t.Run("non-owner accessing public note", func(t *testing.T) { - result := ViewNote(&anotherUser, publicNote) - assert.Equal(t, result, true, "result mismatch") - }) - - t.Run("guest accessing private note", func(t *testing.T) { - result := ViewNote(nil, privateNote) + t.Run("guest accessing note", func(t *testing.T) { + result := ViewNote(nil, note) assert.Equal(t, result, false, "result mismatch") }) - - t.Run("guest accessing public note", func(t *testing.T) { - result := ViewNote(nil, publicNote) - assert.Equal(t, result, true, "result mismatch") - }) } diff --git a/pkg/server/presenters/book.go b/pkg/server/presenters/book.go index a639abe7..9c117726 100644 --- a/pkg/server/presenters/book.go +++ b/pkg/server/presenters/book.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package presenters diff --git a/pkg/server/presenters/book_test.go b/pkg/server/presenters/book_test.go new file mode 100644 index 00000000..bf8a1b48 --- /dev/null +++ b/pkg/server/presenters/book_test.go @@ -0,0 +1,214 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package presenters + +import ( + "testing" + "time" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/server/database" +) + +func TestPresentBook(t *testing.T) { + createdAt := time.Date(2025, 1, 15, 10, 30, 45, 123456789, time.UTC) + updatedAt := time.Date(2025, 2, 20, 14, 45, 30, 987654321, time.UTC) + + testCases := []struct { + name string + input database.Book + expected Book + }{ + { + name: "basic book", + input: database.Book{ + Model: database.Model{ + ID: 1, + CreatedAt: createdAt, + UpdatedAt: updatedAt, + }, + UUID: "a1b2c3d4-e5f6-4789-a012-3456789abcde", + UserID: 42, + Label: "JavaScript", + USN: 100, + }, + expected: Book{ + UUID: "a1b2c3d4-e5f6-4789-a012-3456789abcde", + USN: 100, + CreatedAt: FormatTS(createdAt), + UpdatedAt: FormatTS(updatedAt), + Label: "JavaScript", + }, + }, + { + name: "book with special characters in label", + input: database.Book{ + Model: database.Model{ + ID: 2, + CreatedAt: createdAt, + UpdatedAt: updatedAt, + }, + UUID: "f1e2d3c4-b5a6-4987-b654-321fedcba098", + UserID: 99, + Label: "C++", + USN: 200, + }, + expected: Book{ + UUID: "f1e2d3c4-b5a6-4987-b654-321fedcba098", + USN: 200, + CreatedAt: FormatTS(createdAt), + UpdatedAt: FormatTS(updatedAt), + Label: "C++", + }, + }, + { + name: "book with empty label", + input: database.Book{ + Model: database.Model{ + ID: 3, + CreatedAt: createdAt, + UpdatedAt: updatedAt, + }, + UUID: "12345678-90ab-4cde-8901-234567890abc", + UserID: 1, + Label: "", + USN: 0, + }, + expected: Book{ + UUID: "12345678-90ab-4cde-8901-234567890abc", + USN: 0, + CreatedAt: FormatTS(createdAt), + UpdatedAt: FormatTS(updatedAt), + Label: "", + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + got := PresentBook(tc.input) + + assert.Equal(t, got.UUID, tc.expected.UUID, "UUID mismatch") + assert.Equal(t, got.USN, tc.expected.USN, "USN mismatch") + assert.Equal(t, got.Label, tc.expected.Label, "Label mismatch") + assert.Equal(t, got.CreatedAt, tc.expected.CreatedAt, "CreatedAt mismatch") + assert.Equal(t, got.UpdatedAt, tc.expected.UpdatedAt, "UpdatedAt mismatch") + }) + } +} + +func TestPresentBooks(t *testing.T) { + createdAt1 := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) + updatedAt1 := time.Date(2025, 1, 2, 0, 0, 0, 0, time.UTC) + createdAt2 := time.Date(2025, 2, 1, 0, 0, 0, 0, time.UTC) + updatedAt2 := time.Date(2025, 2, 2, 0, 0, 0, 0, time.UTC) + + testCases := []struct { + name string + input []database.Book + expected []Book + }{ + { + name: "empty slice", + input: []database.Book{}, + expected: []Book{}, + }, + { + name: "single book", + input: []database.Book{ + { + Model: database.Model{ + ID: 1, + CreatedAt: createdAt1, + UpdatedAt: updatedAt1, + }, + UUID: "9a8b7c6d-5e4f-4321-9876-543210fedcba", + UserID: 1, + Label: "Go", + USN: 10, + }, + }, + expected: []Book{ + { + UUID: "9a8b7c6d-5e4f-4321-9876-543210fedcba", + USN: 10, + CreatedAt: FormatTS(createdAt1), + UpdatedAt: FormatTS(updatedAt1), + Label: "Go", + }, + }, + }, + { + name: "multiple books", + input: []database.Book{ + { + Model: database.Model{ + ID: 1, + CreatedAt: createdAt1, + UpdatedAt: updatedAt1, + }, + UUID: "9a8b7c6d-5e4f-4321-9876-543210fedcba", + UserID: 1, + Label: "Go", + USN: 10, + }, + { + Model: database.Model{ + ID: 2, + CreatedAt: createdAt2, + UpdatedAt: updatedAt2, + }, + UUID: "abcdef01-2345-4678-9abc-def012345678", + UserID: 1, + Label: "Python", + USN: 20, + }, + }, + expected: []Book{ + { + UUID: "9a8b7c6d-5e4f-4321-9876-543210fedcba", + USN: 10, + CreatedAt: FormatTS(createdAt1), + UpdatedAt: FormatTS(updatedAt1), + Label: "Go", + }, + { + UUID: "abcdef01-2345-4678-9abc-def012345678", + USN: 20, + CreatedAt: FormatTS(createdAt2), + UpdatedAt: FormatTS(updatedAt2), + Label: "Python", + }, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + got := PresentBooks(tc.input) + + assert.Equal(t, len(got), len(tc.expected), "Length mismatch") + + for i := range got { + assert.Equal(t, got[i].UUID, tc.expected[i].UUID, "UUID mismatch") + assert.Equal(t, got[i].USN, tc.expected[i].USN, "USN mismatch") + assert.Equal(t, got[i].Label, tc.expected[i].Label, "Label mismatch") + assert.Equal(t, got[i].CreatedAt, tc.expected[i].CreatedAt, "CreatedAt mismatch") + assert.Equal(t, got[i].UpdatedAt, tc.expected[i].UpdatedAt, "UpdatedAt mismatch") + } + }) + } +} diff --git a/pkg/server/presenters/email_preference.go b/pkg/server/presenters/email_preference.go deleted file mode 100644 index 4cbab318..00000000 --- a/pkg/server/presenters/email_preference.go +++ /dev/null @@ -1,45 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package presenters - -import ( - "time" - - "github.com/dnote/dnote/pkg/server/database" -) - -// EmailPreference is a presented email digest -type EmailPreference struct { - InactiveReminder bool `json:"inactive_reminder"` - ProductUpdate bool `json:"product_update"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` -} - -// PresentEmailPreference presents a digest -func PresentEmailPreference(p database.EmailPreference) EmailPreference { - ret := EmailPreference{ - InactiveReminder: p.InactiveReminder, - ProductUpdate: p.ProductUpdate, - CreatedAt: FormatTS(p.CreatedAt), - UpdatedAt: FormatTS(p.UpdatedAt), - } - - return ret -} diff --git a/pkg/server/presenters/helpers.go b/pkg/server/presenters/helpers.go index 67ccb4d4..3cce728c 100644 --- a/pkg/server/presenters/helpers.go +++ b/pkg/server/presenters/helpers.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package presenters diff --git a/pkg/server/presenters/helpers_test.go b/pkg/server/presenters/helpers_test.go new file mode 100644 index 00000000..33b0ed2e --- /dev/null +++ b/pkg/server/presenters/helpers_test.go @@ -0,0 +1,32 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package presenters + +import ( + "testing" + "time" + + "github.com/dnote/dnote/pkg/assert" +) + +func TestFormatTS(t *testing.T) { + input := time.Date(2025, 1, 15, 10, 30, 45, 123456789, time.UTC) + expected := time.Date(2025, 1, 15, 10, 30, 45, 123457000, time.UTC) + + got := FormatTS(input) + + assert.Equal(t, got, expected, "FormatTS mismatch") +} diff --git a/pkg/server/presenters/note.go b/pkg/server/presenters/note.go index 9d832b6d..267717da 100644 --- a/pkg/server/presenters/note.go +++ b/pkg/server/presenters/note.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package presenters @@ -31,7 +28,6 @@ type Note struct { UpdatedAt time.Time `json:"updated_at"` Body string `json:"content"` AddedOn int64 `json:"added_on"` - Public bool `json:"public"` USN int `json:"usn"` Book NoteBook `json:"book"` User NoteUser `json:"user"` @@ -57,7 +53,6 @@ func PresentNote(note database.Note) Note { UpdatedAt: FormatTS(note.UpdatedAt), Body: note.Body, AddedOn: note.AddedOn, - Public: note.Public, USN: note.USN, Book: NoteBook{ UUID: note.Book.UUID, diff --git a/pkg/server/presenters/note_test.go b/pkg/server/presenters/note_test.go new file mode 100644 index 00000000..ee2fe78d --- /dev/null +++ b/pkg/server/presenters/note_test.go @@ -0,0 +1,120 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package presenters + +import ( + "testing" + "time" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/server/database" +) + +func TestPresentNote(t *testing.T) { + createdAt := time.Date(2025, 1, 15, 10, 30, 45, 123456789, time.UTC) + updatedAt := time.Date(2025, 2, 20, 14, 45, 30, 987654321, time.UTC) + + input := database.Note{ + Model: database.Model{ + ID: 1, + CreatedAt: createdAt, + UpdatedAt: updatedAt, + }, + UUID: "a1b2c3d4-e5f6-4789-a012-3456789abcde", + UserID: 42, + BookUUID: "f1e2d3c4-b5a6-4987-b654-321fedcba098", + Body: "Test note content", + AddedOn: 1234567890, + USN: 100, + Book: database.Book{ + UUID: "f1e2d3c4-b5a6-4987-b654-321fedcba098", + Label: "JavaScript", + }, + User: database.User{ + UUID: "9a8b7c6d-5e4f-4321-9876-543210fedcba", + }, + } + + got := PresentNote(input) + + assert.Equal(t, got.UUID, "a1b2c3d4-e5f6-4789-a012-3456789abcde", "UUID mismatch") + assert.Equal(t, got.Body, "Test note content", "Body mismatch") + assert.Equal(t, got.AddedOn, int64(1234567890), "AddedOn mismatch") + assert.Equal(t, got.USN, 100, "USN mismatch") + assert.Equal(t, got.CreatedAt, FormatTS(createdAt), "CreatedAt mismatch") + assert.Equal(t, got.UpdatedAt, FormatTS(updatedAt), "UpdatedAt mismatch") + assert.Equal(t, got.Book.UUID, "f1e2d3c4-b5a6-4987-b654-321fedcba098", "Book UUID mismatch") + assert.Equal(t, got.Book.Label, "JavaScript", "Book Label mismatch") + assert.Equal(t, got.User.UUID, "9a8b7c6d-5e4f-4321-9876-543210fedcba", "User UUID mismatch") +} + +func TestPresentNotes(t *testing.T) { + createdAt1 := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC) + updatedAt1 := time.Date(2025, 1, 2, 0, 0, 0, 0, time.UTC) + createdAt2 := time.Date(2025, 2, 1, 0, 0, 0, 0, time.UTC) + updatedAt2 := time.Date(2025, 2, 2, 0, 0, 0, 0, time.UTC) + + input := []database.Note{ + { + Model: database.Model{ + ID: 1, + CreatedAt: createdAt1, + UpdatedAt: updatedAt1, + }, + UUID: "a1b2c3d4-e5f6-4789-a012-3456789abcde", + UserID: 1, + BookUUID: "f1e2d3c4-b5a6-4987-b654-321fedcba098", + Body: "First note", + AddedOn: 1000000000, + USN: 10, + Book: database.Book{ + UUID: "f1e2d3c4-b5a6-4987-b654-321fedcba098", + Label: "Go", + }, + User: database.User{ + UUID: "9a8b7c6d-5e4f-4321-9876-543210fedcba", + }, + }, + { + Model: database.Model{ + ID: 2, + CreatedAt: createdAt2, + UpdatedAt: updatedAt2, + }, + UUID: "12345678-90ab-4cde-8901-234567890abc", + UserID: 1, + BookUUID: "abcdef01-2345-4678-9abc-def012345678", + Body: "Second note", + AddedOn: 2000000000, + USN: 20, + Book: database.Book{ + UUID: "abcdef01-2345-4678-9abc-def012345678", + Label: "Python", + }, + User: database.User{ + UUID: "9a8b7c6d-5e4f-4321-9876-543210fedcba", + }, + }, + } + + got := PresentNotes(input) + + assert.Equal(t, len(got), 2, "Length mismatch") + assert.Equal(t, got[0].UUID, "a1b2c3d4-e5f6-4789-a012-3456789abcde", "Note 0 UUID mismatch") + assert.Equal(t, got[0].Body, "First note", "Note 0 Body mismatch") + assert.Equal(t, got[1].UUID, "12345678-90ab-4cde-8901-234567890abc", "Note 1 UUID mismatch") + assert.Equal(t, got[1].Body, "Second note", "Note 1 Body mismatch") +} diff --git a/pkg/server/session/session.go b/pkg/server/session/session.go index 60a6a107..b9ae62d4 100644 --- a/pkg/server/session/session.go +++ b/pkg/server/session/session.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package session @@ -24,18 +21,14 @@ import ( // Session represents user session type Session struct { - UUID string `json:"uuid"` - Email string `json:"email"` - EmailVerified bool `json:"email_verified"` - Pro bool `json:"pro"` + UUID string `json:"uuid"` + Email string `json:"email"` } // New returns a new session for the given user -func New(user database.User, account database.Account) Session { +func New(user database.User) Session { return Session{ - UUID: user.UUID, - Pro: user.Cloud, - Email: account.Email.String, - EmailVerified: account.EmailVerified, + UUID: user.UUID, + Email: user.Email.String, } } diff --git a/pkg/server/session/session_test.go b/pkg/server/session/session_test.go index a5867ce6..081a82de 100644 --- a/pkg/server/session/session_test.go +++ b/pkg/server/session/session_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package session @@ -27,38 +24,34 @@ import ( ) func TestNew(t *testing.T) { - u1 := database.User{UUID: "0f5f0054-d23f-4be1-b5fb-57673109e9cb", Cloud: true} - a1 := database.Account{Email: database.ToNullString("alice@example.com"), EmailVerified: false} + u1 := database.User{ + UUID: "0f5f0054-d23f-4be1-b5fb-57673109e9cb", + Email: database.ToNullString("alice@example.com"), + } - u2 := database.User{UUID: "718a1041-bbe6-496e-bbe4-ea7e572c295e", Cloud: false} - a2 := database.Account{Email: database.ToNullString("bob@example.com"), EmailVerified: false} + u2 := database.User{ + UUID: "718a1041-bbe6-496e-bbe4-ea7e572c295e", + Email: database.ToNullString("bob@example.com"), + } testCases := []struct { - user database.User - account database.Account - expectedPro bool + user database.User }{ { - user: u1, - account: a1, - expectedPro: true, + user: u1, }, { - user: u2, - account: a2, - expectedPro: false, + user: u2, }, } - for _, tc := range testCases { - t.Run(fmt.Sprintf("user pro %t", tc.expectedPro), func(t *testing.T) { + for idx, tc := range testCases { + t.Run(fmt.Sprintf("user %d", idx), func(t *testing.T) { // Execute - got := New(tc.user, tc.account) + got := New(tc.user) expected := Session{ - UUID: tc.user.UUID, - Pro: tc.expectedPro, - Email: tc.account.Email.String, - EmailVerified: tc.account.EmailVerified, + UUID: tc.user.UUID, + Email: tc.user.Email.String, } assert.DeepEqual(t, got, expected, "result mismatch") diff --git a/pkg/server/testutils/main.go b/pkg/server/testutils/main.go index b49b2699..8fce9ebf 100644 --- a/pkg/server/testutils/main.go +++ b/pkg/server/testutils/main.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package testutils provides utilities used in tests @@ -27,129 +24,94 @@ import ( "net/http" "net/url" "reflect" - // "strconv" "strings" "sync" "testing" "time" - "github.com/dnote/dnote/pkg/server/config" "github.com/dnote/dnote/pkg/server/database" - "github.com/jinzhu/gorm" + "github.com/dnote/dnote/pkg/server/helpers" "github.com/pkg/errors" "golang.org/x/crypto/bcrypt" + "gorm.io/driver/sqlite" + "gorm.io/gorm" ) -func init() { - rand.Seed(time.Now().UnixNano()) +// InitDB opens a database at the given path and initializes the schema +func InitDB(dbPath string) *gorm.DB { + db := database.Open(dbPath) + database.InitSchema(db) + database.Migrate(db) + return db } -// DB is the database connection to a test database -var DB *gorm.DB - -// InitTestDB establishes connection pool with the test database specified by -// the environment variable configuration and initalizes a new schema -func InitTestDB() { - c := config.Load() - fmt.Println(c.DB.GetConnectionStr()) - db := database.Open(c) +// InitMemoryDB creates an in-memory SQLite database with the schema initialized +func InitMemoryDB(t *testing.T) *gorm.DB { + // Use file-based in-memory database with unique UUID per test to avoid sharing + uuid, err := helpers.GenUUID() + if err != nil { + t.Fatalf("failed to generate UUID for test database: %v", err) + } + dbName := fmt.Sprintf("file:%s?mode=memory&cache=shared", uuid) + db, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open in-memory database: %v", err) + } database.InitSchema(db) + database.Migrate(db) - DB = db + return db } -// ClearData deletes all records from the database -func ClearData(db *gorm.DB) { - if err := db.Delete(&database.Book{}).Error; err != nil { - panic(errors.Wrap(err, "Failed to clear books")) - } - if err := db.Delete(&database.Note{}).Error; err != nil { - panic(errors.Wrap(err, "Failed to clear notes")) - } - if err := db.Delete(&database.Notification{}).Error; err != nil { - panic(errors.Wrap(err, "Failed to clear notifications")) - } - if err := db.Delete(&database.User{}).Error; err != nil { - panic(errors.Wrap(err, "Failed to clear users")) - } - if err := db.Delete(&database.Account{}).Error; err != nil { - panic(errors.Wrap(err, "Failed to clear accounts")) - } - if err := db.Delete(&database.Token{}).Error; err != nil { - panic(errors.Wrap(err, "Failed to clear tokens")) - } - if err := db.Delete(&database.EmailPreference{}).Error; err != nil { - panic(errors.Wrap(err, "Failed to clear email preferences")) - } - if err := db.Delete(&database.Session{}).Error; err != nil { - panic(errors.Wrap(err, "Failed to clear sessions")) +// MustUUID generates a UUID and fails the test on error +func MustUUID(t *testing.T) string { + uuid, err := helpers.GenUUID() + if err != nil { + t.Fatal(errors.Wrap(err, "Failed to generate UUID")) } + return uuid } -// SetupUserData creates and returns a new user for testing purposes -func SetupUserData() database.User { - user := database.User{ - Cloud: true, - } - - if err := DB.Save(&user).Error; err != nil { - panic(errors.Wrap(err, "Failed to prepare user")) - } - - return user -} - -// SetupAccountData creates and returns a new account for the user -func SetupAccountData(user database.User, email, password string) database.Account { - account := database.Account{ - UserID: user.ID, - } - if email != "" { - account.Email = database.ToNullString(email) +// SetupUserData creates and returns a new user with email and password for testing purposes +func SetupUserData(db *gorm.DB, email, password string) database.User { + uuid, err := helpers.GenUUID() + if err != nil { + panic(errors.Wrap(err, "Failed to generate UUID")) } hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) if err != nil { panic(errors.Wrap(err, "Failed to hash password")) } - account.Password = database.ToNullString(string(hashedPassword)) - if err := DB.Save(&account).Error; err != nil { - panic(errors.Wrap(err, "Failed to prepare account")) + user := database.User{ + UUID: uuid, + Email: database.ToNullString(email), + Password: database.ToNullString(string(hashedPassword)), } - return account + if err := db.Save(&user).Error; err != nil { + panic(errors.Wrap(err, "Failed to prepare user")) + } + + return user } // SetupSession creates and returns a new user session -func SetupSession(t *testing.T, user database.User) database.Session { +func SetupSession(db *gorm.DB, user database.User) database.Session { session := database.Session{ Key: "Vvgm3eBXfXGEFWERI7faiRJ3DAzJw+7DdT9J1LEyNfI=", UserID: user.ID, ExpiresAt: time.Now().Add(time.Hour * 24), } - if err := DB.Save(&session).Error; err != nil { - t.Fatal(errors.Wrap(err, "Failed to prepare user")) + if err := db.Save(&session).Error; err != nil { + panic(errors.Wrap(err, "Failed to prepare user")) } return session } -// SetupEmailPreferenceData creates and returns a new email frequency for a user -func SetupEmailPreferenceData(user database.User, inactiveReminder bool) database.EmailPreference { - frequency := database.EmailPreference{ - UserID: user.ID, - InactiveReminder: inactiveReminder, - } - - if err := DB.Save(&frequency).Error; err != nil { - panic(errors.Wrap(err, "Failed to prepare email frequency")) - } - - return frequency -} - // HTTPDo makes an HTTP request and returns a response func HTTPDo(t *testing.T, req *http.Request) *http.Response { hc := http.Client{ @@ -169,8 +131,8 @@ func HTTPDo(t *testing.T, req *http.Request) *http.Response { return res } -// SetReqAuthHeader sets the authorization header in the given request for the given user -func SetReqAuthHeader(t *testing.T, req *http.Request, user database.User) { +// SetReqAuthHeader sets the authorization header in the given request for the given user with a specific DB +func SetReqAuthHeader(t *testing.T, db *gorm.DB, req *http.Request, user database.User) { b := make([]byte, 32) if _, err := rand.Read(b); err != nil { t.Fatal(errors.Wrap(err, "reading random bits")) @@ -181,19 +143,18 @@ func SetReqAuthHeader(t *testing.T, req *http.Request, user database.User) { UserID: user.ID, ExpiresAt: time.Now().Add(time.Hour * 10 * 24), } - if err := DB.Save(&session).Error; err != nil { + if err := db.Save(&session).Error; err != nil { t.Fatal(errors.Wrap(err, "Failed to prepare user")) } req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", session.Key)) } -// HTTPAuthDo makes an HTTP request with an appropriate authorization header for a user -func HTTPAuthDo(t *testing.T, req *http.Request, user database.User) *http.Response { - SetReqAuthHeader(t, req, user) +// HTTPAuthDo makes an HTTP request with an appropriate authorization header for a user with a specific DB +func HTTPAuthDo(t *testing.T, db *gorm.DB, req *http.Request, user database.User) *http.Response { + SetReqAuthHeader(t, db, req, user) return HTTPDo(t, req) - } // MakeReq makes an HTTP request and returns a response @@ -249,10 +210,10 @@ func MustRespondJSON(t *testing.T, w http.ResponseWriter, i interface{}, message // MockEmail is a mock email data type MockEmail struct { - Subject string - From string - To []string - Body string + TemplateType string + From string + To []string + Data interface{} } // MockEmailbackendImplementation is an email backend that simply discards the emails @@ -269,16 +230,16 @@ func (b *MockEmailbackendImplementation) Clear() { b.Emails = []MockEmail{} } -// Queue is an implementation of Backend.Queue. -func (b *MockEmailbackendImplementation) Queue(subject, from string, to []string, contentType, body string) error { +// SendEmail is an implementation of Backend.SendEmail. +func (b *MockEmailbackendImplementation) SendEmail(templateType, from string, to []string, data interface{}) error { b.mu.Lock() defer b.mu.Unlock() b.Emails = append(b.Emails, MockEmail{ - Subject: subject, - From: from, - To: to, - Body: body, + TemplateType: templateType, + From: from, + To: to, + Data: data, }) return nil diff --git a/pkg/server/tmpl/app.go b/pkg/server/tmpl/app.go deleted file mode 100644 index 78509a45..00000000 --- a/pkg/server/tmpl/app.go +++ /dev/null @@ -1,103 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package tmpl - -import ( - "bytes" - "html/template" - "net/http" - "regexp" - - "github.com/jinzhu/gorm" - "github.com/pkg/errors" -) - -// routes -var notesPathRegex = regexp.MustCompile("^/notes/([^/]+)$") - -// template names -var templateIndex = "index" -var templateNoteMetaTags = "note_metatags" - -// AppShell represents the application in HTML -type AppShell struct { - DB *gorm.DB - T *template.Template -} - -// ErrNotFound is an error indicating that a resource was not found -var ErrNotFound = errors.New("not found") - -// NewAppShell parses the templates for the application -func NewAppShell(db *gorm.DB, content []byte) (AppShell, error) { - t, err := template.New(templateIndex).Parse(string(content)) - if err != nil { - return AppShell{}, errors.Wrap(err, "parsing the index template") - } - - _, err = t.New(templateNoteMetaTags).Parse(noteMetaTags) - if err != nil { - return AppShell{}, errors.Wrap(err, "parsing the note meta tags template") - } - - return AppShell{DB: db, T: t}, nil -} - -// Execute executes the index template -func (a AppShell) Execute(r *http.Request) ([]byte, error) { - data, err := a.getData(r) - if err != nil { - return nil, errors.Wrap(err, "getting data") - } - - var buf bytes.Buffer - if err := a.T.ExecuteTemplate(&buf, templateIndex, data); err != nil { - return nil, errors.Wrap(err, "executing template") - } - - return buf.Bytes(), nil -} - -func (a AppShell) getData(r *http.Request) (tmplData, error) { - path := r.URL.Path - - if ok, params := matchPath(path, notesPathRegex); ok { - p, err := a.newNotePage(r, params[0]) - if err != nil { - return tmplData{}, errors.Wrap(err, "instantiating note page") - } - - return p.getData() - } - - p := defaultPage{} - return p.getData(), nil -} - -// matchPath checks if the given path matches the given regular expressions -// and returns a boolean as well as any parameters from regex capture groups. -func matchPath(p string, reg *regexp.Regexp) (bool, []string) { - match := notesPathRegex.FindStringSubmatch(p) - - if len(match) > 0 { - return true, match[1:] - } - - return false, nil -} diff --git a/pkg/server/tmpl/app_test.go b/pkg/server/tmpl/app_test.go deleted file mode 100644 index 7a26798f..00000000 --- a/pkg/server/tmpl/app_test.go +++ /dev/null @@ -1,87 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package tmpl - -import ( - "fmt" - "net/http" - "testing" - - "github.com/dnote/dnote/pkg/assert" - "github.com/dnote/dnote/pkg/server/database" - "github.com/dnote/dnote/pkg/server/testutils" - "github.com/pkg/errors" -) - -func TestAppShellExecute(t *testing.T) { - t.Run("home", func(t *testing.T) { - a, err := NewAppShell(testutils.DB, []byte("{{ .Title }}{{ .MetaTags }}")) - if err != nil { - t.Fatal(errors.Wrap(err, "preparing app shell")) - } - - r, err := http.NewRequest("GET", "http://mock.url/", nil) - if err != nil { - t.Fatal(errors.Wrap(err, "preparing request")) - } - - b, err := a.Execute(r) - if err != nil { - t.Fatal(errors.Wrap(err, "executing")) - } - - assert.Equal(t, string(b), "Dnote", "result mismatch") - }) - - t.Run("note", func(t *testing.T) { - defer testutils.ClearData(testutils.DB) - - user := testutils.SetupUserData() - b1 := database.Book{ - UserID: user.ID, - Label: "js", - } - testutils.MustExec(t, testutils.DB.Save(&b1), "preparing b1") - n1 := database.Note{ - UserID: user.ID, - BookUUID: b1.UUID, - Public: true, - Body: "n1 content", - } - testutils.MustExec(t, testutils.DB.Save(&n1), "preparing note") - - a, err := NewAppShell(testutils.DB, []byte("{{ .MetaTags }}")) - if err != nil { - t.Fatal(errors.Wrap(err, "preparing app shell")) - } - - endpoint := fmt.Sprintf("http://mock.url/notes/%s", n1.UUID) - r, err := http.NewRequest("GET", endpoint, nil) - if err != nil { - t.Fatal(errors.Wrap(err, "preparing request")) - } - - b, err := a.Execute(r) - if err != nil { - t.Fatal(errors.Wrap(err, "executing")) - } - - assert.NotEqual(t, string(b), "", "result should not be empty") - }) -} diff --git a/pkg/server/tmpl/data.go b/pkg/server/tmpl/data.go deleted file mode 100644 index 8c827e68..00000000 --- a/pkg/server/tmpl/data.go +++ /dev/null @@ -1,141 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package tmpl - -import ( - "bytes" - "fmt" - "html/template" - "net/http" - "regexp" - "strings" - "time" - - "github.com/dnote/dnote/pkg/server/database" - "github.com/dnote/dnote/pkg/server/middleware" - "github.com/dnote/dnote/pkg/server/operations" - "github.com/pkg/errors" -) - -var newlineRegexp = regexp.MustCompile(`\r?\n`) - -// tmplData is the data to be passed to the app shell template -type tmplData struct { - Title string - MetaTags template.HTML -} - -type noteMetaTagsData struct { - Title string - Description string -} - -type notePage struct { - Note database.Note - T *template.Template -} - -func (a AppShell) newNotePage(r *http.Request, noteUUID string) (notePage, error) { - user, _, err := middleware.AuthWithSession(a.DB, r) - if err != nil { - return notePage{}, errors.Wrap(err, "authenticating with session") - } - - note, ok, err := operations.GetNote(a.DB, noteUUID, &user) - - if !ok { - return notePage{}, ErrNotFound - } - if err != nil { - return notePage{}, errors.Wrap(err, "getting note") - } - - return notePage{note, a.T}, nil -} - -func (p notePage) getTitle() string { - note := p.Note - date := time.Unix(0, note.AddedOn).Format("Jan 2 2006") - - return fmt.Sprintf("Note: %s (%s)", note.Book.Label, date) -} - -func excerpt(s string, maxLen int) string { - if len(s) > maxLen { - - var lastIdx int - if maxLen > 3 { - lastIdx = maxLen - 3 - } else { - lastIdx = maxLen - } - - return s[:lastIdx] + "..." - } - - return s -} - -func formatMetaDescContent(s string) string { - desc := excerpt(s, 200) - desc = strings.Trim(desc, " ") - - return newlineRegexp.ReplaceAllString(desc, " ") -} - -func (p notePage) getMetaTags() (template.HTML, error) { - title := p.getTitle() - desc := formatMetaDescContent(p.Note.Body) - - data := noteMetaTagsData{ - Title: title, - Description: desc, - } - - var buf bytes.Buffer - if err := p.T.ExecuteTemplate(&buf, templateNoteMetaTags, data); err != nil { - return "", errors.Wrap(err, "executing template") - } - - return template.HTML(buf.String()), nil -} - -func (p notePage) getData() (tmplData, error) { - mt, err := p.getMetaTags() - if err != nil { - return tmplData{}, errors.Wrap(err, "getting meta tags") - } - - dat := tmplData{ - Title: p.getTitle(), - MetaTags: mt, - } - - return dat, nil -} - -type defaultPage struct { -} - -func (p defaultPage) getData() tmplData { - return tmplData{ - Title: "Dnote", - MetaTags: "", - } -} diff --git a/pkg/server/tmpl/data_test.go b/pkg/server/tmpl/data_test.go deleted file mode 100644 index b26af312..00000000 --- a/pkg/server/tmpl/data_test.go +++ /dev/null @@ -1,64 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package tmpl - -import ( - "html/template" - "testing" - "time" - - "github.com/dnote/dnote/pkg/assert" - "github.com/dnote/dnote/pkg/server/database" - "github.com/dnote/dnote/pkg/server/testutils" - "github.com/pkg/errors" -) - -func TestDefaultPageGetData(t *testing.T) { - p := defaultPage{} - - result := p.getData() - - assert.Equal(t, result.MetaTags, template.HTML(""), "MetaTags mismatch") - assert.Equal(t, result.Title, "Dnote", "Title mismatch") -} - -func TestNotePageGetData(t *testing.T) { - a, err := NewAppShell(testutils.DB, nil) - if err != nil { - t.Fatal(errors.Wrap(err, "preparing app shell")) - } - - p := notePage{ - Note: database.Note{ - Book: database.Book{ - Label: "vocabulary", - }, - AddedOn: time.Date(2019, time.January, 2, 0, 0, 0, 0, time.UTC).UnixNano(), - }, - T: a.T, - } - - result, err := p.getData() - if err != nil { - t.Fatal(errors.Wrap(err, "executing")) - } - - assert.NotEqual(t, result.MetaTags, template.HTML(""), "MetaTags should not be empty") - assert.Equal(t, result.Title, "Note: vocabulary (Jan 2 2019)", "Title mismatch") -} diff --git a/pkg/server/tmpl/main_test.go b/pkg/server/tmpl/main_test.go deleted file mode 100644 index bb664779..00000000 --- a/pkg/server/tmpl/main_test.go +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package tmpl - -import ( - "os" - "testing" - - "github.com/dnote/dnote/pkg/server/testutils" -) - -func TestMain(m *testing.M) { - testutils.InitTestDB() - - code := m.Run() - testutils.ClearData(testutils.DB) - - os.Exit(code) -} diff --git a/pkg/server/tmpl/tmpl.go b/pkg/server/tmpl/tmpl.go deleted file mode 100644 index d7cd424f..00000000 --- a/pkg/server/tmpl/tmpl.go +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package tmpl - -var noteMetaTags = ` - - - - - - - -` diff --git a/pkg/server/token/main_test.go b/pkg/server/token/main_test.go deleted file mode 100644 index f3cbe394..00000000 --- a/pkg/server/token/main_test.go +++ /dev/null @@ -1,35 +0,0 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . - */ - -package token - -import ( - "os" - "testing" - - "github.com/dnote/dnote/pkg/server/testutils" -) - -func TestMain(m *testing.M) { - testutils.InitTestDB() - - code := m.Run() - testutils.ClearData(testutils.DB) - - os.Exit(code) -} diff --git a/pkg/server/token/token.go b/pkg/server/token/token.go index 6e6b8332..af1feda5 100644 --- a/pkg/server/token/token.go +++ b/pkg/server/token/token.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package token @@ -23,7 +20,7 @@ import ( "encoding/base64" "github.com/dnote/dnote/pkg/server/database" - "github.com/jinzhu/gorm" + "gorm.io/gorm" "github.com/pkg/errors" ) diff --git a/pkg/server/token/token_test.go b/pkg/server/token/token_test.go index a8777e85..5daed181 100644 --- a/pkg/server/token/token_test.go +++ b/pkg/server/token/token_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package token @@ -33,30 +30,30 @@ func TestCreate(t *testing.T) { kind string }{ { - kind: database.TokenTypeEmailPreference, + kind: database.TokenTypeResetPassword, }, } for _, tc := range testCases { t.Run(fmt.Sprintf("token type %s", tc.kind), func(t *testing.T) { - defer testutils.ClearData(testutils.DB) + db := testutils.InitMemoryDB(t) // Set up - u := testutils.SetupUserData() + u := testutils.SetupUserData(db, "user@test.com", "password123") // Execute - tok, err := Create(testutils.DB, u.ID, tc.kind) + tok, err := Create(db, u.ID, tc.kind) if err != nil { t.Fatal(errors.Wrap(err, "performing")) } // Test - var count int - testutils.MustExec(t, testutils.DB.Model(&database.Token{}).Count(&count), "counting token") - assert.Equalf(t, count, 1, "error mismatch") + var count int64 + testutils.MustExec(t, db.Model(&database.Token{}).Count(&count), "counting token") + assert.Equalf(t, count, int64(1), "error mismatch") var tokenRecord database.Token - testutils.MustExec(t, testutils.DB.First(&tokenRecord), "finding token") + testutils.MustExec(t, db.First(&tokenRecord), "finding token") assert.Equalf(t, tokenRecord.UserID, tok.UserID, "UserID mismatch") assert.Equalf(t, tokenRecord.Value, tok.Value, "Value mismatch") assert.Equalf(t, tokenRecord.Type, tok.Type, "Type mismatch") diff --git a/pkg/server/views/data.go b/pkg/server/views/data.go index 0fa7e0b3..87fd2a90 100644 --- a/pkg/server/views/data.go +++ b/pkg/server/views/data.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package views @@ -50,9 +47,8 @@ type Alert struct { type Data struct { Alert *Alert // CSRF template.HTML - User *database.User - Account *database.Account - Yield map[string]interface{} + User *database.User + Yield map[string]interface{} } func getErrMessage(err error) string { diff --git a/pkg/server/views/embed.go b/pkg/server/views/embed.go index 47d6d55a..1fcb7d19 100644 --- a/pkg/server/views/embed.go +++ b/pkg/server/views/embed.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package views diff --git a/pkg/server/views/engine.go b/pkg/server/views/engine.go index 4a167d61..fbad7979 100644 --- a/pkg/server/views/engine.go +++ b/pkg/server/views/engine.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package views diff --git a/pkg/server/views/helpers.go b/pkg/server/views/helpers.go index 29f0862f..3e8f1d56 100644 --- a/pkg/server/views/helpers.go +++ b/pkg/server/views/helpers.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package views @@ -50,7 +47,7 @@ func initHelpers(c Config, a *app.App) template.FuncMap { "defaultValue": ctx.defaultValue, "add": ctx.add, "assetBaseURL": func() string { - return a.Config.AssetBaseURL + return a.AssetBaseURL }, } diff --git a/pkg/server/views/helpers_test.go b/pkg/server/views/helpers_test.go index deedf6f3..af39b48c 100644 --- a/pkg/server/views/helpers_test.go +++ b/pkg/server/views/helpers_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package views diff --git a/pkg/server/views/templates/books/index.gohtml b/pkg/server/views/templates/books/index.gohtml deleted file mode 100644 index 46ebbecc..00000000 --- a/pkg/server/views/templates/books/index.gohtml +++ /dev/null @@ -1,20 +0,0 @@ -{{define "yield"}} -
- - - -
- -
-
-{{end}} diff --git a/pkg/server/views/templates/books/show.gohtml b/pkg/server/views/templates/books/show.gohtml deleted file mode 100644 index 4d84d095..00000000 --- a/pkg/server/views/templates/books/show.gohtml +++ /dev/null @@ -1,4 +0,0 @@ -{{define "yield"}} - content - {{ .Note.Body }} -{{end}} diff --git a/pkg/server/views/templates/icons/book.gohtml b/pkg/server/views/templates/icons/book.gohtml deleted file mode 100644 index a04a3e68..00000000 --- a/pkg/server/views/templates/icons/book.gohtml +++ /dev/null @@ -1,17 +0,0 @@ -{{define "book"}} - - Book - Icon depicting a book - - -{{end}} diff --git a/pkg/server/views/templates/icons/caret.gohtml b/pkg/server/views/templates/icons/caret.gohtml deleted file mode 100644 index 0a9f60a0..00000000 --- a/pkg/server/views/templates/icons/caret.gohtml +++ /dev/null @@ -1,26 +0,0 @@ -{{define "caret"}} - - - - -{{end}} diff --git a/pkg/server/views/templates/layouts/navbar.gohtml b/pkg/server/views/templates/layouts/navbar.gohtml index ea7c026f..b39a1e4e 100644 --- a/pkg/server/views/templates/layouts/navbar.gohtml +++ b/pkg/server/views/templates/layouts/navbar.gohtml @@ -41,7 +41,7 @@ diff --git a/pkg/server/views/templates/notes/index.gohtml b/pkg/server/views/templates/notes/index.gohtml deleted file mode 100644 index 168430f5..00000000 --- a/pkg/server/views/templates/notes/index.gohtml +++ /dev/null @@ -1,91 +0,0 @@ -{{define "yield"}} -
-

Notes

- - {{template "pageToolbar" dict "data" . "class" "toolbar"}} - -
- {{if eq (len .NoteGroups) 0 }} -
No notes found.
- {{end}} - - {{range .NoteGroups}} - {{template "noteGroup" .}} - {{end}} -
-
-{{end}} - -{{define "noteGroup"}} -
-
-

- -

-
- -
    - {{range .Data}} - {{template "noteItem" .}} - {{end}} -
-
-{{end}} - -{{define "noteItem"}} -
  • - -
    -
    -

    - {{ .Book.Label }} -

    - - {{template "time" dict "value" .UpdatedAt "text" (timeAgo .UpdatedAt)}} -
    - -
    - {{ excerpt .Body 160 }} -
    -
    -
    -
  • -{{end}} - -{{define "pageToolbarContent"}} - -{{end}} - -{{define "pager"}} - -{{$ariaLabel := ""}} -{{if eq .direction "left"}} - {{$ariaLabel = "Previous page"}} -{{else}} - {{$ariaLabel = "Next page"}} -{{end}} - -{{if .disabled}} - - {{template "caret" dict "direction" .direction "stroke" "gray"}} - -{{else}} - - {{template "caret" dict "direction" .direction "stroke" "black"}} - -{{end}} -{{end}} diff --git a/pkg/server/views/templates/notes/show.gohtml b/pkg/server/views/templates/notes/show.gohtml deleted file mode 100644 index 7051bee4..00000000 --- a/pkg/server/views/templates/notes/show.gohtml +++ /dev/null @@ -1,33 +0,0 @@ -{{define "yield"}} -
    -
    -
    -
    -
    - {{template "book" dict "fill" "#000000"}} - -

    - - {{ .Note.Book.Label }} - -

    -
    -
    - - -
    -
    - {{ .Content }} -
    -
    - -
    -
    - Last edit: - {{ timeFormat .Note.UpdatedAt "January 02, 2006" }} -
    -
    -
    -
    -
    -{{end}} diff --git a/pkg/server/views/templates/partials/page_toolbar.gohtml b/pkg/server/views/templates/partials/page_toolbar.gohtml deleted file mode 100644 index 4d1abdfd..00000000 --- a/pkg/server/views/templates/partials/page_toolbar.gohtml +++ /dev/null @@ -1,5 +0,0 @@ -{{define "pageToolbar"}} -
    - {{template "pageToolbarContent" .data}} -
    -{{end}} diff --git a/pkg/server/views/templates/partials/time.gohtml b/pkg/server/views/templates/partials/time.gohtml deleted file mode 100644 index b05b3a86..00000000 --- a/pkg/server/views/templates/partials/time.gohtml +++ /dev/null @@ -1,13 +0,0 @@ -{{define "time"}} - -{{$mobileText := defaultValue .mobileText .text}} - - - - -{{end}} diff --git a/pkg/server/views/templates/users/email_verification.gohtml b/pkg/server/views/templates/users/email_verification.gohtml deleted file mode 100644 index 969688a4..00000000 --- a/pkg/server/views/templates/users/email_verification.gohtml +++ /dev/null @@ -1,2 +0,0 @@ -{{define "yield"}} -{{end}} diff --git a/pkg/server/views/templates/users/settings.gohtml b/pkg/server/views/templates/users/settings.gohtml index a2af6649..9f747b3e 100644 --- a/pkg/server/views/templates/users/settings.gohtml +++ b/pkg/server/views/templates/users/settings.gohtml @@ -12,9 +12,6 @@
    - {{if ne .Standalone "true"}} - {{template "planSection" .}} - {{end}} {{template "emailSection" .}} {{template "passwordSection" .}}
    @@ -147,34 +144,6 @@
    -
    -
    -
    -

    Email Verified

    -
    - -
    - {{ if eq true false }} b{{end}} - - {{if .EmailVerified}} - Yes - {{else}} - No - - - {{end}} -
    -
    -
    -
    @@ -208,34 +177,4 @@
    -{{end}} - -{{define "planSection"}} -
    -

    Plan

    - -
    -
    -
    -

    Dnote Pro

    -

    - Fully hosted and managed Dnote for you. -

    -
    - -
    - {{if .Cloud}} - Yes - {{else}} - - Unlock - - {{end}} -
    -
    - -
    -
    -{{end}} +{{end}} \ No newline at end of file diff --git a/pkg/server/views/templates/users/settings_about.gohtml b/pkg/server/views/templates/users/settings_about.gohtml index bba9a8f0..3252b9de 100644 --- a/pkg/server/views/templates/users/settings_about.gohtml +++ b/pkg/server/views/templates/users/settings_about.gohtml @@ -27,27 +27,6 @@
    - {{if ne .Standalone "true"}} -
    -
    -
    -

    Support

    -
    - -
    - {{if .User.Cloud}} - - support@getdnote.com - - {{else}} - Not eligible - {{end}} -
    -
    -
    - {{else}} - - {{end}} diff --git a/pkg/server/views/time.go b/pkg/server/views/time.go index 56efcf99..88c71169 100644 --- a/pkg/server/views/time.go +++ b/pkg/server/views/time.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package views diff --git a/pkg/server/views/view.go b/pkg/server/views/view.go index a4c67d71..53315334 100644 --- a/pkg/server/views/view.go +++ b/pkg/server/views/view.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package views @@ -108,19 +105,13 @@ func (v *View) Render(w http.ResponseWriter, r *http.Request, data *Data, status } vd.User = context.User(r.Context()) - vd.Account = context.Account(r.Context()) // Put user data in Yield if vd.Yield == nil { vd.Yield = map[string]interface{}{} } - if vd.Account != nil { - vd.Yield["Email"] = vd.Account.Email.String - vd.Yield["EmailVerified"] = vd.Account.EmailVerified - vd.Yield["EmailVerified"] = vd.Account.EmailVerified - } if vd.User != nil { - vd.Yield["Cloud"] = vd.User.Cloud + vd.Yield["Email"] = vd.User.Email.String } vd.Yield["CurrentPath"] = r.URL.Path vd.Yield["Standalone"] = buildinfo.Standalone diff --git a/pkg/watcher/main.go b/pkg/watcher/main.go index a412a31d..7509fb90 100644 --- a/pkg/watcher/main.go +++ b/pkg/watcher/main.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package main diff --git a/scripts/cli/build.sh b/scripts/cli/build.sh index bd417cdc..d17cb477 100755 --- a/scripts/cli/build.sh +++ b/scripts/cli/build.sh @@ -36,7 +36,7 @@ if [[ $1 == v* ]]; then exit 1 fi -goVersion=go-1.20.x +goVersion=go-1.25.x get_binary_name() { platform=$1 @@ -57,7 +57,7 @@ build() { # build binary destDir="$outputDir/$platform-$arch" - ldflags="-X main.apiEndpoint=https://api.getdnote.com -X github.com/dnote/dnote/pkg/server/buildinfo.Version=$version" + ldflags="-X main.apiEndpoint=https://localhost:3001/api -X main.versionTag=$version" tags="fts5" pushd "$projectDir" @@ -92,13 +92,13 @@ build() { popd binaryName=$(get_binary_name "$platform") - mv "$destDir/cli-${platform}-"* "$destDir/$binaryName" + mv "$destDir/cli-"* "$destDir/$binaryName" # build tarball tarballName="dnote_${version}_${platform}_${arch}.tar.gz" tarballPath="$outputDir/$tarballName" - cp "$projectDir/licenses/GPLv3.txt" "$destDir" + cp "$projectDir/LICENSE" "$destDir" cp "$basedir/README.md" "$destDir" tar -C "$destDir" -zcvf "$tarballPath" "." rm -rf "$destDir" @@ -113,10 +113,20 @@ if [ -z "$GOOS" ] && [ -z "$GOARCH" ]; then # install the tool go install src.techknowlogick.com/xgo@latest + # Linux build linux amd64 build linux arm64 + build linux arm + + # macOS build darwin amd64 + build darwin arm64 + + # Windows build windows amd64 + + # FreeBSD + build freebsd amd64 else build "$GOOS" "$GOARCH" true fi diff --git a/scripts/cli/dev.sh b/scripts/cli/dev.sh index ce173496..d4a23e76 100755 --- a/scripts/cli/dev.sh +++ b/scripts/cli/dev.sh @@ -6,6 +6,6 @@ dir=$(dirname "${BASH_SOURCE[0]}") sudo rm -rf "$(which dnote)" "$GOPATH/bin/cli" # change tags to darwin if on macos -go install -ldflags "-X main.apiEndpoint=http://127.0.0.1:3000/api" --tags "linux fts5" "$dir/../../pkg/cli" +go install -ldflags "-X main.apiEndpoint=http://127.0.0.1:3001/api" --tags "linux fts5" "$dir/../../pkg/cli" sudo ln -s "$GOPATH/bin/cli" /usr/local/bin/dnote diff --git a/scripts/cli/test.sh b/scripts/cli/test.sh index e66db37a..42e0ad86 100755 --- a/scripts/cli/test.sh +++ b/scripts/cli/test.sh @@ -1,6 +1,5 @@ #!/usr/bin/env bash -# test.sh runs test files sequentially -# https://stackoverflow.com/questions/23715302/go-how-to-run-tests-for-multiple-packages +# test.sh runs tests for CLI packages set -eux dir=$(dirname "${BASH_SOURCE[0]}") @@ -8,7 +7,5 @@ pushd "$dir/../../pkg/cli" # clear tmp dir in case not properly torn down rm -rf "./tmp" -go test -a ./... \ - -p 1\ - --tags "fts5" +go test ./... --tags "fts5" popd diff --git a/scripts/e2e/test.sh b/scripts/e2e/test.sh new file mode 100755 index 00000000..8713bb89 --- /dev/null +++ b/scripts/e2e/test.sh @@ -0,0 +1,9 @@ +#!/bin/bash +set -eux + +dir=$(dirname "${BASH_SOURCE[0]}") +basePath=$(realpath "$dir/../../") + +pushd "$basePath"/pkg/e2e +go test --tags "fts5" ./... -v -timeout 5m +popd diff --git a/scripts/generate-changelog.sh b/scripts/generate-changelog.sh new file mode 100755 index 00000000..8f24a8ec --- /dev/null +++ b/scripts/generate-changelog.sh @@ -0,0 +1,62 @@ +#!/bin/bash +set -e + +# Usage: ./generate-changelog.sh +# Example: ./generate-changelog.sh cli cli-v0.15.2 cli-v0.15.1 + +COMPONENT=$1 +CURRENT_TAG=$2 +PREV_TAG=$3 + +if [ -z "$COMPONENT" ] || [ -z "$CURRENT_TAG" ]; then + echo "Usage: $0 [previous-tag]" + echo "Example: $0 cli cli-v0.15.2 cli-v0.15.1" + exit 1 +fi + +# Validate that tags match the component +if [[ ! "$CURRENT_TAG" =~ ^${COMPONENT}- ]]; then + echo "Error: Current tag '$CURRENT_TAG' doesn't match component '$COMPONENT'" + echo "Expected tag to start with '${COMPONENT}-'" + exit 1 +fi + +if [ -n "$PREV_TAG" ] && [[ ! "$PREV_TAG" =~ ^${COMPONENT}- ]]; then + echo "Error: Previous tag '$PREV_TAG' doesn't match component '$COMPONENT'" + echo "Expected tag to start with '${COMPONENT}-'" + exit 1 +fi + +# Define paths for each component +# Shared paths that apply to both components +SHARED_PATHS="pkg/dirs/" + +if [ "$COMPONENT" == "cli" ]; then + FILTER_PATHS="pkg/cli/ cmd/cli/ $SHARED_PATHS" +elif [ "$COMPONENT" == "server" ]; then + FILTER_PATHS="pkg/server/ host/ $SHARED_PATHS" +else + echo "Unknown component: $COMPONENT" + echo "Valid components: cli, server" + exit 1 +fi + +# Determine commit range +if [ -z "$PREV_TAG" ]; then + echo "Error: No previous tag specified" + exit 1 +fi + +RANGE="$PREV_TAG..$CURRENT_TAG" + +# Get all commits that touched the relevant paths +# Warnings go to stderr (visible in logs), commits go to stdout (captured for file) +COMMITS=$( + for path in $FILTER_PATHS; do + git log --oneline --no-merges --pretty=format:"- %s%n" "$RANGE" -- "$path" 2>&2 + done | sort -u | grep -v "^$" +) + +echo "## What's Changed" +echo "" +echo "$COMMITS" diff --git a/scripts/license.sh b/scripts/license.sh index 1b9f7a1a..4f7788e4 100755 --- a/scripts/license.sh +++ b/scripts/license.sh @@ -1,75 +1,46 @@ #!/usr/bin/env bash set -eux -function remove_notice { - sed -i -e '/\/\* Copyright/,/\*\//d' "$1" - - # remove leading newline - sed -i '/./,$!d' "$1" +function has_license { + # Check if file already has a copyright notice + grep -q "Copyright.*Dnote Authors" "$1" } function add_notice { ed "$1" <. - */" - -agpl="/* Copyright (C) 2019, 2020, 2021, 2022, 2023 Monomax Software Pty Ltd - * - * This file is part of Dnote. - * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an \"AS IS\" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */" dir=$(dirname "${BASH_SOURCE[0]}") basedir="$dir/.." pkgPath="$basedir/pkg" -serverPath="$basedir/pkg/server" -gplFiles=$(find "$pkgPath" -type f \( -name "*.go" -o -name "*.js" -o -name "*.ts" -o -name "*.tsx" -o -name "*.scss" -o -name "*.css" \) ! -path "**/vendor/*" ! -path "**/node_modules/*" ! -path "$serverPath/*") +# Apply license to all source files +allFiles=$(find "$pkgPath" -type f \( -name "*.go" -o -name "*.js" -o -name "*.ts" -o -name "*.tsx" -o -name "*.scss" -o -name "*.css" \) ! -path "**/vendor/*" ! -path "**/node_modules/*" ! -path "**/dist/*") -for file in $gplFiles; do - remove_notice "$file" - add_notice "$file" "$gpl" -done - -agplFiles=$(find "$serverPath" -type f \( -name "*.go" -o -name "*.js" -o -name "*.ts" -o -name "*.tsx" -o -name "*.scss" -o -name "*.css" \) ! -path "**/vendor/*" ! -path "**/node_modules/*" ! -path "**/dist/*") - -for file in $agplFiles; do - remove_notice "$file" - add_notice "$file" "$agpl" +for file in $allFiles; do + if ! has_license "$file"; then + add_notice "$file" "$license" + fi done diff --git a/scripts/release.sh b/scripts/release.sh deleted file mode 100755 index c4a7d638..00000000 --- a/scripts/release.sh +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env bash -# -# release.sh releases the tarballs and checksum in the build directory -# to GitHub and brew. A prerequisite is to build those files using build.sh. -# use: ./scripts/release.sh cli v0.4.8 path/to/assets - -set -euxo pipefail - -project=$1 -version=$2 -assetPath=$3 - -if [ "$project" != "cli" ] && [ "$project" != "server" ]; then - echo "unrecognized project '$project'" - exit 1 -fi -if [ -z "$version" ]; then - echo "no version specified." - exit 1 -fi -if [[ $version == v* ]]; then - echo "do not prefix version with v" - exit 1 -fi - -# 1. push tag -version_tag="$project-v$version" - -echo "* tagging and pushing the tag" -git tag -a "$version_tag" -m "Release $version_tag" -git push --tags - -# 2. release on GitHub -files=("$assetPath"/*) -file_flags=() -for file in "${files[@]}"; do - file_flags+=("--attach=$file") -done - -# mark as prerelease if version is not in a form of major.minor.patch -# e.g. 1.0.1-beta.1 -flags=() -if [[ ! "$version" =~ ^[0-9]+.[0-9]+.[0-9]+$ ]]; then - flags+=("--prerelease") -fi - -echo "* creating release" -set -x - -# first message is the title and the following are body in markdown -hub release create \ - "${file_flags[@]}" \ - "${flags[@]}" \ - --message="$version_tag"\ - --message="Please see the [CHANGELOG](https://github.com/dnote/dnote/blob/master/CHANGELOG.md)" \ - "$version_tag" diff --git a/scripts/server/build.sh b/scripts/server/build.sh index cae626e4..09bc16e3 100755 --- a/scripts/server/build.sh +++ b/scripts/server/build.sh @@ -29,28 +29,35 @@ build() { platform=$1 arch=$2 - pushd "$basedir" - destDir="$outputDir/$platform-$arch" mkdir -p "$destDir" # build binary moduleName="github.com/dnote/dnote" ldflags="-X '$moduleName/pkg/server/buildinfo.CSSFiles=main.css' -X '$moduleName/pkg/server/buildinfo.JSFiles=main.js' -X '$moduleName/pkg/server/buildinfo.Version=$version' -X '$moduleName/pkg/server/buildinfo.Standalone=true'" + tags="fts5" - GOOS="$platform" \ - GOARCH="$arch" go build \ - -o "$destDir/dnote-server" \ + pushd "$projectDir" + + xgo \ + -go go-1.25.x \ + -targets="$platform/$arch" \ -ldflags "$ldflags" \ - "$basedir"/*.go + -dest="$destDir" \ + -out="server" \ + -tags "$tags" \ + -pkg pkg/server \ + . popd + mv "$destDir/server-${platform}"* "$destDir/dnote-server" + # build tarball tarballName="dnote_server_${version}_${platform}_${arch}.tar.gz" tarballPath="$outputDir/$tarballName" - cp "$projectDir/licenses/AGPLv3.txt" "$destDir" + cp "$projectDir/LICENSE" "$destDir" cp "$basedir/README.md" "$destDir" tar -C "$destDir" -zcvf "$tarballPath" "." rm -rf "$destDir" @@ -62,5 +69,11 @@ build() { } +# install the tool +go install src.techknowlogick.com/xgo@latest + build linux amd64 build linux arm64 +build linux arm +build linux 386 +build freebsd amd64 diff --git a/scripts/server/dev.sh b/scripts/server/dev.sh index 11f1e207..3786f270 100755 --- a/scripts/server/dev.sh +++ b/scripts/server/dev.sh @@ -7,11 +7,8 @@ dir=$(dirname "${BASH_SOURCE[0]}") basePath="$dir/../.." serverPath="$basePath/pkg/server" -# load env -set -a -dotenvPath="$serverPath/.env.dev" -source "$dotenvPath" -set +a +# Set env +DBPath=../../dev-server.db # copy assets mkdir -p "$basePath/pkg/server/static" @@ -23,7 +20,7 @@ cp "$basePath"/pkg/server/assets/static/* "$basePath/pkg/server/static" # run server moduleName="github.com/dnote/dnote" ldflags="-X '$moduleName/pkg/server/buildinfo.CSSFiles=main.css' -X '$moduleName/pkg/server/buildinfo.JSFiles=main.js' -X '$moduleName/pkg/server/buildinfo.Version=dev' -X '$moduleName/pkg/server/buildinfo.Standalone=true'" -task="go run -ldflags \"$ldflags\" main.go start -port 3000" +task="go run -ldflags \"$ldflags\" --tags fts5 main.go start -port 3001" ( cd "$basePath/pkg/watcher" && \ diff --git a/scripts/server/test-local.sh b/scripts/server/test-local.sh index ce50d7d6..504ef444 100755 --- a/scripts/server/test-local.sh +++ b/scripts/server/test-local.sh @@ -5,8 +5,4 @@ set -ex dir=$(dirname "${BASH_SOURCE[0]}") -set -a -source "$dir/../../pkg/server/.env.test" -set +a - "$dir/test.sh" "$1" diff --git a/scripts/server/test.sh b/scripts/server/test.sh index ed1d0a41..10207010 100755 --- a/scripts/server/test.sh +++ b/scripts/server/test.sh @@ -8,9 +8,9 @@ pushd "$dir/../../pkg/server" function run_test { if [ -z "$1" ]; then - go test ./... -cover -p 1 + go test -tags "fts5" ./... -cover else - go test -run "$1" -cover -p 1 + go test -tags "fts5" -run "$1" -cover fi } diff --git a/scripts/vagrant/bootstrap.sh b/scripts/vagrant/bootstrap.sh deleted file mode 100755 index b8299676..00000000 --- a/scripts/vagrant/bootstrap.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env bash -set -ex - -echo "export DNOTE=/go/src/github.com/dnote/dnote" >> /home/vagrant/.bash_profile -echo "cd /go/src/github.com/dnote/dnote" >> /home/vagrant/.bash_profile - -# install dependencies -(cd /go/src/github.com/dnote/dnote && make install) - -# set up database -sudo -u postgres createdb dnote -sudo -u postgres createdb dnote_test -sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';" - -# allow connection from host and allow to connect without password -sudo sed -i "/port*/a listen_addresses = '*'" /etc/postgresql/14/main/postgresql.conf -sudo sed -i 's/host.*all.*.all.*md5/# &/' /etc/postgresql/14/main/pg_hba.conf -sudo sed -i "$ a host all all all trust" /etc/postgresql/14/main/pg_hba.conf -sudo service postgresql restart diff --git a/scripts/vagrant/install_go.sh b/scripts/vagrant/install_go.sh deleted file mode 100755 index b6b9de23..00000000 --- a/scripts/vagrant/install_go.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env bash -# shellcheck disable=SC1091 -set -eux - -VERSION=1.20 -OS=linux -ARCH=amd64 - -tarball=go$VERSION.$OS-$ARCH.tar.gz - -wget -q https://dl.google.com/go/"$tarball" -sudo tar -C /usr/local -xzf "$tarball" -sudo tar -xf "$tarball" - -sudo mkdir -p /go/src -sudo mkdir -p /go/bin -sudo mkdir -p /go/pkg -sudo chown -R vagrant:vagrant /go - -GOPATH=/go -echo "export GOPATH=$GOPATH" >> /home/vagrant/.bash_profile -echo "export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin" >> /home/vagrant/.bash_profile -source /home/vagrant/.bash_profile - -go version -go env diff --git a/scripts/vagrant/install_node.sh b/scripts/vagrant/install_node.sh deleted file mode 100755 index 1233d449..00000000 --- a/scripts/vagrant/install_node.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env bash -# shellcheck disable=SC1090,SC1091 -set -eux - -VERSION=12.16.2 -NVM_VERSION=v0.35.0 - -# Install nvm -wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/"$NVM_VERSION"/install.sh | bash -cat >> /home/vagrant/.bash_profile<< EOF -export NVM_DIR="\$([ -z "\${XDG_CONFIG_HOME-}" ] && printf %s "\${HOME}/.nvm" || printf %s "\${XDG_CONFIG_HOME}/nvm")" -[ -s "\$NVM_DIR/nvm.sh" ] && \. "\$NVM_DIR/nvm.sh" # This loads nvm -EOF -source /home/vagrant/.bash_profile - -# Install a node and alias -nvm install --no-progress "$VERSION" 1>/dev/null -nvm alias default "$VERSION" -nvm use default diff --git a/scripts/vagrant/install_postgres.sh b/scripts/vagrant/install_postgres.sh deleted file mode 100755 index f2738021..00000000 --- a/scripts/vagrant/install_postgres.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash -set -ex - -sudo apt-get -y install wget ca-certificates -wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - -sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list' - -sudo apt-get update -sudo apt-get install -y postgresql-14 diff --git a/scripts/vagrant/install_utils.sh b/scripts/vagrant/install_utils.sh deleted file mode 100755 index 46322005..00000000 --- a/scripts/vagrant/install_utils.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash -set -eux - -sudo apt-get update -sudo apt-get install -y htop git wget build-essential inotify-tools - -# Install Chrome -wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list -sudo apt-get -y update -sudo apt-get install -y google-chrome-stable - -# Install dart-sass -dart_version=1.34.1 -dart_tarball="dart-sass-$dart_version-linux-x64.tar.gz" -wget -q "https://github.com/sass/dart-sass/releases/download/$dart_version/$dart_tarball" -tar -xvzf "$dart_tarball" -C /tmp/ -sudo install /tmp/dart-sass/sass /usr/bin