GitRoot

Craft your forge, Build your project, Grow your community freely
  1// SPDX-FileCopyrightText: 2026 Romain Maneschi <romain@gitroot.dev>
  2//
  3// SPDX-License-Identifier: EUPL-1.2
  4
  5package user
  6
  7import (
  8	_ "embed"
  9	"testing"
 10)
 11
 12//go:embed test_ressources/users.yml
 13var users []byte
 14
 15//go:embed test_ressources/users_no_anonymous.yml
 16var usersNoAnonymous []byte
 17
 18//go:embed test_ressources/contributors.yml
 19var contributors []byte
 20
 21const myPubKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHk+AMq9/FbuwoJXJ1xoClIOtucWwY3+KX0bcnwXMVNm"
 22const mermaidPubKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJ/CNCQRucc6Ge+ZTkxeIbuM2drMbt0Hu7tBBrEzs4Fq"
 23const anonymous = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOXwAG4f53d03BUl1e7dOMRLHWNIlCto2biAJhQQXrde"
 24
 25func TestUserCanWrite(t *testing.T) {
 26	user, err := FindUser(users, myPubKey)
 27	if err != nil {
 28		t.Fatal(err)
 29	}
 30	if user.Pseudo != "romain" {
 31		t.Fatalf("no good pseudo `%s`", user.Pseudo)
 32	}
 33	if !user.CanWrite("startBlog") {
 34		t.Fatal("should be able to write")
 35	}
 36	if user.NeedToBeCreated("startBlog") {
 37		t.Fatal("user should not created to write in startBlog")
 38	}
 39	if user.IsAnonymous() {
 40		t.Fatal("user is not anonymous")
 41	}
 42
 43	userMermaid, err := FindUser(users, mermaidPubKey)
 44	if err != nil {
 45		t.Fatal(err)
 46	}
 47	if userMermaid.Pseudo != "apex_mermaid" {
 48		t.Fatalf("no good pseudo for apex_mermaid `%s`", userMermaid.Pseudo)
 49	}
 50	if userMermaid.CanWrite("startBlog") {
 51		t.Fatal("should not be able to write")
 52	}
 53	if userMermaid.IsAnonymous() {
 54		t.Fatal("userMermaid is not anonymous")
 55	}
 56
 57	anonymousUser, err := FindUser(users, anonymous)
 58	if err != nil {
 59		t.Fatal(err)
 60	}
 61	if anonymousUser.Pseudo != "" {
 62		t.Fatalf("no good pseudo for anonymous `%s`", anonymousUser.Pseudo)
 63	}
 64	if anonymousUser.CanWrite("main") {
 65		t.Fatal("should not be able to write")
 66	}
 67	if anonymousUser.NeedToBeCreated("main") {
 68		t.Fatal("anonymous should not created to write in main")
 69	}
 70	if !anonymousUser.NeedToBeCreated("unexisting") {
 71		t.Fatal("anonymous should be created to write in unexisting")
 72	}
 73	if !anonymousUser.IsAnonymous() {
 74		t.Fatal("anonymousUser is anonymous")
 75	}
 76}
 77
 78func TestUserCanWriteNoAnonymous(t *testing.T) {
 79	user, err := FindUser(usersNoAnonymous, myPubKey)
 80	if err != nil {
 81		t.Fatal(err)
 82	}
 83	if user.Pseudo != "romain" {
 84		t.Fatalf("no good pseudo `%s`", user.Pseudo)
 85	}
 86	if !user.CanWrite("startBlog") {
 87		t.Fatal("should be able to write")
 88	}
 89	if user.NeedToBeCreated("startBlog") {
 90		t.Fatal("user should not created to write in startBlog")
 91	}
 92	if user.NeedToBeCreated("unexisting") {
 93		t.Fatal("user should not created to write in unexisting")
 94	}
 95	if user.IsAnonymous() {
 96		t.Fatal("user is not anonymous")
 97	}
 98
 99	anonymousUser, err := FindUser(usersNoAnonymous, anonymous)
100	if err != nil {
101		t.Fatal(err)
102	}
103	if anonymousUser.Pseudo != "" {
104		t.Fatalf("no good pseudo for anonymous `%s`", anonymousUser.Pseudo)
105	}
106	if anonymousUser.CanWrite("main") {
107		t.Fatal("should not be able to write")
108	}
109	if anonymousUser.NeedToBeCreated("main") {
110		t.Fatal("anonymous should not created to write in main")
111	}
112	if anonymousUser.NeedToBeCreated("unexisting") {
113		t.Fatal("anonymous is not authorized to write in unexisting")
114	}
115	if !anonymousUser.IsAnonymous() {
116		t.Fatal("anonymousUser is anonymous")
117	}
118}
119
120func TestUserCanWriteContributors(t *testing.T) {
121	user, err := FindUser(contributors, myPubKey)
122	if err != nil {
123		t.Fatal(err)
124	}
125	if user.Pseudo != "romain" {
126		t.Fatalf("no good pseudo `%s`", user.Pseudo)
127	}
128	if !user.CanWrite("main") {
129		t.Fatal("should be able to write in main")
130	}
131	if !user.CanWrite("unexisting") {
132		t.Fatal("should be able to write in unexisting")
133	}
134	if user.NeedToBeCreated("unexisting") {
135		t.Fatal("user should not created to write in unexisting")
136	}
137	if user.IsAnonymous() {
138		t.Fatal("user is not anonymous")
139	}
140
141	userMermaid, err := FindUser(contributors, mermaidPubKey)
142	if err != nil {
143		t.Fatal(err)
144	}
145	if userMermaid.Pseudo != "apex_mermaid" {
146		t.Fatalf("no good pseudo for apex_mermaid `%s`", userMermaid.Pseudo)
147	}
148	if userMermaid.CanWrite("main") {
149		t.Fatal("userMermaid should not be able to write in main")
150	}
151	if !userMermaid.CanWrite("unexisting") {
152		t.Fatal("userMermaid should be able to write in unexisting")
153	}
154	if userMermaid.NeedToBeCreated("unexisting") {
155		t.Fatal("userMermaid should not create to be able to write in unexisting")
156	}
157	if userMermaid.IsAnonymous() {
158		t.Fatal("userMermaid is not anonymous")
159	}
160
161	anonymousUser, err := FindUser(contributors, anonymous)
162	if err != nil {
163		t.Fatal(err)
164	}
165	if anonymousUser.Pseudo != "" {
166		t.Fatalf("no good pseudo for anonymous `%s`", anonymousUser.Pseudo)
167	}
168	if anonymousUser.CanWrite("main") {
169		t.Fatal("should not be able to write")
170	}
171	if anonymousUser.NeedToBeCreated("main") {
172		t.Fatal("anonymous should not created to write in main")
173	}
174	if anonymousUser.NeedToBeCreated("unexisting") {
175		t.Fatal("anonymous is not authorized to write in unexisting")
176	}
177	if !anonymousUser.IsAnonymous() {
178		t.Fatal("anonymousUser is anonymous")
179	}
180}