Browse Source

started to rework memberships controller

Lou 2 years ago
parent
commit
d62d390300

+ 4
- 0
.dockerignore View File

@@ -0,0 +1,4 @@
1
+erd.pdf
2
+.git
3
+.gitignore
4
+.gitattributes

+ 65
- 0
Dockerfile View File

@@ -0,0 +1,65 @@
1
+# 
2
+# This is an unofficial Alpine image embedding rbenv
3
+# Inspired by https://github.com/didlich/docker-alpine-rbenv/blob/master/Dockerfile
4
+# 
5
+
6
+FROM alpine:3.15
7
+
8
+RUN apk add --update \
9
+    bash \
10
+    git \
11
+    wget \
12
+    curl \
13
+    vim \
14
+    build-base \
15
+    readline-dev \
16
+    openssl-dev \
17
+    zlib-dev \
18
+&& rm -rf /var/cache/apk/*
19
+
20
+# rbenv
21
+ENV PATH /usr/local/rbenv/shims:/usr/local/rbenv/bin:$PATH
22
+ENV RBENV_ROOT /usr/local/rbenv
23
+ENV RUBY_VERSION 3.0.2
24
+ENV CONFIGURE_OPTS --disable-install-doc
25
+
26
+RUN apk add --update \
27
+    linux-headers \
28
+    imagemagick-dev \    
29
+    libffi-dev \    
30
+    libffi-dev \
31
+	libpq-dev \
32
+	&& rm -rf /var/cache/apk/*
33
+
34
+RUN git clone --depth 1 git://github.com/sstephenson/rbenv.git ${RBENV_ROOT} \
35
+&&  git clone --depth 1 https://github.com/sstephenson/ruby-build.git ${RBENV_ROOT}/plugins/ruby-build \
36
+&&  git clone --depth 1 git://github.com/jf/rbenv-gemset.git ${RBENV_ROOT}/plugins/rbenv-gemset \
37
+&& ${RBENV_ROOT}/plugins/ruby-build/install.sh
38
+
39
+RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh 
40
+
41
+RUN rbenv install $RUBY_VERSION \
42
+&&  rbenv global $RUBY_VERSION
43
+
44
+RUN gem install bundler
45
+
46
+WORKDIR /app
47
+
48
+# Getting required gems
49
+COPY Gemfile ./
50
+RUN bundle install
51
+
52
+COPY [^Gemfile]* ./
53
+
54
+# RUN RAILS_ENV=production / db:create
55
+# RUN / db:migrate
56
+
57
+# Deleting useless packages
58
+# RUN apk del git wget curl build-base \
59
+#     rm -rf /var/cache/apk/*
60
+
61
+EXPOSE 9000
62
+
63
+CMD [ "top" ]
64
+
65
+# CMD [ "rails", "server", "-p 9000", "-b 127.0.0.1", "-e production" ]

+ 17
- 16
app/controllers/api/v1/memberships_controller.rb View File

@@ -3,29 +3,30 @@ class Api::V1::MembershipsController < ApplicationController
3 3
   before_action :check_login
4 4
 
5 5
   def index
6
-    render json: MembershipSerializer.new(Membership.all).serializable_hash.to_json
6
+    if !params[:team_id]
7
+      render json: MembershipSerializer.new(Membership.all).serializable_hash.to_json
8
+    else
9
+      render json: MembershipSerializer.new(Team.find(params[:team_id]).memberships).serializable_hash.to_json
10
+    end
7 11
   end
8
-
12
+  
9 13
   def show
10 14
     render json: MembershipSerializer.new(@membership).serializable_hash.to_json
11 15
   end
12 16
 
13
-  def create
14
-    member_id = User.where(username: params[:membership][:username])[0].id
15
-    team_id = Team.where(name: params[:membership][:team_name])[0].id
16
-
17
-    membership = Membership.new(team_id: team_id, member_id: member_id, can_edit: false)
18
-    
19
-    if membership.save
20
-      render json: MembershipSerializer.new(membership).serializable_hash.to_json, status: :created
21
-    else
22
-      render json: membership.errors, status: :unprocessable_entity
23
-    end
24
-  end
17
+  # def create  
18
+  #   membership = current_user.memberships.build(membership_params)
19
+  
20
+  #   if membership.save
21
+  #     render json: MembershipSerializer.new(membership).serializable_hash.to_json, status: :created
22
+  #   else
23
+  #     render json: membership.errors, status: :unprocessable_entity
24
+  #   end
25
+  # end
25 26
 
26 27
   def update
27 28
     if params[:membership][:can_edit] 
28
-      if @membership.update(memberships_params)
29
+      if @membership.update(membership_params)
29 30
         render json: MembershipSerializer.new(@membership).serializable_hash.to_json, status: :ok
30 31
       else
31 32
         render json: @membership.errors, status: :unprocessable_entity
@@ -42,7 +43,7 @@ class Api::V1::MembershipsController < ApplicationController
42 43
 
43 44
   private
44 45
 
45
-  def memberships_params
46
+  def membership_params
46 47
     params.require(:membership).permit(:can_edit)
47 48
   end
48 49
   

+ 1
- 1
app/models/activity.rb View File

@@ -5,7 +5,7 @@ class Activity < ApplicationRecord
5 5
   has_one :user, :through => :joined_user_activity
6 6
 
7 7
   has_many :tasks
8
-
8
+  
9 9
   validates :name, presence: true
10 10
   validates :author, presence: true
11 11
 end

+ 0
- 3
app/models/membership.rb View File

@@ -1,7 +1,4 @@
1 1
 class Membership < ApplicationRecord
2
-  # attr_readonly :member
3
-  # attr_readonly :team
4
-
5 2
   validates :member, presence: :true
6 3
   validates :team, presence: :true
7 4
   validates_uniqueness_of :member, scope: :team, message: "user already part of this team"

+ 3
- 3
app/models/task.rb View File

@@ -1,9 +1,9 @@
1 1
 class Task < ApplicationRecord
2
+  belongs_to :activity
3
+  belongs_to :owner, class_name: "User"
4
+
2 5
   validates :name, presence: true
3 6
   validates :owner, presence: true
4 7
   validates :activity, presence: true
5 8
   validates :start_time, presence: true
6
-
7
-  belongs_to :activity
8
-  belongs_to :owner, class_name: "User"
9 9
 end

+ 13
- 23
config/database.yml View File

@@ -1,31 +1,21 @@
1
-# SQLite. Versions 3.8.0 and up are supported.
2
-#   gem install sqlite3
3
-#
4
-#   Ensure the SQLite 3 gem is defined in your Gemfile
5
-#   gem 'sqlite3'
6
-#
7 1
 default: &default
8
-  # adapter: sqlite3
9
-  adapter: postgresql
10
-  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
11
-  timeout: 5000
12
-  username: <%= ENV['PG_USER'] %>
13
-  password: <%= ENV['PG_USER_PWD'] %>
14
-
15
-development:
16
-  <<: *default
17
-  # database: db/development.sqlite3
18
-  database: chronobriq_development
2
+    adapter: postgresql
3
+    pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
4
+    timeout: 5000
5
+    username: <%= ENV['PG_USER'] %>
6
+    password: <%= ENV['PG_USER_PWD'] %>
19 7
 
20 8
 # Warning: The database defined as "test" will be erased and
21 9
 # re-generated from your development database when you run "rake".
22 10
 # Do not set this db to the same as development or production.
23 11
 test:
24
-  <<: *default
25
-  # database: db/test.sqlite3
26
-  database: chronobriq_test
12
+    <<: *default
13
+    database: chronobriq_test
14
+
15
+development:
16
+    <<: *default
17
+    database: chronobriq_development
27 18
 
28 19
 production:
29
-  <<: *default
30
-  # database: db/production.sqlite3
31
-  database: chronobriq_production
20
+    <<: *default
21
+    database: chronobriq_production

+ 5
- 3
config/routes.rb View File

@@ -3,12 +3,14 @@ Rails.application.routes.draw do
3 3
     namespace :v1 do
4 4
       resources :users
5 5
       resources :tokens, only: %i[create]
6
-      resources :teams
7
-      resources :memberships
6
+      resources :teams do
7
+        resources :memberships
8
+      end
9
+      resources :memberships, only: %i[index]
8 10
       resources :activities do
9 11
         resources :tasks
10 12
       end
11
-      resources :tasks
13
+      resources :tasks, only: %i[index]
12 14
     end
13 15
   end
14 16
 end

+ 15
- 0
docker-compose.yml View File

@@ -0,0 +1,15 @@
1
+version: "3"
2
+services:
3
+    chronobriq-api:
4
+        build: .
5
+        ports:
6
+            - "9000:9000"
7
+        depends_on:
8
+            - db
9
+    db:
10
+        image: postgres:12-alpine
11
+        volumes:
12
+            - database:/var/lib/postgresql/chronobriq-api
13
+
14
+volumes:
15
+    database:

+ 60
- 50
test/controllers/api/v1/memberships_controller_test.rb View File

@@ -10,29 +10,39 @@ class Api::V1::MembershipsControllerTest < ActionDispatch::IntegrationTest
10 10
   end
11 11
 
12 12
   # INDEX
13
-  test "should access Membership index" do
14
-    get api_v1_memberships_url,
13
+  test "should access Membership index - specifix team" do
14
+    get api_v1_team_memberships_url(@team),
15 15
     headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
16 16
     as: :json
17 17
     assert_response :success
18
+
19
+    json_response = JSON.parse(self.response.body)
20
+    assert_equal @membership.member_id, json_response['data'][0]['attributes']['member_id']
18 21
   end
19 22
 
20
-  test "should forbid Membership index" do
23
+  test "should access Membership index - all teams" do
21 24
     get api_v1_memberships_url,
25
+    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
26
+    as: :json
27
+    assert_response :success
28
+  end
29
+
30
+  test "should not access Membership index" do
31
+    get api_v1_team_memberships_url(@team),
22 32
     as: :json
23 33
     assert_response :forbidden
24 34
   end
25 35
 
26 36
   # SHOW
27 37
   test "should show Membership" do
28
-    get api_v1_memberships_url(@membership),
38
+    get api_v1_team_memberships_url(@team, @membership),
29 39
     headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
30 40
     as: :json
31 41
     assert_response :success
32 42
   end
33 43
 
34 44
   test "should forbid show Membership" do
35
-    get api_v1_memberships_url(@membership),
45
+    get api_v1_team_memberships_url(@team, @membership),
36 46
     as: :json
37 47
     assert_response :forbidden
38 48
   end
@@ -41,60 +51,60 @@ class Api::V1::MembershipsControllerTest < ActionDispatch::IntegrationTest
41 51
   # test "should create membership" do
42 52
   #   assert_difference("Membership.count") do
43 53
   #     post api_v1_memberships_url,
44
-  #     params: { membership: { team_id: @team.id, member_id: @user.id, can_edit: false } },
54
+  #     params: { membership: { team_id: @team2.id, member_id: @user2.id, can_edit: false } },
45 55
   #     headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
46 56
   #     as: :json
47 57
   #   end
48 58
   #   assert_response :created
49 59
   # end
50 60
 
51
-  test "should forbid create membership" do
52
-    assert_no_difference("Membership.count") do
53
-      post api_v1_memberships_url,
54
-      params: { membership: { team_id: @team.id, member_id: @user.id, can_edit: false } },
55
-      as: :json
56
-    end
57
-    assert_response :forbidden
58
-  end
61
+  # test "should forbid create membership" do
62
+  #   assert_no_difference("Membership.count") do
63
+  #     post api_v1_memberships_url,
64
+  #     params: { membership: { team_id: @team.id, member_id: @user.id, can_edit: false } },
65
+  #     as: :json
66
+  #   end
67
+  #   assert_response :forbidden
68
+  # end
59 69
 
60
-  # UPDATE
61
-  test "should update membership" do
62
-    patch api_v1_membership_url(@membership),
63
-    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
64
-    params: { membership: { can_edit: true } },
65
-    as: :json
66
-    assert_response :success
67
-  end
70
+  # # UPDATE
71
+  # test "should update membership" do
72
+  #   patch api_v1_membership_url(@membership),
73
+  #   headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
74
+  #   params: { membership: { can_edit: true } },
75
+  #   as: :json
76
+  #   assert_response :success
77
+  # end
68 78
 
69
-  test "should forbid update membership" do
70
-    patch api_v1_membership_url(@membership),
71
-    params: { membership: { can_edit: true } },
72
-    as: :json
73
-    assert_response :forbidden
74
-  end
79
+  # test "should forbid update membership" do
80
+  #   patch api_v1_membership_url(@membership),
81
+  #   params: { membership: { can_edit: true } },
82
+  #   as: :json
83
+  #   assert_response :forbidden
84
+  # end
75 85
 
76
-  test "should forbid update read_only attributes - team_id" do
77
-    patch api_v1_membership_url(@membership),
78
-    headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
79
-    params: { membership: { team_id: @team2.id, member_id: @user2.id } },
80
-    as: :json
81
-    assert_response :unprocessable_entity
82
-  end
86
+  # test "should forbid update read_only attributes - team_id" do
87
+  #   patch api_v1_membership_url(@membership),
88
+  #   headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, 
89
+  #   params: { membership: { team_id: @team2.id, member_id: @user2.id } },
90
+  #   as: :json
91
+  #   assert_response :unprocessable_entity
92
+  # end
83 93
 
84
-  # DESTROY
85
-  test "should destroy membership" do
86
-    assert_difference('Membership.count', -1) do
87
-      delete api_v1_membership_url(@membership), 
88
-      headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
89
-      as: :json
90
-    end
91
-    assert_response :no_content
92
-  end
94
+  # # DESTROY
95
+  # test "should destroy membership" do
96
+  #   assert_difference('Membership.count', -1) do
97
+  #     delete api_v1_membership_url(@membership), 
98
+  #     headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
99
+  #     as: :json
100
+  #   end
101
+  #   assert_response :no_content
102
+  # end
93 103
 
94
-  test "should forbid destroy membership" do
95
-    assert_no_difference('Membership.count') do
96
-      delete api_v1_membership_url(@membership), as: :json
97
-    end
98
-    assert_response :forbidden
99
-  end
104
+  # test "should forbid destroy membership" do
105
+  #   assert_no_difference('Membership.count') do
106
+  #     delete api_v1_membership_url(@membership), as: :json
107
+  #   end
108
+  #   assert_response :forbidden
109
+  # end
100 110
 end

+ 3
- 3
test/controllers/api/v1/tasks_controller_test.rb View File

@@ -44,17 +44,17 @@ class Api::V1::TasksControllerTest < ActionDispatch::IntegrationTest
44 44
   test "should create task" do
45 45
     assert_difference("Task.count") do
46 46
       post api_v1_activity_tasks_url(@activity),
47
-      params: { task: { name: @task.name, description: @task.description, owner_id: @user, activity_id: @activity, start_time: DateTime.now } },
47
+      params: { task: { name: @task.name, description: @task.description, owner_id: @user, start_time: DateTime.now } },
48 48
       headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
49 49
       as: :json
50 50
     end
51 51
     assert_response :created
52 52
   end
53 53
 
54
-  test "should not create task" do
54
+  test "should not create task - not logged in" do
55 55
     assert_no_difference("Task.count") do
56 56
       post api_v1_activity_tasks_url(@activity),
57
-      params: { task: { name: @task.name, description: @task.description, owner_id: @user, activity_id: @activity } },
57
+      params: { task: { name: @task.name, description: @task.description, owner_id: @user } },
58 58
       as: :json
59 59
     end
60 60
     assert_response :forbidden

Loading…
Cancel
Save