11917: Do not clear rails cache at boot time.
[arvados.git] / apps / workbench / test / controllers / repositories_controller_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6 require 'helpers/repository_stub_helper'
7 require 'helpers/share_object_helper'
8
9 class RepositoriesControllerTest < ActionController::TestCase
10   include RepositoryStubHelper
11   include ShareObjectHelper
12
13   [
14     :active, #owner
15     :admin,
16   ].each do |user|
17     test "#{user} shares repository with a user and group" do
18       uuid_list = [api_fixture("groups")["future_project_viewing_group"]["uuid"],
19                    api_fixture("users")["future_project_user"]["uuid"]]
20       post(:share_with, {
21              id: api_fixture("repositories")["foo"]["uuid"],
22              uuids: uuid_list,
23              format: "json"},
24            session_for(user))
25       assert_response :success
26       assert_equal(uuid_list, json_response["success"])
27     end
28   end
29
30   test "user with repository read permission cannot add permissions" do
31     share_uuid = api_fixture("users")["project_viewer"]["uuid"]
32     post(:share_with, {
33            id: api_fixture("repositories")["arvados"]["uuid"],
34            uuids: [share_uuid],
35            format: "json"},
36          session_for(:spectator))
37     assert_response 422
38     assert(json_response["errors"].andand.
39              any? { |msg| msg.start_with?("#{share_uuid}: ") },
40            "JSON response missing properly formatted sharing error")
41   end
42
43   test "admin can_manage repository" do
44     assert user_can_manage(:admin, api_fixture("repositories")["foo"])
45   end
46
47   test "owner can_manage repository" do
48     assert user_can_manage(:active, api_fixture("repositories")["foo"])
49   end
50
51   test "viewer cannot manage repository" do
52     refute user_can_manage(:spectator, api_fixture("repositories")["arvados"])
53   end
54
55   [
56     [:active, ['#Sharing', '#Advanced']],
57     [:admin,  ['#Attributes', '#Sharing', '#Advanced']],
58   ].each do |user, expected_panes|
59     test "#{user} sees panes #{expected_panes}" do
60       get :show, {
61         id: api_fixture('repositories')['foo']['uuid']
62       }, session_for(user)
63       assert_response :success
64
65       panes = css_select('[data-toggle=tab]').each do |pane|
66         pane_name = pane.attributes['href']
67         assert_includes expected_panes, pane_name
68       end
69     end
70   end
71
72   ### Browse repository content
73
74   [:active, :spectator].each do |user|
75     test "show tree to #{user}" do
76       reset_api_fixtures_after_test false
77       sha1, _, _ = stub_repo_content
78       get :show_tree, {
79         id: api_fixture('repositories')['foo']['uuid'],
80         commit: sha1,
81       }, session_for(user)
82       assert_response :success
83       assert_select 'tr td a', 'COPYING'
84       assert_select 'tr td', '625 bytes'
85       assert_select 'tr td a', 'apps'
86       assert_select 'tr td a', 'workbench'
87       assert_select 'tr td a', 'Gemfile'
88       assert_select 'tr td', '33.7 KiB'
89     end
90
91     test "show commit to #{user}" do
92       reset_api_fixtures_after_test false
93       sha1, commit, _ = stub_repo_content
94       get :show_commit, {
95         id: api_fixture('repositories')['foo']['uuid'],
96         commit: sha1,
97       }, session_for(user)
98       assert_response :success
99       assert_select 'pre', h(commit)
100     end
101
102     test "show blob to #{user}" do
103       reset_api_fixtures_after_test false
104       sha1, _, filedata = stub_repo_content filename: 'COPYING'
105       get :show_blob, {
106         id: api_fixture('repositories')['foo']['uuid'],
107         commit: sha1,
108         path: 'COPYING',
109       }, session_for(user)
110       assert_response :success
111       assert_select 'pre', h(filedata)
112     end
113   end
114
115   ['', '/'].each do |path|
116     test "show tree with path '#{path}'" do
117       reset_api_fixtures_after_test false
118       sha1, _, _ = stub_repo_content filename: 'COPYING'
119       get :show_tree, {
120         id: api_fixture('repositories')['foo']['uuid'],
121         commit: sha1,
122         path: path,
123       }, session_for(:active)
124       assert_response :success
125       assert_select 'tr td', 'COPYING'
126     end
127   end
128 end