Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
pleroma
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
kaniini
pleroma
Commits
373753e5
Commit
373753e5
authored
7 years ago
by
lain
Browse files
Options
Downloads
Patches
Plain Diff
Add some basic changesets.
parent
e12a6d56
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
lib/pleroma/object.ex
+8
-1
8 additions, 1 deletion
lib/pleroma/object.ex
lib/pleroma/user.ex
+14
-0
14 additions, 0 deletions
lib/pleroma/user.ex
test/object_test.exs
+12
-1
12 additions, 1 deletion
test/object_test.exs
test/user_test.exs
+41
-0
41 additions, 0 deletions
test/user_test.exs
with
75 additions
and
2 deletions
lib/pleroma/object.ex
+
8
−
1
View file @
373753e5
defmodule
Pleroma
.
Object
do
use
Ecto
.
Schema
alias
Pleroma
.
{
Repo
,
Object
}
import
Ecto
.
Query
import
Ecto
.
{
Query
,
Changeset
}
schema
"objects"
do
field
:data
,
:map
...
...
@@ -9,6 +9,13 @@ defmodule Pleroma.Object do
timestamps
()
end
def
change
(
struct
,
params
\\
%{})
do
changeset
=
struct
|>
cast
(
params
,
[
:data
])
|>
validate_required
([
:data
])
|>
unique_constraint
(
:ap_id
,
name:
:objects_unique_apid_index
)
end
def
get_by_ap_id
(
ap_id
)
do
Repo
.
one
(
from
object
in
Object
,
where:
fragment
(
"? @> ?"
,
object
.
data
,
^
%{
id:
ap_id
}))
...
...
This diff is collapsed.
Click to expand it.
lib/pleroma/user.ex
+
14
−
0
View file @
373753e5
...
...
@@ -61,6 +61,17 @@ defmodule Pleroma.User do
}
end
@email_regex
~r/^[a-zA-Z0-9.!#$%&'*+\/
=
?^
_
`
{
|
}
~
-
]
+
@
[
a
-
zA
-
Z0
-
9
](
?:
[
a
-
zA
-
Z0
-
9
-
]{
0
,
61
}[
a
-
zA
-
Z0
-
9
])
?(?:
\
.
[
a
-
zA
-
Z0
-
9
](
?:
[
a
-
zA
-
Z0
-
9
-
]{
0
,
61
}[
a
-
zA
-
Z0
-
9
])
?)
*
$
/
def
remote_user_creation
(
params
)
do
changeset
=
%
User
{}
|>
cast
(
params
,
[
:bio
,
:name
,
:ap_id
,
:nickname
,
:info
])
|>
validate_required
([
:bio
,
:name
,
:ap_id
,
:nickname
])
|>
unique_constraint
(
:nickname
)
|>
validate_format
(
:nickname
,
@email_regex
)
|>
validate_length
(
:bio
,
max:
1000
)
|>
validate_length
(
:name
,
max:
100
)
end
def
register_changeset
(
struct
,
params
\\
%{})
do
changeset
=
struct
|>
cast
(
params
,
[
:bio
,
:email
,
:name
,
:nickname
,
:password
,
:password_confirmation
])
...
...
@@ -69,6 +80,9 @@ defmodule Pleroma.User do
|>
unique_constraint
(
:email
)
|>
unique_constraint
(
:nickname
)
|>
validate_format
(
:nickname
,
~r/^[a-zA-Z\d]+$/
)
|>
validate_format
(
:email
,
@email_regex
)
|>
validate_length
(
:bio
,
max:
1000
)
|>
validate_length
(
:name
,
max:
100
)
if
changeset
.
valid?
do
hashed
=
Pbkdf2
.
hashpwsalt
(
changeset
.
changes
[
:password
])
...
...
This diff is collapsed.
Click to expand it.
test/object_test.exs
+
12
−
1
View file @
373753e5
defmodule
Pleroma
.
ObjectTest
do
use
Pleroma
.
DataCase
import
Pleroma
.
Factory
alias
Pleroma
.
{
Repo
,
Object
}
test
"returns an object by it's AP id"
do
object
=
insert
(
:note
)
found_object
=
Pleroma
.
Object
.
get_by_ap_id
(
object
.
data
[
"id"
])
found_object
=
Object
.
get_by_ap_id
(
object
.
data
[
"id"
])
assert
object
==
found_object
end
describe
"generic changeset"
do
test
"it ensures uniqueness of the id"
do
object
=
insert
(
:note
)
cs
=
Object
.
change
(%
Object
{},
%{
data:
%{
id:
object
.
data
[
"id"
]}})
assert
cs
.
valid?
{
:error
,
result
}
=
Repo
.
insert
(
cs
)
end
end
end
This diff is collapsed.
Click to expand it.
test/user_test.exs
+
41
−
0
View file @
373753e5
...
...
@@ -139,5 +139,46 @@ defmodule Pleroma.UserTest do
user
=
insert
(
:user
)
assert
User
.
ap_followers
(
user
)
==
Pleroma
.
Web
.
Router
.
Helpers
.
o_status_url
(
Pleroma
.
Web
.
Endpoint
,
:feed_redirect
,
user
.
nickname
)
<>
"/followers"
end
describe
"remote user creation changeset"
do
@valid_remote
%{
bio:
"hello"
,
name:
"Someone"
,
nickname:
"a@b.de"
,
ap_id:
"http..."
,
info:
%{
some:
"info"
}
}
test
"it confirms validity"
do
cs
=
User
.
remote_user_creation
(
@valid_remote
)
assert
cs
.
valid?
end
test
"it enforces the fqn format for nicknames"
do
cs
=
User
.
remote_user_creation
(%{
@valid_remote
|
nickname:
"bla"
})
refute
cs
.
valid?
end
test
"it has required fields"
do
[
:bio
,
:name
,
:nickname
,
:ap_id
]
|>
Enum
.
each
(
fn
(
field
)
->
cs
=
User
.
remote_user_creation
(
Map
.
delete
(
@valid_remote
,
field
))
refute
cs
.
valid?
end
)
end
test
"it restricts some sizes"
do
[
bio:
1000
,
name:
100
]
|>
Enum
.
each
(
fn
({
field
,
size
})
->
string
=
String
.
pad_leading
(
"."
,
size
)
cs
=
User
.
remote_user_creation
(
Map
.
put
(
@valid_remote
,
field
,
string
))
assert
cs
.
valid?
string
=
String
.
pad_leading
(
"."
,
size
+
1
)
cs
=
User
.
remote_user_creation
(
Map
.
put
(
@valid_remote
,
field
,
string
))
refute
cs
.
valid?
end
)
end
end
end
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment