Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

23.9 KB
Raw Blame
package handlers_test

import (
	"bytes"
	"context"
	"image"
	"io"
	"net/http"
	"runtime"
	"testing"

	"github.com/getfider/fider/app"
	"github.com/getfider/fider/app/models/cmd"
	"github.com/getfider/fider/app/models/dto"
	"github.com/getfider/fider/app/models/entity"
	"github.com/getfider/fider/app/models/query"
	"github.com/getfider/fider/app/pkg/bus"

	"github.com/getfider/fider/app/pkg/mock"

	"github.com/getfider/fider/app/handlers"
	. "github.com/getfider/fider/app/pkg/assert"
)

func TestGravatarHandler(t *testing.T) {
	RegisterT(t)
	bus.Init()

	server := mock.NewServer()

	user := &entity.User{
		ID:     3,
		Name:   "Darth Vader",
		Email:  "[email protected]",
		Tenant: mock.DemoTenant,
	}

	bus.AddHandler(func(ctx context.Context, q *query.GetUserByID) error {
		if q.UserID == user.ID {
			q.Result = user
			return nil
		}
		return app.ErrNotFound
	})

	expectedAvatar := []byte{255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 1, 0, 96, 0, 96, 0, 0, 255, 254, 0, 59, 67, 82, 69, 65, 84, 79, 82, 58, 32, 103, 100, 45, 106, 112, 101, 103, 32, 118, 49, 46, 48, 32, 40, 117, 115, 105, 110, 103, 32, 73, 74, 71, 32, 74, 80, 69, 71, 32, 118, 54, 50, 41, 44, 32, 113, 117, 97, 108, 105, 116, 121, 32, 61, 32, 57, 48, 10, 255, 219, 0, 67, 0, 3, 2, 2, 3, 2, 2, 3, 3, 3, 3, 4, 3, 3, 4, 5, 8, 5, 5, 4, 4, 5, 10, 7, 7, 6, 8, 12, 10, 12, 12, 11, 10, 11, 11, 13, 14, 18, 16, 13, 14, 17, 14, 11, 11, 16, 22, 16, 17, 19, 20, 21, 21, 21, 12, 15, 23, 24, 22, 20, 24, 18, 20, 21, 20, 255, 219, 0, 67, 1, 3, 4, 4, 5, 4, 5, 9, 5, 5, 9, 20, 13, 11, 13, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 255, 192, 0, 17, 8, 0, 50, 0, 50, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, 255, 196, 0, 31, 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 255, 196, 0, 181, 16, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125, 1, 2, 3, 0, 4, 17, 5, 18, 33, 49, 65, 6, 19, 81, 97, 7, 34, 113, 20, 50, 129, 145, 161, 8, 35, 66, 177, 193, 21, 82, 209, 240, 36, 51, 98, 114, 130, 9, 10, 22, 23, 24, 25, 26, 37, 38, 39, 40, 41, 42, 52, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 131, 132, 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162, 163, 164, 165, 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184, 185, 186, 194, 195, 196, 197, 198, 199, 200, 201, 202, 210, 211, 212, 213, 214, 215, 216, 217, 218, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 255, 196, 0, 31, 1, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 255, 196, 0, 181, 17, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119, 0, 1, 2, 3, 17, 4, 5, 33, 49, 6, 18, 65, 81, 7, 97, 113, 19, 34, 50, 129, 8, 20, 66, 145, 161, 177, 193, 9, 35, 51, 82, 240, 21, 98, 114, 209, 10, 22, 36, 52, 225, 37, 241, 23, 24, 25, 26, 38, 39, 40, 41, 42, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 130, 131, 132, 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162, 163, 164, 165, 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184, 185, 186, 194, 195, 196, 197, 198, 199, 200, 201, 202, 210, 211, 212, 213, 214, 215, 216, 217, 218, 226, 227, 228, 229, 230, 231, 232, 233, 234, 242, 243, 244, 245, 246, 247, 248, 249, 250, 255, 218, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0, 252, 208, 109, 22, 21, 35, 134, 31, 70, 171, 7, 70, 180, 134, 217, 167, 148, 186, 162, 142, 216, 57, 62, 149, 245, 7, 236, 7, 225, 255, 0, 3, 235, 159, 181, 54, 155, 99, 227, 221, 31, 79, 214, 180, 137, 244, 107, 166, 182, 179, 213, 32, 89, 160, 123, 133, 101, 32, 148, 111, 148, 144, 130, 67, 200, 237, 94, 237, 255, 0, 5, 55, 248, 125, 240, 166, 199, 194, 254, 23, 210, 126, 29, 120, 111, 194, 158, 24, 214, 165, 214, 86, 43, 171, 171, 8, 161, 179, 45, 7, 145, 35, 54, 66, 224, 20, 12, 83, 45, 142, 14, 209, 223, 158, 181, 23, 102, 212, 79, 38, 50, 189, 175, 61, 207, 139, 79, 193, 22, 211, 52, 49, 168, 222, 70, 100, 114, 155, 150, 44, 0, 188, 142, 6, 113, 147, 212, 87, 153, 234, 146, 105, 246, 183, 205, 107, 13, 150, 226, 173, 183, 60, 18, 79, 229, 95, 98, 107, 215, 22, 154, 159, 195, 216, 109, 172, 47, 109, 239, 110, 108, 161, 141, 46, 166, 14, 171, 26, 72, 171, 144, 75, 19, 141, 167, 110, 115, 158, 1, 175, 155, 188, 61, 225, 157, 34, 63, 27, 91, 203, 169, 106, 250, 125, 213, 177, 144, 52, 130, 194, 79, 63, 96, 207, 94, 196, 143, 112, 42, 171, 69, 69, 46, 84, 20, 36, 229, 204, 230, 246, 41, 232, 62, 13, 176, 215, 33, 127, 147, 202, 184, 143, 5, 226, 145, 48, 64, 61, 8, 246, 226, 174, 205, 240, 222, 209, 115, 242, 196, 6, 51, 209, 191, 198, 190, 148, 253, 157, 60, 33, 224, 79, 27, 254, 220, 94, 30, 240, 142, 161, 104, 186, 255, 0, 132, 245, 13, 42, 224, 75, 228, 207, 44, 11, 184, 91, 188, 170, 217, 141, 149, 134, 26, 53, 227, 61, 249, 232, 107, 237, 31, 136, 63, 177, 79, 192, 157, 58, 127, 16, 90, 91, 217, 234, 176, 95, 52, 72, 208, 152, 245, 89, 10, 88, 22, 220, 78, 55, 19, 158, 54, 156, 62, 227, 200, 233, 154, 170, 113, 83, 124, 188, 183, 102, 85, 28, 161, 21, 62, 107, 38, 126, 73, 31, 135, 54, 89, 60, 71, 255, 0, 143, 255, 0, 241, 84, 87, 215, 151, 223, 7, 62, 7, 88, 94, 220, 90, 203, 241, 42, 225, 100, 130, 70, 141, 149, 175, 109, 242, 8, 56, 32, 252, 158, 212, 87, 79, 176, 143, 101, 247, 163, 155, 219, 84, 238, 255, 0, 19, 205, 191, 103, 15, 15, 105, 55, 94, 34, 208, 245, 123, 251, 120, 228, 54, 98, 235, 37, 191, 136, 48, 217, 183, 233, 130, 127, 58, 249, 231, 227, 79, 140, 135, 196, 79, 137, 218, 189, 214, 157, 30, 52, 209, 114, 214, 250, 117, 180, 35, 32, 66, 172, 66, 144, 7, 118, 251, 199, 221, 171, 209, 172, 188, 93, 39, 132, 62, 21, 106, 23, 16, 55, 151, 113, 44, 82, 65, 17, 28, 16, 206, 197, 114, 61, 192, 36, 254, 21, 224, 22, 171, 59, 200, 222, 70, 242, 193, 73, 98, 157, 64, 239, 92, 181, 231, 238, 40, 47, 83, 191, 7, 13, 93, 71, 232, 143, 65, 211, 116, 171, 47, 14, 248, 119, 196, 218, 6, 187, 164, 94, 191, 137, 175, 237, 160, 125, 36, 91, 190, 244, 183, 148, 77, 27, 49, 145, 81, 207, 204, 98, 243, 151, 4, 18, 11, 12, 129, 219, 145, 186, 176, 146, 198, 198, 218, 237, 36, 48, 221, 70, 251, 30, 22, 148, 9, 81, 178, 72, 33, 65, 220, 6, 0, 228, 129, 201, 175, 88, 248, 60, 124, 123, 240, 239, 81, 178, 241, 7, 135, 102, 151, 68, 213, 44, 90, 89, 146, 107, 80, 209, 92, 148, 49, 68, 197, 26, 69, 231, 107, 35, 18, 20, 231, 230, 66, 8, 234, 43, 176, 248, 203, 127, 175, 248, 219, 192, 218, 142, 179, 226, 89, 227, 182, 146, 105, 197, 200, 148, 5, 129, 175, 223, 4, 1, 229, 182, 54, 149, 57, 220, 0, 198, 87, 3, 147, 129, 199, 110, 167, 99, 169, 202, 237, 189, 206, 55, 246, 122, 248, 156, 254, 26, 248, 235, 224, 31, 16, 222, 72, 98, 134, 59, 163, 167, 93, 77, 19, 20, 104, 214, 117, 104, 89, 242, 58, 0, 178, 110, 255, 0, 128, 176, 198, 43, 221, 127, 106, 191, 24, 120, 226, 223, 90, 213, 180, 221, 23, 196, 215, 226, 207, 88, 185, 243, 111, 55, 204, 76, 153, 57, 4, 7, 234, 170, 70, 62, 81, 233, 95, 18, 216, 200, 166, 70, 138, 71, 217, 28, 188, 111, 207, 221, 111, 225, 111, 207, 244, 38, 190, 175, 215, 124, 111, 31, 196, 79, 12, 104, 222, 36, 119, 18, 220, 221, 219, 5, 187, 25, 229, 46, 211, 11, 48, 62, 153, 111, 156, 15, 238, 200, 149, 217, 66, 87, 139, 129, 201, 139, 135, 43, 141, 69, 208, 241, 101, 248, 79, 1, 81, 230, 95, 49, 147, 31, 55, 29, 251, 209, 91, 242, 106, 114, 9, 27, 25, 198, 79, 122, 43, 79, 103, 15, 229, 56, 189, 189, 95, 230, 99, 237, 180, 65, 226, 207, 1, 197, 167, 25, 68, 82, 63, 207, 28, 135, 160, 96, 73, 25, 246, 237, 248, 215, 157, 234, 191, 12, 60, 83, 160, 193, 113, 52, 154, 116, 166, 209, 20, 249, 147, 219, 184, 116, 42, 57, 201, 193, 233, 192, 60, 250, 87, 93, 163, 248, 198, 203, 195, 158, 31, 178, 123, 169, 137, 148, 198, 25, 97, 143, 150, 111, 240, 252, 107, 151, 241, 151, 197, 77, 91, 197, 214, 198, 199, 119, 216, 244, 204, 228, 219, 198, 114, 100, 199, 77, 237, 223, 233, 210, 178, 171, 236, 218, 187, 220, 235, 195, 42, 209, 147, 138, 94, 237, 250, 254, 135, 29, 230, 29, 155, 113, 207, 173, 122, 207, 194, 31, 131, 62, 37, 248, 233, 125, 105, 109, 110, 183, 115, 233, 214, 99, 201, 150, 241, 137, 116, 182, 140, 100, 129, 150, 56, 85, 30, 157, 6, 106, 15, 13, 248, 3, 80, 127, 3, 201, 226, 22, 182, 176, 212, 65, 140, 249, 112, 92, 196, 204, 219, 71, 28, 21, 32, 231, 142, 249, 170, 222, 34, 212, 60, 75, 240, 235, 67, 58, 61, 166, 169, 62, 159, 101, 168, 160, 123, 155, 123, 55, 104, 210, 77, 193, 88, 119, 207, 66, 1, 245, 197, 114, 242, 53, 103, 37, 161, 218, 234, 41, 222, 48, 122, 152, 31, 19, 124, 4, 159, 14, 188, 85, 121, 163, 166, 177, 101, 172, 136, 28, 175, 157, 103, 32, 113, 244, 56, 36, 2, 61, 50, 107, 91, 225, 183, 137, 219, 78, 210, 238, 244, 251, 141, 74, 222, 43, 39, 151, 206, 142, 214, 81, 135, 18, 96, 2, 202, 196, 96, 2, 48, 8, 207, 59, 87, 142, 5, 112, 173, 101, 112, 108, 77, 217, 140, 249, 2, 65, 30, 243, 221, 136, 39, 31, 144, 254, 85, 90, 136, 203, 150, 92, 200, 185, 67, 218, 67, 146, 76, 245, 201, 47, 148, 187, 16, 220, 18, 113, 131, 69, 121, 38, 226, 59, 209, 93, 31, 88, 242, 56, 190, 164, 191, 155, 240, 31, 112, 197, 156, 100, 147, 133, 3, 159, 165, 22, 192, 27, 136, 129, 25, 5, 199, 7, 235, 69, 21, 200, 122, 75, 100, 125, 193, 225, 251, 104, 96, 240, 21, 218, 69, 18, 70, 131, 204, 1, 81, 64, 3, 138, 242, 223, 143, 86, 241, 61, 198, 149, 186, 36, 111, 244, 59, 115, 202, 143, 238, 37, 20, 87, 167, 91, 248, 127, 113, 243, 244, 63, 137, 243, 60, 227, 196, 240, 199, 31, 194, 242, 85, 21, 79, 246, 186, 12, 129, 142, 60, 134, 175, 52, 162, 138, 243, 165, 208, 246, 168, 252, 33, 69, 20, 84, 155, 159, 255, 217}
	var requestedURL string
	bus.AddHandler(func(ctx context.Context, c *cmd.HTTPRequest) error {
		requestedURL = c.URL
		c.ResponseStatusCode = http.StatusOK
		c.ResponseBody = expectedAvatar
		return nil
	})

	code, response := server.
		WithURL("https://demo.test.fider.io/?size=50").
		OnTenant(mock.DemoTenant).
		AddParam("id", user.ID).
		AddParam("name", user.Name).
		Execute(handlers.Gravatar())

	Expect(code).Equals(http.StatusOK)
	Expect(response.Body.Bytes()).Equals(expectedAvatar)
	Expect(response.Header().Get("Content-Type")).Equals("image/jpeg")
	Expect(requestedURL).Equals("https://www.gravatar.com/avatar/6565d6375c9b79d78458bbbafb4513b7?s=50&d=404")
}

func TestGravatarNotFound_LetterAvatarHandler(t *testing.T) {
	RegisterT(t)
	bus.Init()

	server := mock.NewServer()

	user := &entity.User{
		ID:     3,
		Name:   "Darth Vader",
		Email:  "[email protected]",
		Tenant: mock.DemoTenant,
	}

	bus.AddHandler(func(ctx context.Context, q *query.GetUserByID) error {
		if q.UserID == user.ID {
			q.Result = user
			return nil
		}
		return app.ErrNotFound
	})

	bus.AddHandler(func(ctx context.Context, c *cmd.HTTPRequest) error {
		c.ResponseStatusCode = http.StatusNotFound
		return nil
	})

	code, response := server.
		OnTenant(mock.DemoTenant).
		WithURL("https://demo.test.fider.io/?size=50").
		AddParam("id", user.ID).
		AddParam("name", user.Name).
		Execute(handlers.Gravatar())

	expectedAvatar := []byte{137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 50, 0, 0, 0, 50, 8, 2, 0, 0, 0, 145, 93, 31, 230, 0, 0, 3, 6, 73, 68, 65, 84, 120, 156, 236, 152, 75, 79, 19, 81, 20, 128, 207, 204, 212, 210, 18, 90, 232, 211, 34, 196, 40, 6, 65, 129, 0, 209, 2, 11, 100, 97, 140, 113, 225, 2, 127, 3, 198, 196, 184, 55, 38, 38, 36, 198, 132, 168, 11, 149, 152, 24, 35, 97, 99, 220, 24, 23, 46, 140, 186, 49, 38, 242, 42, 20, 10, 72, 241, 149, 64, 75, 12, 125, 208, 86, 104, 7, 90, 103, 134, 206, 29, 211, 14, 25, 154, 66, 116, 46, 96, 101, 113, 191, 213, 225, 204, 153, 123, 191, 204, 185, 15, 82, 205, 197, 87, 157, 176, 255, 160, 255, 183, 192, 246, 16, 45, 28, 136, 22, 14, 68, 11, 7, 162, 133, 3, 209, 194, 129, 104, 225, 64, 180, 112, 32, 90, 56, 104, 212, 20, 117, 84, 158, 57, 119, 248, 172, 242, 103, 50, 157, 138, 164, 34, 19, 97, 143, 55, 54, 171, 36, 105, 138, 190, 209, 114, 93, 199, 20, 45, 115, 203, 15, 39, 31, 109, 29, 228, 90, 211, 213, 131, 197, 118, 1, 173, 223, 113, 223, 75, 163, 244, 30, 104, 89, 245, 150, 26, 243, 241, 188, 228, 133, 35, 231, 125, 9, 255, 125, 79, 111, 52, 21, 5, 0, 36, 161, 53, 97, 181, 177, 162, 1, 0, 94, 124, 127, 25, 74, 134, 243, 70, 232, 168, 108, 7, 0, 119, 120, 226, 175, 78, 216, 77, 124, 58, 211, 255, 192, 211, 251, 120, 250, 201, 251, 31, 31, 214, 81, 186, 170, 244, 104, 79, 251, 109, 147, 206, 36, 63, 29, 12, 12, 203, 65, 75, 121, 75, 222, 139, 173, 229, 173, 27, 53, 139, 67, 106, 38, 194, 211, 154, 138, 76, 143, 134, 220, 31, 23, 7, 251, 102, 250, 187, 71, 110, 241, 34, 111, 212, 26, 174, 52, 116, 201, 79, 63, 69, 103, 18, 60, 155, 145, 112, 56, 243, 94, 108, 203, 138, 38, 215, 147, 147, 145, 169, 189, 215, 202, 197, 23, 247, 189, 158, 127, 3, 0, 205, 246, 102, 171, 222, 42, 247, 209, 21, 116, 1, 192, 177, 178, 42, 139, 222, 162, 84, 154, 117, 166, 234, 178, 106, 0, 24, 13, 185, 213, 116, 112, 183, 59, 209, 21, 26, 5, 0, 138, 2, 167, 227, 148, 156, 217, 236, 163, 227, 180, 82, 230, 116, 56, 41, 74, 126, 170, 170, 131, 187, 213, 10, 174, 133, 228, 192, 166, 183, 201, 193, 92, 124, 62, 156, 92, 202, 106, 109, 246, 177, 181, 60, 19, 71, 127, 197, 190, 254, 252, 86, 8, 45, 36, 33, 94, 20, 0, 160, 180, 168, 84, 73, 14, 7, 71, 0, 160, 214, 92, 99, 212, 26, 0, 192, 168, 53, 156, 48, 215, 102, 242, 129, 17, 245, 35, 239, 246, 56, 213, 208, 153, 35, 70, 16, 121, 37, 35, 239, 53, 154, 162, 157, 217, 15, 230, 116, 56, 105, 42, 51, 203, 192, 226, 96, 129, 180, 76, 58, 19, 147, 157, 50, 198, 45, 43, 201, 80, 50, 60, 31, 247, 41, 189, 147, 247, 160, 63, 177, 16, 88, 11, 22, 72, 171, 222, 114, 82, 14, 230, 86, 230, 114, 243, 242, 210, 174, 183, 212, 217, 139, 109, 117, 214, 186, 220, 173, 240, 207, 181, 180, 140, 246, 82, 117, 39, 0, 172, 112, 241, 220, 91, 40, 187, 188, 92, 162, 132, 24, 154, 185, 220, 208, 197, 80, 52, 146, 208, 112, 1, 180, 40, 138, 170, 53, 215, 116, 183, 221, 172, 40, 57, 4, 0, 125, 222, 126, 36, 161, 220, 2, 150, 103, 189, 81, 47, 0, 52, 218, 50, 119, 209, 108, 236, 115, 156, 79, 96, 77, 161, 234, 78, 84, 184, 219, 209, 35, 73, 168, 228, 64, 9, 67, 51, 0, 32, 34, 241, 217, 151, 231, 158, 165, 201, 173, 149, 3, 129, 161, 38, 123, 227, 70, 140, 179, 216, 119, 162, 37, 239, 121, 0, 224, 69, 97, 44, 228, 126, 235, 127, 231, 79, 44, 108, 91, 57, 30, 158, 224, 210, 156, 78, 163, 227, 210, 220, 88, 120, 28, 87, 139, 82, 243, 179, 155, 81, 107, 80, 78, 38, 9, 128, 21, 88, 54, 123, 247, 253, 25, 123, 177, 189, 136, 209, 242, 162, 16, 73, 69, 112, 181, 84, 125, 45, 86, 88, 101, 133, 85, 220, 161, 119, 96, 163, 176, 79, 255, 59, 37, 90, 56, 16, 45, 28, 136, 22, 14, 68, 11, 7, 162, 133, 3, 209, 194, 129, 104, 225, 240, 59, 0, 0, 255, 255, 193, 108, 28, 164, 14, 189, 96, 131, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130}
	Expect(code).Equals(http.StatusOK)
	Expect(response.Header().Get("Content-Type")).Equals("image/png")
	body, _ := io.ReadAll(response.Body)
	expectSamePixels(body, expectedAvatar)
}

func mockBlob(content []byte, contentType string) {
	bus.AddHandler(func(ctx context.Context, q *query.GetBlobByKey) error {
		q.Result = &dto.Blob{
			Content:     content,
			ContentType: contentType,
			Size:        int64(len(content)),
		}
		return nil
	})
}

func responseDimensions(body []byte) (int, int) {
	cfg, _, err := image.DecodeConfig(bytes.NewReader(body))
	Expect(err).IsNil()
	return cfg.Width, cfg.Height
}

// PNG compression output changes between Go releases, so compare decoded pixels rather than bytes
func expectSamePixels(actual, expected []byte) {
	a, format, err := image.Decode(bytes.NewReader(actual))
	Expect(err).IsNil()
	e, _, expectedErr := image.Decode(bytes.NewReader(expected))
	Expect(expectedErr).IsNil()
	if err != nil || expectedErr != nil {
		return
	}
	Expect(format).Equals("png")
	Expect(a.Bounds()).Equals(e.Bounds())

	// Compare color values, not color.Color types (RGB and RGBA PNGs decode to different types)
	diff := 0
	for y := e.Bounds().Min.Y; y < e.Bounds().Max.Y; y++ {
		for x := e.Bounds().Min.X; x < e.Bounds().Max.X; x++ {
			ar, ag, ab, aa := a.At(x, y).RGBA()
			er, eg, eb, ea := e.At(x, y).RGBA()
			if ar != er || ag != eg || ab != eb || aa != ea {
				diff++
			}
		}
	}
	Expect(diff).Equals(0)
}

func TestViewUploadedImage_Resize(t *testing.T) {
	RegisterT(t)
	bus.Init()
	mockBlob(mock.UniformPNG(2000, 1000), "image/png")

	server := mock.NewServer()
	code, response := server.
		WithURL("https://demo.test.fider.io/?size=500").
		OnTenant(mock.DemoTenant).
		AddParam("bkey", "attachments/image.png").
		Execute(handlers.ViewUploadedImage())

	Expect(code).Equals(http.StatusOK)
	width, height := responseDimensions(response.Body.Bytes())
	Expect(width).Equals(500)
	Expect(height).Equals(250)
}

func TestViewUploadedImage_DecompressionBomb_ServedWithoutDecoding(t *testing.T) {
	RegisterT(t)
	bus.Init()
	bomb := mock.UniformPNG(12000, 12000)
	mockBlob(bomb, "image/png")

	server := mock.NewServer()

	var before, after runtime.MemStats
	runtime.GC()
	runtime.ReadMemStats(&before)
	code, response := server.
		WithURL("https://demo.test.fider.io/?size=500").
		OnTenant(mock.DemoTenant).
		AddParam("bkey", "attachments/bomb.png").
		Execute(handlers.ViewUploadedImage())
	runtime.ReadMemStats(&after)

	Expect(code).Equals(http.StatusOK)
	Expect(response.Body.Bytes()).Equals(bomb)
	// Decoding would allocate hundreds of MB
	Expect(after.TotalAlloc-before.TotalAlloc < 10*1024*1024).IsTrue()
}

func TestFavicon_DecompressionBomb_ServedWithoutDecoding(t *testing.T) {
	RegisterT(t)
	bus.Init()
	bomb := mock.UniformPNG(12000, 12000)
	mockBlob(bomb, "image/png")

	server := mock.NewServer()

	var before, after runtime.MemStats
	runtime.GC()
	runtime.ReadMemStats(&before)
	code, response := server.
		WithURL("https://demo.test.fider.io/?size=100&bg=white").
		OnTenant(mock.DemoTenant).
		AddParam("bkey", "logos/bomb.png").
		Execute(handlers.Favicon())
	runtime.ReadMemStats(&after)

	Expect(code).Equals(http.StatusOK)
	Expect(response.Body.Bytes()).Equals(bomb)
	Expect(after.TotalAlloc-before.TotalAlloc < 10*1024*1024).IsTrue()
}

func TestViewUploadedImage_UnsupportedFormat_ServedAsIs(t *testing.T) {
	RegisterT(t)
	bus.Init()
	// e.g. a WebP image, which imagic doesn't support
	content := []byte("RIFF\x00\x00\x00\x00WEBPVP8 ")
	mockBlob(content, "image/webp")

	server := mock.NewServer()
	code, response := server.
		WithURL("https://demo.test.fider.io/?size=500").
		OnTenant(mock.DemoTenant).
		AddParam("bkey", "attachments/image.webp").
		Execute(handlers.ViewUploadedImage())

	Expect(code).Equals(http.StatusOK)
	Expect(response.Body.Bytes()).Equals(content)
}

func TestViewUploadedImage_CorruptPixelData_ServedAsIs(t *testing.T) {
	RegisterT(t)
	bus.Init()
	// Valid header, but no image data
	content := mock.GIFHeader(100, 100)
	mockBlob(content, "image/gif")

	server := mock.NewServer()
	code, response := server.
		WithURL("https://demo.test.fider.io/?size=50").
		OnTenant(mock.DemoTenant).
		AddParam("bkey", "attachments/image.gif").
		Execute(handlers.ViewUploadedImage())

	Expect(code).Equals(http.StatusOK)
	Expect(response.Body.Bytes()).Equals(content)
}

func TestViewUploadedImage_AlreadyWithinSize_ServedAsIs(t *testing.T) {
	RegisterT(t)
	bus.Init()
	content := mock.UniformPNG(800, 600)
	mockBlob(content, "image/png")

	for _, size := range []string{"800", "1000", "2000"} {
		server := mock.NewServer()
		code, response := server.
			WithURL("https://demo.test.fider.io/?size="+size).
			OnTenant(mock.DemoTenant).
			AddParam("bkey", "attachments/image.png").
			Execute(handlers.ViewUploadedImage())

		Expect(code).Equals(http.StatusOK)
		Expect(response.Body.Bytes()).Equals(content)
	}
}

func TestFavicon_Bundled(t *testing.T) {
	RegisterT(t)
	bus.Init()

	server := mock.NewServer()
	code, response := server.
		WithURL("https://demo.test.fider.io/?size=64").
		OnTenant(mock.DemoTenant).
		Execute(handlers.Favicon())

	Expect(code).Equals(http.StatusOK)
	width, height := responseDimensions(response.Body.Bytes())
	Expect(width).Equals(64)
	Expect(height).Equals(64)
}

func TestFavicon_Resize(t *testing.T) {
	RegisterT(t)
	bus.Init()
	mockBlob(mock.UniformPNG(400, 400), "image/png")

	server := mock.NewServer()
	code, response := server.
		WithURL("https://demo.test.fider.io/?size=100").
		OnTenant(mock.DemoTenant).
		AddParam("bkey", "logos/logo.png").
		Execute(handlers.Favicon())

	Expect(code).Equals(http.StatusOK)
	width, height := responseDimensions(response.Body.Bytes())
	Expect(width).Equals(100)
	Expect(height).Equals(100)
}

func TestUnknownUser_LetterAvatarHandler(t *testing.T) {
	RegisterT(t)
	bus.Init()

	server := mock.NewServer()
	code, response := server.
		OnTenant(mock.DemoTenant).
		WithURL("https://demo.test.fider.io/?size=50").
		AddParam("id", 0).
		AddParam("name", "Jon Snow").
		Execute(handlers.Gravatar())

	expectedAvatar := []byte{137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 50, 0, 0, 0, 50, 8, 2, 0, 0, 0, 145, 93, 31, 230, 0, 0, 2, 137, 73, 68, 65, 84, 120, 156, 236, 152, 75, 79, 19, 81, 20, 199, 255, 3, 51, 244, 65, 95, 80, 74, 43, 160, 149, 135, 36, 5, 177, 169, 137, 40, 42, 209, 152, 200, 74, 19, 87, 110, 248, 84, 126, 7, 227, 70, 23, 110, 140, 137, 154, 248, 32, 33, 152, 72, 20, 13, 6, 8, 62, 210, 74, 209, 218, 161, 239, 55, 237, 152, 67, 167, 37, 141, 36, 222, 139, 19, 202, 226, 254, 114, 51, 28, 78, 239, 220, 249, 245, 204, 205, 153, 73, 101, 237, 225, 61, 28, 63, 58, 218, 45, 112, 48, 66, 139, 7, 161, 197, 131, 208, 226, 65, 104, 241, 32, 180, 120, 16, 90, 60, 8, 45, 30, 132, 22, 15, 242, 127, 157, 237, 61, 133, 241, 243, 20, 124, 120, 141, 76, 162, 177, 164, 130, 145, 41, 156, 24, 134, 205, 65, 255, 22, 114, 248, 21, 198, 151, 143, 40, 230, 143, 74, 203, 100, 129, 219, 71, 129, 210, 165, 103, 44, 54, 92, 190, 5, 187, 107, 127, 142, 98, 130, 163, 23, 167, 39, 176, 244, 20, 234, 246, 145, 104, 253, 77, 232, 26, 57, 105, 26, 214, 222, 97, 251, 59, 180, 42, 122, 188, 152, 184, 8, 179, 21, 211, 115, 120, 254, 0, 187, 21, 150, 101, 12, 221, 91, 138, 9, 253, 39, 41, 136, 108, 96, 125, 25, 105, 21, 153, 36, 194, 235, 120, 255, 10, 245, 210, 14, 140, 48, 174, 100, 104, 181, 58, 26, 95, 178, 82, 106, 201, 199, 163, 248, 246, 153, 130, 114, 169, 29, 90, 165, 2, 149, 199, 238, 162, 157, 164, 254, 68, 244, 171, 158, 175, 238, 98, 229, 13, 215, 74, 70, 55, 136, 149, 5, 212, 106, 232, 148, 105, 39, 221, 184, 139, 241, 16, 172, 246, 67, 44, 99, 180, 86, 124, 11, 11, 143, 145, 138, 83, 236, 232, 165, 205, 62, 55, 143, 43, 183, 209, 235, 109, 171, 22, 128, 68, 12, 47, 31, 97, 241, 9, 194, 27, 168, 148, 41, 227, 25, 196, 236, 29, 186, 179, 204, 24, 221, 32, 154, 196, 34, 52, 58, 58, 49, 56, 138, 201, 25, 152, 45, 8, 206, 66, 141, 210, 230, 99, 128, 185, 90, 157, 50, 186, 204, 52, 90, 144, 244, 191, 154, 70, 71, 103, 31, 134, 198, 104, 72, 141, 124, 173, 74, 205, 98, 249, 197, 222, 92, 137, 90, 63, 27, 204, 213, 26, 11, 34, 112, 129, 130, 103, 247, 145, 207, 234, 73, 171, 77, 15, 202, 69, 58, 246, 15, 97, 242, 18, 5, 233, 29, 26, 77, 82, 170, 30, 40, 102, 198, 171, 49, 87, 43, 249, 91, 15, 134, 167, 244, 192, 210, 13, 127, 128, 130, 66, 22, 185, 52, 5, 212, 214, 247, 202, 118, 238, 42, 53, 207, 58, 146, 132, 209, 198, 41, 153, 29, 198, 171, 49, 87, 43, 22, 33, 51, 151, 7, 103, 130, 240, 249, 233, 185, 235, 242, 64, 81, 232, 163, 213, 183, 250, 156, 108, 18, 171, 75, 56, 59, 131, 190, 1, 220, 156, 167, 130, 85, 138, 176, 247, 232, 61, 34, 165, 226, 199, 38, 227, 213, 36, 142, 223, 183, 76, 22, 132, 174, 147, 83, 147, 82, 129, 156, 194, 107, 45, 211, 124, 126, 4, 166, 225, 116, 239, 103, 106, 53, 108, 109, 226, 211, 162, 126, 175, 13, 214, 170, 99, 181, 195, 225, 134, 44, 211, 27, 75, 34, 70, 155, 250, 64, 186, 157, 176, 57, 233, 205, 162, 84, 160, 54, 198, 252, 216, 169, 195, 223, 32, 242, 25, 26, 255, 36, 151, 162, 113, 88, 142, 233, 219, 169, 208, 226, 65, 104, 241, 32, 180, 120, 16, 90, 60, 8, 45, 30, 132, 22, 15, 66, 139, 135, 63, 1, 0, 0, 255, 255, 117, 91, 183, 108, 67, 255, 85, 68, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130}
	Expect(code).Equals(http.StatusOK)
	Expect(response.Header().Get("Content-Type")).Equals("image/png")
	body, _ := io.ReadAll(response.Body)
	expectSamePixels(body, expectedAvatar)
}